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