-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathSlide.js
405 lines (362 loc) · 12.1 KB
/
Slide.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
405
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { Transition } from 'react-transition-group';
import chainPropTypes from '@mui/utils/chainPropTypes';
import HTMLElementType from '@mui/utils/HTMLElementType';
import elementAcceptingRef from '@mui/utils/elementAcceptingRef';
import getReactElementRef from '@mui/utils/getReactElementRef';
import debounce from '../utils/debounce';
import useForkRef from '../utils/useForkRef';
import { useTheme } from '../zero-styled';
import { reflow, getTransitionProps } from '../transitions/utils';
import { ownerWindow } from '../utils';
// Translate the node so it can't be seen on the screen.
// Later, we're going to translate the node back to its original location with `none`.
function getTranslateValue(direction, node, resolvedContainer) {
const rect = node.getBoundingClientRect();
const containerRect = resolvedContainer && resolvedContainer.getBoundingClientRect();
const containerWindow = ownerWindow(node);
let transform;
if (node.fakeTransform) {
transform = node.fakeTransform;
} else {
const computedStyle = containerWindow.getComputedStyle(node);
transform =
computedStyle.getPropertyValue('-webkit-transform') ||
computedStyle.getPropertyValue('transform');
}
let offsetX = 0;
let offsetY = 0;
if (transform && transform !== 'none' && typeof transform === 'string') {
const transformValues = transform.split('(')[1].split(')')[0].split(',');
offsetX = parseInt(transformValues[4], 10);
offsetY = parseInt(transformValues[5], 10);
}
if (direction === 'left') {
if (containerRect) {
return `translateX(${containerRect.right + offsetX - rect.left}px)`;
}
return `translateX(${containerWindow.innerWidth + offsetX - rect.left}px)`;
}
if (direction === 'right') {
if (containerRect) {
return `translateX(-${rect.right - containerRect.left - offsetX}px)`;
}
return `translateX(-${rect.left + rect.width - offsetX}px)`;
}
if (direction === 'up') {
if (containerRect) {
return `translateY(${containerRect.bottom + offsetY - rect.top}px)`;
}
return `translateY(${containerWindow.innerHeight + offsetY - rect.top}px)`;
}
// direction === 'down'
if (containerRect) {
return `translateY(-${rect.top - containerRect.top + rect.height - offsetY}px)`;
}
return `translateY(-${rect.top + rect.height - offsetY}px)`;
}
function resolveContainer(containerPropProp) {
return typeof containerPropProp === 'function' ? containerPropProp() : containerPropProp;
}
export function setTranslateValue(direction, node, containerProp) {
const resolvedContainer = resolveContainer(containerProp);
const transform = getTranslateValue(direction, node, resolvedContainer);
if (transform) {
node.style.webkitTransform = transform;
node.style.transform = transform;
}
}
/**
* The Slide transition is used by the [Drawer](/material-ui/react-drawer/) component.
* It uses [react-transition-group](https://github.com/reactjs/react-transition-group) internally.
*/
const Slide = React.forwardRef(function Slide(props, ref) {
const theme = useTheme();
const defaultEasing = {
enter: theme.transitions.easing.easeOut,
exit: theme.transitions.easing.sharp,
};
const defaultTimeout = {
enter: theme.transitions.duration.enteringScreen,
exit: theme.transitions.duration.leavingScreen,
};
const {
addEndListener,
appear = true,
children,
container: containerProp,
direction = 'down',
easing: easingProp = defaultEasing,
in: inProp,
onEnter,
onEntered,
onEntering,
onExit,
onExited,
onExiting,
style,
timeout = defaultTimeout,
// eslint-disable-next-line react/prop-types
TransitionComponent = Transition,
...other
} = props;
const childrenRef = React.useRef(null);
const handleRef = useForkRef(getReactElementRef(children), childrenRef, ref);
const normalizedTransitionCallback = (callback) => (isAppearing) => {
if (callback) {
// onEnterXxx and onExitXxx callbacks have a different arguments.length value.
if (isAppearing === undefined) {
callback(childrenRef.current);
} else {
callback(childrenRef.current, isAppearing);
}
}
};
const handleEnter = normalizedTransitionCallback((node, isAppearing) => {
setTranslateValue(direction, node, containerProp);
reflow(node);
if (onEnter) {
onEnter(node, isAppearing);
}
});
const handleEntering = normalizedTransitionCallback((node, isAppearing) => {
const transitionProps = getTransitionProps(
{ timeout, style, easing: easingProp },
{
mode: 'enter',
},
);
node.style.webkitTransition = theme.transitions.create('-webkit-transform', {
...transitionProps,
});
node.style.transition = theme.transitions.create('transform', {
...transitionProps,
});
node.style.webkitTransform = 'none';
node.style.transform = 'none';
if (onEntering) {
onEntering(node, isAppearing);
}
});
const handleEntered = normalizedTransitionCallback(onEntered);
const handleExiting = normalizedTransitionCallback(onExiting);
const handleExit = normalizedTransitionCallback((node) => {
const transitionProps = getTransitionProps(
{ timeout, style, easing: easingProp },
{
mode: 'exit',
},
);
node.style.webkitTransition = theme.transitions.create('-webkit-transform', transitionProps);
node.style.transition = theme.transitions.create('transform', transitionProps);
setTranslateValue(direction, node, containerProp);
if (onExit) {
onExit(node);
}
});
const handleExited = normalizedTransitionCallback((node) => {
// No need for transitions when the component is hidden
node.style.webkitTransition = '';
node.style.transition = '';
if (onExited) {
onExited(node);
}
});
const handleAddEndListener = (next) => {
if (addEndListener) {
// Old call signature before `react-transition-group` implemented `nodeRef`
addEndListener(childrenRef.current, next);
}
};
const updatePosition = React.useCallback(() => {
if (childrenRef.current) {
setTranslateValue(direction, childrenRef.current, containerProp);
}
}, [direction, containerProp]);
React.useEffect(() => {
// Skip configuration where the position is screen size invariant.
if (inProp || direction === 'down' || direction === 'right') {
return undefined;
}
const handleResize = debounce(() => {
if (childrenRef.current) {
setTranslateValue(direction, childrenRef.current, containerProp);
}
});
const containerWindow = ownerWindow(childrenRef.current);
containerWindow.addEventListener('resize', handleResize);
return () => {
handleResize.clear();
containerWindow.removeEventListener('resize', handleResize);
};
}, [direction, inProp, containerProp]);
React.useEffect(() => {
if (!inProp) {
// We need to update the position of the drawer when the direction change and
// when it's hidden.
updatePosition();
}
}, [inProp, updatePosition]);
return (
<TransitionComponent
nodeRef={childrenRef}
onEnter={handleEnter}
onEntered={handleEntered}
onEntering={handleEntering}
onExit={handleExit}
onExited={handleExited}
onExiting={handleExiting}
addEndListener={handleAddEndListener}
appear={appear}
in={inProp}
timeout={timeout}
{...other}
>
{/* Ensure "ownerState" is not forwarded to the child DOM element when a direct HTML element is used. This avoids unexpected behavior since "ownerState" is intended for internal styling, component props and not as a DOM attribute. */}
{(state, { ownerState, ...restChildProps }) => {
return React.cloneElement(children, {
ref: handleRef,
style: {
visibility: state === 'exited' && !inProp ? 'hidden' : undefined,
...style,
...children.props.style,
},
...restChildProps,
});
}}
</TransitionComponent>
);
});
Slide.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`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* Add a custom transition end trigger. Called with the transitioning DOM
* node and a done callback. Allows for more fine grained transition end
* logic. Note: Timeouts are still used as a fallback if provided.
*/
addEndListener: PropTypes.func,
/**
* Perform the enter transition when it first mounts if `in` is also `true`.
* Set this to `false` to disable this behavior.
* @default true
*/
appear: PropTypes.bool,
/**
* A single child content element.
*/
children: elementAcceptingRef.isRequired,
/**
* An HTML element, or a function that returns one.
* It's used to set the container the Slide is transitioning from.
*/
container: chainPropTypes(PropTypes.oneOfType([HTMLElementType, PropTypes.func]), (props) => {
if (props.open) {
const resolvedContainer = resolveContainer(props.container);
if (resolvedContainer && resolvedContainer.nodeType === 1) {
const box = resolvedContainer.getBoundingClientRect();
if (
process.env.NODE_ENV !== 'test' &&
box.top === 0 &&
box.left === 0 &&
box.right === 0 &&
box.bottom === 0
) {
return new Error(
[
'MUI: The `container` prop provided to the component is invalid.',
'The anchor element should be part of the document layout.',
"Make sure the element is present in the document or that it's not display none.",
].join('\n'),
);
}
} else if (
!resolvedContainer ||
typeof resolvedContainer.getBoundingClientRect !== 'function' ||
(resolvedContainer.contextElement != null &&
resolvedContainer.contextElement.nodeType !== 1)
) {
return new Error(
[
'MUI: The `container` prop provided to the component is invalid.',
'It should be an HTML element instance.',
].join('\n'),
);
}
}
return null;
}),
/**
* Direction the child node will enter from.
* @default 'down'
*/
direction: PropTypes.oneOf(['down', 'left', 'right', 'up']),
/**
* The transition timing function.
* You may specify a single easing or a object containing enter and exit values.
* @default {
* enter: theme.transitions.easing.easeOut,
* exit: theme.transitions.easing.sharp,
* }
*/
easing: PropTypes.oneOfType([
PropTypes.shape({
enter: PropTypes.string,
exit: PropTypes.string,
}),
PropTypes.string,
]),
/**
* If `true`, the component will transition in.
*/
in: PropTypes.bool,
/**
* @ignore
*/
onEnter: PropTypes.func,
/**
* @ignore
*/
onEntered: PropTypes.func,
/**
* @ignore
*/
onEntering: PropTypes.func,
/**
* @ignore
*/
onExit: PropTypes.func,
/**
* @ignore
*/
onExited: PropTypes.func,
/**
* @ignore
*/
onExiting: PropTypes.func,
/**
* @ignore
*/
style: 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,
* }
*/
timeout: PropTypes.oneOfType([
PropTypes.number,
PropTypes.shape({
appear: PropTypes.number,
enter: PropTypes.number,
exit: PropTypes.number,
}),
]),
};
export default Slide;