/ components / ui / alert.tsx
alert.tsx
 1  import * as React from 'react';
 2  import { cva, type VariantProps } from 'class-variance-authority';
 3  
 4  import { cn } from '@/lib/utils';
 5  
 6  const alertVariants = cva(
 7    'relative w-full rounded-lg border px-4 py-3 text-sm grid has-[>svg]:grid-cols-[calc(var(--spacing)*4)_1fr] grid-cols-[0_1fr] has-[>svg]:gap-x-3 gap-y-0.5 items-start [&>svg]:size-4 [&>svg]:translate-y-0.5 [&>svg]:text-current',
 8    {
 9      variants: {
10        variant: {
11          default: 'bg-card text-card-foreground',
12          destructive:
13            'text-destructive bg-card [&>svg]:text-current *:data-[slot=alert-description]:text-destructive/90',
14        },
15      },
16      defaultVariants: {
17        variant: 'default',
18      },
19    }
20  );
21  
22  function Alert({ className, variant, ...props }: React.ComponentProps<'div'> & VariantProps<typeof alertVariants>) {
23    return <div data-slot="alert" role="alert" className={cn(alertVariants({ variant }), className)} {...props} />;
24  }
25  
26  function AlertTitle({ className, ...props }: React.ComponentProps<'div'>) {
27    return (
28      <div
29        data-slot="alert-title"
30        className={cn('col-start-2 line-clamp-1 min-h-4 font-medium tracking-tight', className)}
31        {...props}
32      />
33    );
34  }
35  
36  function AlertDescription({ className, ...props }: React.ComponentProps<'div'>) {
37    return (
38      <div
39        data-slot="alert-description"
40        className={cn(
41          'text-muted-foreground col-start-2 grid justify-items-start gap-1 text-sm [&_p]:leading-relaxed',
42          className
43        )}
44        {...props}
45      />
46    );
47  }
48  
49  export { Alert, AlertTitle, AlertDescription };