sheet.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import * as React from "react"
  2. import * as SheetPrimitive from "@radix-ui/react-dialog"
  3. import { cva, type VariantProps } from "class-variance-authority"
  4. import { X } from "lucide-react"
  5. import { cn } from "@/lib/utils"
  6. const Sheet = SheetPrimitive.Root
  7. const SheetTrigger = SheetPrimitive.Trigger
  8. const SheetClose = SheetPrimitive.Close
  9. const SheetPortal = SheetPrimitive.Portal
  10. const SheetOverlay = React.forwardRef<
  11. React.ElementRef<typeof SheetPrimitive.Overlay>,
  12. React.ComponentPropsWithoutRef<typeof SheetPrimitive.Overlay>
  13. >(({ className, ...props }, ref) => (
  14. <SheetPrimitive.Overlay
  15. className={cn(
  16. "fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
  17. className
  18. )}
  19. {...props}
  20. ref={ref}
  21. />
  22. ))
  23. SheetOverlay.displayName = SheetPrimitive.Overlay.displayName
  24. const sheetVariants = cva(
  25. "fixed z-50 gap-4 bg-background p-6 shadow-lg transition ease-in-out data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-300 data-[state=open]:duration-500",
  26. {
  27. variants: {
  28. side: {
  29. top: "inset-x-0 top-0 border-b data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top",
  30. bottom:
  31. "inset-x-0 bottom-0 border-t data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom",
  32. left: "inset-y-0 left-0 h-full w-3/4 border-r data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left sm:max-w-sm",
  33. right:
  34. "inset-y-0 right-0 h-full w-full border-l data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right sm:max-w-md",
  35. },
  36. },
  37. defaultVariants: {
  38. side: "right",
  39. },
  40. }
  41. )
  42. interface SheetContentProps
  43. extends React.ComponentPropsWithoutRef<typeof SheetPrimitive.Content>,
  44. VariantProps<typeof sheetVariants> {}
  45. const SheetContent = React.forwardRef<
  46. React.ElementRef<typeof SheetPrimitive.Content>,
  47. SheetContentProps
  48. >(({ side = "right", className, children, ...props }, ref) => (
  49. <SheetPortal>
  50. <SheetOverlay />
  51. <SheetPrimitive.Content
  52. ref={ref}
  53. className={cn(sheetVariants({ side }), className)}
  54. {...props}
  55. >
  56. {children}
  57. <SheetPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-secondary">
  58. <X className="h-4 w-4" />
  59. <span className="sr-only">Close</span>
  60. </SheetPrimitive.Close>
  61. </SheetPrimitive.Content>
  62. </SheetPortal>
  63. ))
  64. SheetContent.displayName = SheetPrimitive.Content.displayName
  65. const SheetHeader = ({
  66. className,
  67. ...props
  68. }: React.HTMLAttributes<HTMLDivElement>) => (
  69. <div
  70. className={cn(
  71. "flex flex-col space-y-2 text-center sm:text-left",
  72. className
  73. )}
  74. {...props}
  75. />
  76. )
  77. SheetHeader.displayName = "SheetHeader"
  78. const SheetFooter = ({
  79. className,
  80. ...props
  81. }: React.HTMLAttributes<HTMLDivElement>) => (
  82. <div
  83. className={cn(
  84. "flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
  85. className
  86. )}
  87. {...props}
  88. />
  89. )
  90. SheetFooter.displayName = "SheetFooter"
  91. const SheetTitle = React.forwardRef<
  92. React.ElementRef<typeof SheetPrimitive.Title>,
  93. React.ComponentPropsWithoutRef<typeof SheetPrimitive.Title>
  94. >(({ className, ...props }, ref) => (
  95. <SheetPrimitive.Title
  96. ref={ref}
  97. className={cn("font-display text-lg font-semibold text-foreground", className)}
  98. {...props}
  99. />
  100. ))
  101. SheetTitle.displayName = SheetPrimitive.Title.displayName
  102. const SheetDescription = React.forwardRef<
  103. React.ElementRef<typeof SheetPrimitive.Description>,
  104. React.ComponentPropsWithoutRef<typeof SheetPrimitive.Description>
  105. >(({ className, ...props }, ref) => (
  106. <SheetPrimitive.Description
  107. ref={ref}
  108. className={cn("text-sm text-muted-foreground", className)}
  109. {...props}
  110. />
  111. ))
  112. SheetDescription.displayName = SheetPrimitive.Description.displayName
  113. export {
  114. Sheet,
  115. SheetPortal,
  116. SheetOverlay,
  117. SheetTrigger,
  118. SheetClose,
  119. SheetContent,
  120. SheetHeader,
  121. SheetFooter,
  122. SheetTitle,
  123. SheetDescription,
  124. }