forked from mantinedev/mantine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drawer.tsx
278 lines (236 loc) · 7.86 KB
/
Drawer.tsx
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import React, { useEffect } from 'react';
import { useScrollLock, useFocusTrap, useFocusReturn } from '@mantine/hooks';
import {
DefaultProps,
MantineNumberSize,
MantineShadow,
Selectors,
MantineStyleSystemSize,
getDefaultZIndex,
useComponentDefaultProps,
} from '@mantine/styles';
import { Paper } from '../Paper';
import { Overlay } from '../Overlay';
import { OptionalPortal } from '../Portal';
import { Text } from '../Text';
import { Box } from '../Box';
import { CloseButton } from '../CloseButton';
import { GroupedTransition, MantineTransition } from '../Transition';
import useStyles, { DrawerPosition } from './Drawer.styles';
export type DrawerStylesNames = Exclude<Selectors<typeof useStyles>, 'withOverlay'>;
export interface DrawerProps
extends Omit<DefaultProps<DrawerStylesNames>, MantineStyleSystemSize>,
Omit<React.ComponentPropsWithoutRef<'div'>, 'title'> {
/** If true drawer is mounted to the dom */
opened: boolean;
/** Called when drawer is closed (Escape key and click outside, depending on options) */
onClose(): void;
/** Drawer body position */
position?: DrawerPosition;
/** Drawer body width (right | left position) or height (top | bottom position), cannot exceed 100vh for height and 100% for width */
size?: string | number;
/** Drawer body shadow from theme or any css shadow value */
shadow?: MantineShadow;
/** Drawer body padding from theme or number for padding in px */
padding?: MantineNumberSize;
/** Drawer z-index property */
zIndex?: React.CSSProperties['zIndex'];
/** Disables focus trap */
trapFocus?: boolean;
/** Disables scroll lock */
lockScroll?: boolean;
/** Disable onMouseDown trigger for outside events */
closeOnClickOutside?: boolean;
/** Disable onKeyDownCapture trigger for escape key press */
closeOnEscape?: boolean;
/** Drawer appear and disappear transition, see Transition component for full documentation */
transition?: MantineTransition;
/** Transition duration in ms */
transitionDuration?: number;
/** Drawer transitionTimingFunction css property */
transitionTimingFunction?: string;
/** Removes overlay entirely */
withOverlay?: boolean;
/** Overlay opacity, number from 0 to 1 */
overlayOpacity?: number;
/** Overlay color, for example, #000 */
overlayColor?: string;
/** Overlay blur in px */
overlayBlur?: number;
/** Drawer title, displayed in header before close button */
title?: React.ReactNode;
/** Hides close button if set to false, drawer still can be closed with escape key and by clicking outside */
withCloseButton?: boolean;
/** Close button aria-label */
closeButtonLabel?: string;
/** Target element or selector where drawer portal should be rendered */
target?: HTMLElement | string;
/** Determines whether drawer should be rendered within Portal, defaults to true */
withinPortal?: boolean;
/** Determines whether focus should be returned to the last active element when drawer is closed */
withFocusReturn?: boolean;
}
const transitions: Record<DrawerPosition, MantineTransition> = {
top: 'slide-down',
bottom: 'slide-up',
left: 'slide-right',
right: 'slide-left',
};
const rtlTransitions: Record<DrawerPosition, MantineTransition> = {
top: 'slide-down',
bottom: 'slide-up',
right: 'slide-right',
left: 'slide-left',
};
const defaultProps: Partial<DrawerProps> = {
position: 'left',
size: 'md',
transitionDuration: 250,
transitionTimingFunction: 'ease',
zIndex: getDefaultZIndex('modal'),
shadow: 'md',
padding: 0,
lockScroll: true,
closeOnClickOutside: true,
closeOnEscape: true,
trapFocus: true,
withOverlay: true,
withCloseButton: true,
withinPortal: true,
withFocusReturn: true,
overlayBlur: 0,
};
export function Drawer(props: DrawerProps) {
const {
className,
opened,
onClose,
position,
size,
trapFocus,
lockScroll,
closeOnClickOutside,
closeOnEscape,
transition,
transitionDuration,
transitionTimingFunction,
zIndex,
overlayColor,
overlayOpacity,
children,
withOverlay,
shadow,
padding,
title,
withCloseButton,
closeButtonLabel,
classNames,
styles,
target,
withinPortal,
overlayBlur,
unstyled,
withFocusReturn,
...others
} = useComponentDefaultProps('Drawer', defaultProps, props);
const { classes, cx, theme } = useStyles(
{ size, position, zIndex, withOverlay },
{ classNames, styles, unstyled, name: 'Drawer' }
);
const focusTrapRef = useFocusTrap(trapFocus && opened);
const [, _lockScroll] = useScrollLock();
const drawerTransition =
transition || (theme.dir === 'rtl' ? rtlTransitions : transitions)[position];
const _overlayOpacity =
typeof overlayOpacity === 'number'
? overlayOpacity
: theme.colorScheme === 'dark'
? 0.85
: 0.75;
const _closeOnEscape = (event: KeyboardEvent) => {
if (event.key === 'Escape' && closeOnEscape) {
onClose();
}
};
useEffect(() => {
if (!trapFocus) {
window.addEventListener('keydown', _closeOnEscape);
return () => window.removeEventListener('keydown', _closeOnEscape);
}
return undefined;
}, [trapFocus]);
useFocusReturn({ opened, shouldReturnFocus: trapFocus && withFocusReturn });
return (
<OptionalPortal withinPortal={withinPortal} target={target}>
<GroupedTransition
onExited={() => _lockScroll(false)}
onEntered={() => _lockScroll(lockScroll && true)}
mounted={opened}
timingFunction={transitionTimingFunction}
transitions={{
overlay: { duration: transitionDuration / 2, transition: 'fade', timingFunction: 'ease' },
drawer: {
duration: transitionDuration,
transition: drawerTransition,
timingFunction: transitionTimingFunction,
},
}}
>
{(transitionStyles) => (
<Box className={cx(classes.root, className)} role="dialog" aria-modal {...others}>
<Paper<'div'>
className={cx(classes.drawer, className)}
ref={focusTrapRef}
style={transitionStyles.drawer}
radius={0}
tabIndex={-1}
onKeyDownCapture={(event) => {
const shouldTrigger =
(event.target as any)?.getAttribute('data-mantine-stop-propagation') !== 'true';
shouldTrigger && event.key === 'Escape' && closeOnEscape && onClose();
}}
shadow={shadow}
p={padding}
unstyled={unstyled}
>
{(title || withCloseButton) && (
<div className={classes.header}>
<Text className={classes.title} unstyled={unstyled}>
{title}
</Text>
{withCloseButton && (
<CloseButton
iconSize={16}
onClick={onClose}
aria-label={closeButtonLabel}
className={classes.closeButton}
unstyled={unstyled}
/>
)}
</div>
)}
{children}
</Paper>
{withOverlay && (
<div style={transitionStyles.overlay}>
<Overlay
unstyled={unstyled}
blur={overlayBlur}
onMouseDown={() => closeOnClickOutside && onClose()}
className={classes.overlay}
opacity={_overlayOpacity}
zIndex={0}
color={
overlayColor ||
(theme.colorScheme === 'dark' ? theme.colors.dark[9] : theme.black)
}
/>
</div>
)}
</Box>
)}
</GroupedTransition>
</OptionalPortal>
);
}
Drawer.displayName = '@mantine/core/Drawer';