-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawer.tsx
More file actions
149 lines (137 loc) · 4.53 KB
/
drawer.tsx
File metadata and controls
149 lines (137 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import type * as React from 'react'
import { type DialogProps, Drawer as DrawerPrimitive } from 'vaul'
import { headingLevelClasses, textSizeClasses, textVariantClasses } from '../styles/classes'
import type {
DrawerBodyProps,
DrawerContentProps,
DrawerDescriptionProps,
DrawerFooterProps,
DrawerHeaderProps,
DrawerProps,
DrawerTitleProps,
DrawerTriggerProps
} from '../types/drawer'
import { cn } from '../utils'
import { Button } from './button'
import { Container } from './container'
const Drawer: React.FC<DrawerProps & DialogProps> = ({
children,
direction = 'right',
...props
}) => {
return (
<DrawerPrimitive.Root data-slot="drawer" direction={direction} {...props}>
{children}
</DrawerPrimitive.Root>
)
}
const DrawerTrigger: React.FC<DrawerTriggerProps> = ({ children, asChild = false }) => {
return (
<DrawerPrimitive.Trigger
data-slot="drawer-trigger"
asChild={asChild}
className={textSizeClasses.sm}
>
{/* To ensure children are wrapped in one parent is asChild is true */}
{asChild ? <div>{children}</div> : children}
</DrawerPrimitive.Trigger>
)
}
const DrawerPortal = (props: React.ComponentProps<typeof DrawerPrimitive.Portal>) => {
return <DrawerPrimitive.Portal data-slot="drawer-portal" {...props} />
}
const DrawerClose = () => {
return (
<Container className="absolute top-4 right-4" arrangement="row" justify="end">
<DrawerPrimitive.Close data-slot="drawer-close" asChild>
<Button label="Close" arrangement="hiddenLabel" icon="x" size="sm" variant="ghost" />
</DrawerPrimitive.Close>
</Container>
)
}
const DrawerOverlay = ({
className,
...props
}: React.ComponentProps<typeof DrawerPrimitive.Overlay>) => {
return (
<DrawerPrimitive.Overlay
data-slot="drawer-overlay"
className={cn(
'data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-50 bg-black/50 data-[state=closed]:animate-out data-[state=open]:animate-in',
className
)}
{...props}
/>
)
}
const DrawerContent: React.FC<DrawerContentProps> = ({ children }) => {
return (
<DrawerPortal data-slot="drawer-portal">
<DrawerOverlay />
<DrawerPrimitive.Content
data-slot="drawer-content"
className={cn(
'group/drawer-content fixed z-50 flex h-auto flex-col bg-background',
'my-2 rounded-l-lg data-[vaul-drawer-direction=top]:inset-x-0 data-[vaul-drawer-direction=top]:top-0 data-[vaul-drawer-direction=top]:mb-24 data-[vaul-drawer-direction=top]:max-h-[80vh] data-[vaul-drawer-direction=top]:rounded-b-lg data-[vaul-drawer-direction=top]:border-b',
'data-[vaul-drawer-direction=bottom]:inset-x-0 data-[vaul-drawer-direction=bottom]:bottom-0 data-[vaul-drawer-direction=bottom]:mt-24 data-[vaul-drawer-direction=bottom]:max-h-[80vh] data-[vaul-drawer-direction=bottom]:rounded-t-lg data-[vaul-drawer-direction=bottom]:border-t',
'data-[vaul-drawer-direction=right]:inset-y-0 data-[vaul-drawer-direction=right]:right-0 data-[vaul-drawer-direction=right]:w-3/4 data-[vaul-drawer-direction=right]:border-l data-[vaul-drawer-direction=right]:sm:max-w-sm',
'data-[vaul-drawer-direction=left]:inset-y-0 data-[vaul-drawer-direction=left]:left-0 data-[vaul-drawer-direction=left]:w-3/4 data-[vaul-drawer-direction=left]:border-r data-[vaul-drawer-direction=left]:sm:max-w-sm',
'rounded-default border'
)}
>
<Container gap="sm" padding="md">
{children}
</Container>
<DrawerClose />
</DrawerPrimitive.Content>
</DrawerPortal>
)
}
const DrawerHeader: React.FC<DrawerHeaderProps> = ({ children }) => {
return (
<Container data-slot="drawer-header" gap="xs">
{children}
</Container>
)
}
const DrawerFooter: React.FC<DrawerFooterProps> = ({ children }) => {
return (
<Container data-slot="drawer-footer" arrangement="row" justify="end" gap="sm">
{children}
</Container>
)
}
const DrawerTitle: React.FC<DrawerTitleProps> = ({ children }) => {
return (
<DrawerPrimitive.Title data-slot="drawer-title" className={cn(headingLevelClasses[2])}>
{children}
</DrawerPrimitive.Title>
)
}
const DrawerDescription: React.FC<DrawerDescriptionProps> = ({ children }) => {
return (
<DrawerPrimitive.Description
data-slot="drawer-description"
className={cn(textVariantClasses.secondary, textSizeClasses.sm)}
>
{children}
</DrawerPrimitive.Description>
)
}
const DrawerBody: React.FC<DrawerBodyProps> = ({ children }) => {
return (
<Container gap="sm" className={textSizeClasses.sm}>
{children}
</Container>
)
}
export {
Drawer,
DrawerBody,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
DrawerTrigger
}