-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathDrawer.js
404 lines (379 loc) · 10.5 KB
/
Drawer.js
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import integerPropType from '@mui/utils/integerPropType';
import composeClasses from '@mui/utils/composeClasses';
import { useRtl } from '@mui/system/RtlProvider';
import Modal from '../Modal';
import Slide from '../Slide';
import Paper from '../Paper';
import capitalize from '../utils/capitalize';
import rootShouldForwardProp from '../styles/rootShouldForwardProp';
import { styled, useTheme } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import { getDrawerUtilityClass } from './drawerClasses';
const overridesResolver = (props, styles) => {
const { ownerState } = props;
return [
styles.root,
(ownerState.variant === 'permanent' || ownerState.variant === 'persistent') && styles.docked,
styles.modal,
];
};
const useUtilityClasses = (ownerState) => {
const { classes, anchor, variant } = ownerState;
const slots = {
root: ['root', `anchor${capitalize(anchor)}`],
docked: [(variant === 'permanent' || variant === 'persistent') && 'docked'],
modal: ['modal'],
paper: [
'paper',
`paperAnchor${capitalize(anchor)}`,
variant !== 'temporary' && `paperAnchorDocked${capitalize(anchor)}`,
],
};
return composeClasses(slots, getDrawerUtilityClass, classes);
};
const DrawerRoot = styled(Modal, {
name: 'MuiDrawer',
slot: 'Root',
overridesResolver,
})(
memoTheme(({ theme }) => ({
zIndex: (theme.vars || theme).zIndex.drawer,
})),
);
const DrawerDockedRoot = styled('div', {
shouldForwardProp: rootShouldForwardProp,
name: 'MuiDrawer',
slot: 'Docked',
skipVariantsResolver: false,
overridesResolver,
})({
flex: '0 0 auto',
});
const DrawerPaper = styled(Paper, {
name: 'MuiDrawer',
slot: 'Paper',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
styles.paper,
styles[`paperAnchor${capitalize(ownerState.anchor)}`],
ownerState.variant !== 'temporary' &&
styles[`paperAnchorDocked${capitalize(ownerState.anchor)}`],
];
},
})(
memoTheme(({ theme }) => ({
overflowY: 'auto',
display: 'flex',
flexDirection: 'column',
height: '100%',
flex: '1 0 auto',
zIndex: (theme.vars || theme).zIndex.drawer,
// Add iOS momentum scrolling for iOS < 13.0
WebkitOverflowScrolling: 'touch',
// temporary style
position: 'fixed',
top: 0,
// We disable the focus ring for mouse, touch and keyboard users.
// At some point, it would be better to keep it for keyboard users.
// :focus-ring CSS pseudo-class will help.
outline: 0,
variants: [
{
props: {
anchor: 'left',
},
style: {
left: 0,
},
},
{
props: {
anchor: 'top',
},
style: {
top: 0,
left: 0,
right: 0,
height: 'auto',
maxHeight: '100%',
},
},
{
props: {
anchor: 'right',
},
style: {
right: 0,
},
},
{
props: {
anchor: 'bottom',
},
style: {
top: 'auto',
left: 0,
bottom: 0,
right: 0,
height: 'auto',
maxHeight: '100%',
},
},
{
props: ({ ownerState }) =>
ownerState.anchor === 'left' && ownerState.variant !== 'temporary',
style: {
borderRight: `1px solid ${(theme.vars || theme).palette.divider}`,
},
},
{
props: ({ ownerState }) =>
ownerState.anchor === 'top' && ownerState.variant !== 'temporary',
style: {
borderBottom: `1px solid ${(theme.vars || theme).palette.divider}`,
},
},
{
props: ({ ownerState }) =>
ownerState.anchor === 'right' && ownerState.variant !== 'temporary',
style: {
borderLeft: `1px solid ${(theme.vars || theme).palette.divider}`,
},
},
{
props: ({ ownerState }) =>
ownerState.anchor === 'bottom' && ownerState.variant !== 'temporary',
style: {
borderTop: `1px solid ${(theme.vars || theme).palette.divider}`,
},
},
],
})),
);
const oppositeDirection = {
left: 'right',
right: 'left',
top: 'down',
bottom: 'up',
};
export function isHorizontal(anchor) {
return ['left', 'right'].includes(anchor);
}
export function getAnchor({ direction }, anchor) {
return direction === 'rtl' && isHorizontal(anchor) ? oppositeDirection[anchor] : anchor;
}
/**
* The props of the [Modal](/material-ui/api/modal/) component are available
* when `variant="temporary"` is set.
*/
const Drawer = React.forwardRef(function Drawer(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiDrawer' });
const theme = useTheme();
const isRtl = useRtl();
const defaultTransitionDuration = {
enter: theme.transitions.duration.enteringScreen,
exit: theme.transitions.duration.leavingScreen,
};
const {
anchor: anchorProp = 'left',
BackdropProps,
children,
className,
elevation = 16,
hideBackdrop = false,
ModalProps: { BackdropProps: BackdropPropsProp, ...ModalProps } = {},
onClose,
open = false,
PaperProps = {},
SlideProps,
// eslint-disable-next-line react/prop-types
TransitionComponent = Slide,
transitionDuration = defaultTransitionDuration,
variant = 'temporary',
...other
} = props;
// Let's assume that the Drawer will always be rendered on user space.
// We use this state is order to skip the appear transition during the
// initial mount of the component.
const mounted = React.useRef(false);
React.useEffect(() => {
mounted.current = true;
}, []);
const anchorInvariant = getAnchor({ direction: isRtl ? 'rtl' : 'ltr' }, anchorProp);
const anchor = anchorProp;
const ownerState = {
...props,
anchor,
elevation,
open,
variant,
...other,
};
const classes = useUtilityClasses(ownerState);
const drawer = (
<DrawerPaper
elevation={variant === 'temporary' ? elevation : 0}
square
{...PaperProps}
className={clsx(classes.paper, PaperProps.className)}
ownerState={ownerState}
>
{children}
</DrawerPaper>
);
if (variant === 'permanent') {
return (
<DrawerDockedRoot
className={clsx(classes.root, classes.docked, className)}
ownerState={ownerState}
ref={ref}
{...other}
>
{drawer}
</DrawerDockedRoot>
);
}
const slidingDrawer = (
<TransitionComponent
in={open}
direction={oppositeDirection[anchorInvariant]}
timeout={transitionDuration}
appear={mounted.current}
{...SlideProps}
>
{drawer}
</TransitionComponent>
);
if (variant === 'persistent') {
return (
<DrawerDockedRoot
className={clsx(classes.root, classes.docked, className)}
ownerState={ownerState}
ref={ref}
{...other}
>
{slidingDrawer}
</DrawerDockedRoot>
);
}
// variant === temporary
return (
<DrawerRoot
BackdropProps={{
...BackdropProps,
...BackdropPropsProp,
transitionDuration,
}}
className={clsx(classes.root, classes.modal, className)}
open={open}
ownerState={ownerState}
onClose={onClose}
hideBackdrop={hideBackdrop}
ref={ref}
{...other}
{...ModalProps}
>
{slidingDrawer}
</DrawerRoot>
);
});
Drawer.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* Side from which the drawer will appear.
* @default 'left'
*/
anchor: PropTypes.oneOf(['bottom', 'left', 'right', 'top']),
/**
* @ignore
*/
BackdropProps: PropTypes.object,
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The elevation of the drawer.
* @default 16
*/
elevation: integerPropType,
/**
* If `true`, the backdrop is not rendered.
* @default false
*/
hideBackdrop: PropTypes.bool,
/**
* Props applied to the [`Modal`](https://mui.com/material-ui/api/modal/) element.
* @default {}
*/
ModalProps: PropTypes.object,
/**
* Callback fired when the component requests to be closed.
* The `reason` parameter can optionally be used to control the response to `onClose`.
*
* @param {object} event The event source of the callback.
* @param {string} reason Can be: `"escapeKeyDown"`, `"backdropClick"`.
*/
onClose: PropTypes.func,
/**
* If `true`, the component is shown.
* @default false
*/
open: PropTypes.bool,
/**
* Props applied to the [`Paper`](https://mui.com/material-ui/api/paper/) element.
* @default {}
*/
PaperProps: PropTypes.object,
/**
* Props applied to the [`Slide`](https://mui.com/material-ui/api/slide/) element.
*/
SlideProps: PropTypes.object,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* The duration for the transition, in milliseconds.
* You may specify a single timeout for all transitions, or individually with an object.
* @default {
* enter: theme.transitions.duration.enteringScreen,
* exit: theme.transitions.duration.leavingScreen,
* }
*/
transitionDuration: PropTypes.oneOfType([
PropTypes.number,
PropTypes.shape({
appear: PropTypes.number,
enter: PropTypes.number,
exit: PropTypes.number,
}),
]),
/**
* The variant to use.
* @default 'temporary'
*/
variant: PropTypes.oneOf(['permanent', 'persistent', 'temporary']),
};
export default Drawer;