alert.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "group/alert relative grid w-full gap-0.5 rounded-lg border px-2.5 py-2 text-left text-sm has-data-[slot=alert-action]:relative has-data-[slot=alert-action]:pr-18 has-[>svg]:grid-cols-[auto_1fr] has-[>svg]:gap-x-2 *:[svg]:row-span-2 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4",
  6. {
  7. variants: {
  8. variant: {
  9. default: 'bg-card text-card-foreground',
  10. destructive:
  11. 'bg-card text-destructive *:data-[slot=alert-description]:text-destructive/90 *:[svg]:text-current',
  12. },
  13. },
  14. defaultVariants: {
  15. variant: 'default',
  16. },
  17. },
  18. );
  19. function Alert({
  20. className,
  21. variant,
  22. ...props
  23. }: React.ComponentProps<'div'> & VariantProps<typeof alertVariants>) {
  24. return (
  25. <div
  26. data-slot="alert"
  27. role="alert"
  28. className={cn(alertVariants({ variant }), className)}
  29. {...props}
  30. />
  31. );
  32. }
  33. function AlertTitle({ className, ...props }: React.ComponentProps<'div'>) {
  34. return (
  35. <div
  36. data-slot="alert-title"
  37. className={cn(
  38. 'font-medium group-has-[>svg]/alert:col-start-2 [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground',
  39. className,
  40. )}
  41. {...props}
  42. />
  43. );
  44. }
  45. function AlertDescription({
  46. className,
  47. ...props
  48. }: React.ComponentProps<'div'>) {
  49. return (
  50. <div
  51. data-slot="alert-description"
  52. className={cn(
  53. 'text-sm text-balance text-muted-foreground md:text-pretty [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p:not(:last-child)]:mb-4',
  54. className,
  55. )}
  56. {...props}
  57. />
  58. );
  59. }
  60. function AlertAction({ className, ...props }: React.ComponentProps<'div'>) {
  61. return (
  62. <div
  63. data-slot="alert-action"
  64. className={cn('absolute top-2 right-2', className)}
  65. {...props}
  66. />
  67. );
  68. }
  69. export { Alert, AlertTitle, AlertDescription, AlertAction };