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