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