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