alert.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import * as React from "react"
  2. import { cva, type VariantProps } from "class-variance-authority"
  3. import { cn } from "@/lib/utils"
  4. const alertVariants = cva(
  5. "relative w-full rounded-lg border p-4 [&>svg~*]:pl-7 [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground",
  6. {
  7. variants: {
  8. variant: {
  9. default: "bg-background text-foreground",
  10. destructive:
  11. "border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive",
  12. },
  13. },
  14. defaultVariants: {
  15. variant: "default",
  16. },
  17. }
  18. )
  19. const Alert = React.forwardRef<
  20. HTMLDivElement,
  21. React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof alertVariants>
  22. >(({ className, variant, ...props }, ref) => (
  23. <div
  24. ref={ref}
  25. role="alert"
  26. className={cn(alertVariants({ variant }), className)}
  27. {...props}
  28. />
  29. ))
  30. Alert.displayName = "Alert"
  31. const AlertTitle = React.forwardRef<
  32. HTMLParagraphElement,
  33. React.HTMLAttributes<HTMLHeadingElement>
  34. >(({ className, ...props }, ref) => (
  35. <h5
  36. ref={ref}
  37. className={cn("mb-1 font-medium leading-none tracking-tight", className)}
  38. {...props}
  39. />
  40. ))
  41. AlertTitle.displayName = "AlertTitle"
  42. const AlertDescription = React.forwardRef<
  43. HTMLParagraphElement,
  44. React.HTMLAttributes<HTMLParagraphElement>
  45. >(({ className, ...props }, ref) => (
  46. <div
  47. ref={ref}
  48. className={cn("text-sm [&_p]:leading-relaxed", className)}
  49. {...props}
  50. />
  51. ))
  52. AlertDescription.displayName = "AlertDescription"
  53. export { Alert, AlertTitle, AlertDescription }