forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PopperUnstyled.js
489 lines (452 loc) · 13.1 KB
/
PopperUnstyled.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import * as React from 'react';
import {
chainPropTypes,
HTMLElementType,
refType,
unstable_ownerDocument as ownerDocument,
unstable_useEnhancedEffect as useEnhancedEffect,
unstable_useForkRef as useForkRef,
} from '@mui/utils';
import { createPopper } from '@popperjs/core';
import PropTypes from 'prop-types';
import composeClasses from '../composeClasses';
import Portal from '../Portal';
import { getPopperUnstyledUtilityClass } from './popperUnstyledClasses';
import { useSlotProps } from '../utils';
function flipPlacement(placement, direction) {
if (direction === 'ltr') {
return placement;
}
switch (placement) {
case 'bottom-end':
return 'bottom-start';
case 'bottom-start':
return 'bottom-end';
case 'top-end':
return 'top-start';
case 'top-start':
return 'top-end';
default:
return placement;
}
}
function resolveAnchorEl(anchorEl) {
return typeof anchorEl === 'function' ? anchorEl() : anchorEl;
}
const useUtilityClasses = () => {
const slots = {
root: ['root'],
};
return composeClasses(slots, getPopperUnstyledUtilityClass, {});
};
const defaultPopperOptions = {};
/* eslint-disable react/prop-types */
const PopperTooltip = React.forwardRef(function PopperTooltip(props, ref) {
const {
anchorEl,
children,
component,
direction,
disablePortal,
modifiers,
open,
ownerState,
placement: initialPlacement,
popperOptions,
popperRef: popperRefProp,
slotProps = {},
slots = {},
TransitionProps,
...other
} = props;
const tooltipRef = React.useRef(null);
const ownRef = useForkRef(tooltipRef, ref);
const popperRef = React.useRef(null);
const handlePopperRef = useForkRef(popperRef, popperRefProp);
const handlePopperRefRef = React.useRef(handlePopperRef);
useEnhancedEffect(() => {
handlePopperRefRef.current = handlePopperRef;
}, [handlePopperRef]);
React.useImperativeHandle(popperRefProp, () => popperRef.current, []);
const rtlPlacement = flipPlacement(initialPlacement, direction);
/**
* placement initialized from prop but can change during lifetime if modifiers.flip.
* modifiers.flip is essentially a flip for controlled/uncontrolled behavior
*/
const [placement, setPlacement] = React.useState(rtlPlacement);
const [resolvedAnchorElement, setResolvedAnchorElement] = React.useState(
resolveAnchorEl(anchorEl),
);
React.useEffect(() => {
if (popperRef.current) {
popperRef.current.forceUpdate();
}
});
React.useEffect(() => {
if (anchorEl) {
setResolvedAnchorElement(resolveAnchorEl(anchorEl));
}
}, [anchorEl]);
useEnhancedEffect(() => {
if (!resolvedAnchorElement || !open) {
return undefined;
}
const handlePopperUpdate = (data) => {
setPlacement(data.placement);
};
if (process.env.NODE_ENV !== 'production') {
if (resolvedAnchorElement && resolvedAnchorElement.nodeType === 1) {
const box = resolvedAnchorElement.getBoundingClientRect();
if (
process.env.NODE_ENV !== 'test' &&
box.top === 0 &&
box.left === 0 &&
box.right === 0 &&
box.bottom === 0
) {
console.warn(
[
'MUI: The `anchorEl` 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'),
);
}
}
}
let popperModifiers = [
{
name: 'preventOverflow',
options: {
altBoundary: disablePortal,
},
},
{
name: 'flip',
options: {
altBoundary: disablePortal,
},
},
{
name: 'onUpdate',
enabled: true,
phase: 'afterWrite',
fn: ({ state }) => {
handlePopperUpdate(state);
},
},
];
if (modifiers != null) {
popperModifiers = popperModifiers.concat(modifiers);
}
if (popperOptions && popperOptions.modifiers != null) {
popperModifiers = popperModifiers.concat(popperOptions.modifiers);
}
const popper = createPopper(resolvedAnchorElement, tooltipRef.current, {
placement: rtlPlacement,
...popperOptions,
modifiers: popperModifiers,
});
handlePopperRefRef.current(popper);
return () => {
popper.destroy();
handlePopperRefRef.current(null);
};
}, [resolvedAnchorElement, disablePortal, modifiers, open, popperOptions, rtlPlacement]);
const childProps = { placement };
if (TransitionProps !== null) {
childProps.TransitionProps = TransitionProps;
}
const classes = useUtilityClasses();
const Root = component ?? slots.root ?? 'div';
const rootProps = useSlotProps({
elementType: Root,
externalSlotProps: slotProps.root,
externalForwardedProps: other,
additionalProps: {
role: 'tooltip',
ref: ownRef,
},
ownerState: {
// shallow merge ownerState from external component, e.g. Joy Menu.
...props,
...ownerState,
},
className: classes.root,
});
return (
<Root {...rootProps}>{typeof children === 'function' ? children(childProps) : children}</Root>
);
});
/* eslint-enable react/prop-types */
/**
* Poppers rely on the 3rd party library [Popper.js](https://popper.js.org/docs/v2/) for positioning.
*/
const PopperUnstyled = React.forwardRef(function PopperUnstyled(props, ref) {
const {
anchorEl,
children,
container: containerProp,
direction = 'ltr',
disablePortal = false,
keepMounted = false,
modifiers,
open,
placement = 'bottom',
popperOptions = defaultPopperOptions,
popperRef,
style,
transition = false,
...other
} = props;
const [exited, setExited] = React.useState(true);
const handleEnter = () => {
setExited(false);
};
const handleExited = () => {
setExited(true);
};
if (!keepMounted && !open && (!transition || exited)) {
return null;
}
// If the container prop is provided, use that
// If the anchorEl prop is provided, use its parent body element as the container
// If neither are provided let the Modal take care of choosing the container
const container =
containerProp || (anchorEl ? ownerDocument(resolveAnchorEl(anchorEl)).body : undefined);
return (
<Portal disablePortal={disablePortal} container={container}>
<PopperTooltip
anchorEl={anchorEl}
direction={direction}
disablePortal={disablePortal}
modifiers={modifiers}
ref={ref}
open={transition ? !exited : open}
placement={placement}
popperOptions={popperOptions}
popperRef={popperRef}
{...other}
style={{
// Prevents scroll issue, waiting for Popper.js to add this style once initiated.
position: 'fixed',
// Fix Popper.js display issue
top: 0,
left: 0,
display: !open && keepMounted && (!transition || exited) ? 'none' : null,
...style,
}}
TransitionProps={
transition
? {
in: open,
onEnter: handleEnter,
onExited: handleExited,
}
: null
}
>
{children}
</PopperTooltip>
</Portal>
);
});
PopperUnstyled.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* An HTML element, [virtualElement](https://popper.js.org/docs/v2/virtual-elements/),
* or a function that returns either.
* It's used to set the position of the popper.
* The return value will passed as the reference object of the Popper instance.
*/
anchorEl: chainPropTypes(
PropTypes.oneOfType([HTMLElementType, PropTypes.object, PropTypes.func]),
(props) => {
if (props.open) {
const resolvedAnchorEl = resolveAnchorEl(props.anchorEl);
if (resolvedAnchorEl && resolvedAnchorEl.nodeType === 1) {
const box = resolvedAnchorEl.getBoundingClientRect();
if (
process.env.NODE_ENV !== 'test' &&
box.top === 0 &&
box.left === 0 &&
box.right === 0 &&
box.bottom === 0
) {
return new Error(
[
'MUI: The `anchorEl` 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 (
!resolvedAnchorEl ||
typeof resolvedAnchorEl.getBoundingClientRect !== 'function' ||
(resolvedAnchorEl.contextElement != null &&
resolvedAnchorEl.contextElement.nodeType !== 1)
) {
return new Error(
[
'MUI: The `anchorEl` prop provided to the component is invalid.',
'It should be an HTML element instance or a virtualElement ',
'(https://popper.js.org/docs/v2/virtual-elements/).',
].join('\n'),
);
}
}
return null;
},
),
/**
* Popper render function or node.
*/
children: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.node,
PropTypes.func,
]),
/**
* An HTML element or function that returns one.
* The `container` will have the portal children appended to it.
*
* By default, it uses the body of the top-level document object,
* so it's simply `document.body` most of the time.
*/
container: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
HTMLElementType,
PropTypes.func,
]),
/**
* Direction of the text.
* @default 'ltr'
*/
direction: PropTypes.oneOf(['ltr', 'rtl']),
/**
* The `children` will be under the DOM hierarchy of the parent component.
* @default false
*/
disablePortal: PropTypes.bool,
/**
* Always keep the children in the DOM.
* This prop can be useful in SEO situation or
* when you want to maximize the responsiveness of the Popper.
* @default false
*/
keepMounted: PropTypes.bool,
/**
* Popper.js is based on a "plugin-like" architecture,
* most of its features are fully encapsulated "modifiers".
*
* A modifier is a function that is called each time Popper.js needs to
* compute the position of the popper.
* For this reason, modifiers should be very performant to avoid bottlenecks.
* To learn how to create a modifier, [read the modifiers documentation](https://popper.js.org/docs/v2/modifiers/).
*/
modifiers: PropTypes.arrayOf(
PropTypes.shape({
data: PropTypes.object,
effect: PropTypes.func,
enabled: PropTypes.bool,
fn: PropTypes.func,
name: PropTypes.any,
options: PropTypes.object,
phase: PropTypes.oneOf([
'afterMain',
'afterRead',
'afterWrite',
'beforeMain',
'beforeRead',
'beforeWrite',
'main',
'read',
'write',
]),
requires: PropTypes.arrayOf(PropTypes.string),
requiresIfExists: PropTypes.arrayOf(PropTypes.string),
}),
),
/**
* If `true`, the component is shown.
*/
open: PropTypes.bool.isRequired,
/**
* Popper placement.
* @default 'bottom'
*/
placement: PropTypes.oneOf([
'auto-end',
'auto-start',
'auto',
'bottom-end',
'bottom-start',
'bottom',
'left-end',
'left-start',
'left',
'right-end',
'right-start',
'right',
'top-end',
'top-start',
'top',
]),
/**
* Options provided to the [`Popper.js`](https://popper.js.org/docs/v2/constructors/#options) instance.
* @default {}
*/
popperOptions: PropTypes.shape({
modifiers: PropTypes.array,
onFirstUpdate: PropTypes.func,
placement: PropTypes.oneOf([
'auto-end',
'auto-start',
'auto',
'bottom-end',
'bottom-start',
'bottom',
'left-end',
'left-start',
'left',
'right-end',
'right-start',
'right',
'top-end',
'top-start',
'top',
]),
strategy: PropTypes.oneOf(['absolute', 'fixed']),
}),
/**
* A ref that points to the used popper instance.
*/
popperRef: refType,
/**
* The props used for each slot inside the Popper.
* @default {}
*/
slotProps: PropTypes.shape({
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
}),
/**
* The components used for each slot inside the Popper.
* Either a string to use a HTML element or a component.
* @default {}
*/
slots: PropTypes.shape({
root: PropTypes.elementType,
}),
/**
* @ignore
*/
style: PropTypes.object,
/**
* Help supporting a react-transition-group/Transition component.
* @default false
*/
transition: PropTypes.bool,
};
export default PopperUnstyled;