sheet.tsx 4.2 KB

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