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