1 | 'use client';
|
2 |
|
3 | import _extends from "@babel/runtime/helpers/esm/extends";
|
4 | import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
5 | const _excluded = ["anchorEl", "children", "direction", "disablePortal", "modifiers", "open", "placement", "popperOptions", "popperRef", "slotProps", "slots", "TransitionProps", "ownerState"],
|
6 | _excluded2 = ["anchorEl", "children", "container", "direction", "disablePortal", "keepMounted", "modifiers", "open", "placement", "popperOptions", "popperRef", "style", "transition", "slotProps", "slots"];
|
7 | import * as React from 'react';
|
8 | import { chainPropTypes, HTMLElementType, refType, unstable_ownerDocument as ownerDocument, unstable_useEnhancedEffect as useEnhancedEffect, unstable_useForkRef as useForkRef } from '@mui/utils';
|
9 | import { createPopper } from '@popperjs/core';
|
10 | import PropTypes from 'prop-types';
|
11 | import { unstable_composeClasses as composeClasses } from '../composeClasses';
|
12 | import { Portal } from '../Portal';
|
13 | import { getPopperUtilityClass } from './popperClasses';
|
14 | import { useSlotProps } from '../utils';
|
15 | import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
|
16 | import { jsx as _jsx } from "react/jsx-runtime";
|
17 | function flipPlacement(placement, direction) {
|
18 | if (direction === 'ltr') {
|
19 | return placement;
|
20 | }
|
21 | switch (placement) {
|
22 | case 'bottom-end':
|
23 | return 'bottom-start';
|
24 | case 'bottom-start':
|
25 | return 'bottom-end';
|
26 | case 'top-end':
|
27 | return 'top-start';
|
28 | case 'top-start':
|
29 | return 'top-end';
|
30 | default:
|
31 | return placement;
|
32 | }
|
33 | }
|
34 | function resolveAnchorEl(anchorEl) {
|
35 | return typeof anchorEl === 'function' ? anchorEl() : anchorEl;
|
36 | }
|
37 | function isHTMLElement(element) {
|
38 | return element.nodeType !== undefined;
|
39 | }
|
40 | function isVirtualElement(element) {
|
41 | return !isHTMLElement(element);
|
42 | }
|
43 | const useUtilityClasses = () => {
|
44 | const slots = {
|
45 | root: ['root']
|
46 | };
|
47 | return composeClasses(slots, useClassNamesOverride(getPopperUtilityClass));
|
48 | };
|
49 | const defaultPopperOptions = {};
|
50 | const PopperTooltip = React.forwardRef(function PopperTooltip(props, forwardedRef) {
|
51 | const {
|
52 | anchorEl,
|
53 | children,
|
54 | direction,
|
55 | disablePortal,
|
56 | modifiers,
|
57 | open,
|
58 | placement: initialPlacement,
|
59 | popperOptions,
|
60 | popperRef: popperRefProp,
|
61 | slotProps = {},
|
62 | slots = {},
|
63 | TransitionProps
|
64 |
|
65 |
|
66 | } = props,
|
67 | other = _objectWithoutPropertiesLoose(props, _excluded);
|
68 | const tooltipRef = React.useRef(null);
|
69 | const ownRef = useForkRef(tooltipRef, forwardedRef);
|
70 | const popperRef = React.useRef(null);
|
71 | const handlePopperRef = useForkRef(popperRef, popperRefProp);
|
72 | const handlePopperRefRef = React.useRef(handlePopperRef);
|
73 | useEnhancedEffect(() => {
|
74 | handlePopperRefRef.current = handlePopperRef;
|
75 | }, [handlePopperRef]);
|
76 | React.useImperativeHandle(popperRefProp, () => popperRef.current, []);
|
77 | const rtlPlacement = flipPlacement(initialPlacement, direction);
|
78 | |
79 |
|
80 |
|
81 |
|
82 | const [placement, setPlacement] = React.useState(rtlPlacement);
|
83 | const [resolvedAnchorElement, setResolvedAnchorElement] = React.useState(resolveAnchorEl(anchorEl));
|
84 | React.useEffect(() => {
|
85 | if (popperRef.current) {
|
86 | popperRef.current.forceUpdate();
|
87 | }
|
88 | });
|
89 | React.useEffect(() => {
|
90 | if (anchorEl) {
|
91 | setResolvedAnchorElement(resolveAnchorEl(anchorEl));
|
92 | }
|
93 | }, [anchorEl]);
|
94 | useEnhancedEffect(() => {
|
95 | if (!resolvedAnchorElement || !open) {
|
96 | return undefined;
|
97 | }
|
98 | const handlePopperUpdate = data => {
|
99 | setPlacement(data.placement);
|
100 | };
|
101 | if (process.env.NODE_ENV !== 'production') {
|
102 | if (resolvedAnchorElement && isHTMLElement(resolvedAnchorElement) && resolvedAnchorElement.nodeType === 1) {
|
103 | const box = resolvedAnchorElement.getBoundingClientRect();
|
104 | if (process.env.NODE_ENV !== 'test' && box.top === 0 && box.left === 0 && box.right === 0 && box.bottom === 0) {
|
105 | 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'));
|
106 | }
|
107 | }
|
108 | }
|
109 | let popperModifiers = [{
|
110 | name: 'preventOverflow',
|
111 | options: {
|
112 | altBoundary: disablePortal
|
113 | }
|
114 | }, {
|
115 | name: 'flip',
|
116 | options: {
|
117 | altBoundary: disablePortal
|
118 | }
|
119 | }, {
|
120 | name: 'onUpdate',
|
121 | enabled: true,
|
122 | phase: 'afterWrite',
|
123 | fn: ({
|
124 | state
|
125 | }) => {
|
126 | handlePopperUpdate(state);
|
127 | }
|
128 | }];
|
129 | if (modifiers != null) {
|
130 | popperModifiers = popperModifiers.concat(modifiers);
|
131 | }
|
132 | if (popperOptions && popperOptions.modifiers != null) {
|
133 | popperModifiers = popperModifiers.concat(popperOptions.modifiers);
|
134 | }
|
135 | const popper = createPopper(resolvedAnchorElement, tooltipRef.current, _extends({
|
136 | placement: rtlPlacement
|
137 | }, popperOptions, {
|
138 | modifiers: popperModifiers
|
139 | }));
|
140 | handlePopperRefRef.current(popper);
|
141 | return () => {
|
142 | popper.destroy();
|
143 | handlePopperRefRef.current(null);
|
144 | };
|
145 | }, [resolvedAnchorElement, disablePortal, modifiers, open, popperOptions, rtlPlacement]);
|
146 | const childProps = {
|
147 | placement: placement
|
148 | };
|
149 | if (TransitionProps !== null) {
|
150 | childProps.TransitionProps = TransitionProps;
|
151 | }
|
152 | const classes = useUtilityClasses();
|
153 | const Root = slots.root ?? 'div';
|
154 | const rootProps = useSlotProps({
|
155 | elementType: Root,
|
156 | externalSlotProps: slotProps.root,
|
157 | externalForwardedProps: other,
|
158 | additionalProps: {
|
159 | role: 'tooltip',
|
160 | ref: ownRef
|
161 | },
|
162 | ownerState: props,
|
163 | className: classes.root
|
164 | });
|
165 | return _jsx(Root, _extends({}, rootProps, {
|
166 | children: typeof children === 'function' ? children(childProps) : children
|
167 | }));
|
168 | });
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | const Popper = React.forwardRef(function Popper(props, forwardedRef) {
|
182 | const {
|
183 | anchorEl,
|
184 | children,
|
185 | container: containerProp,
|
186 | direction = 'ltr',
|
187 | disablePortal = false,
|
188 | keepMounted = false,
|
189 | modifiers,
|
190 | open,
|
191 | placement = 'bottom',
|
192 | popperOptions = defaultPopperOptions,
|
193 | popperRef,
|
194 | style,
|
195 | transition = false,
|
196 | slotProps = {},
|
197 | slots = {}
|
198 | } = props,
|
199 | other = _objectWithoutPropertiesLoose(props, _excluded2);
|
200 | const [exited, setExited] = React.useState(true);
|
201 | const handleEnter = () => {
|
202 | setExited(false);
|
203 | };
|
204 | const handleExited = () => {
|
205 | setExited(true);
|
206 | };
|
207 | if (!keepMounted && !open && (!transition || exited)) {
|
208 | return null;
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | let container;
|
215 | if (containerProp) {
|
216 | container = containerProp;
|
217 | } else if (anchorEl) {
|
218 | const resolvedAnchorEl = resolveAnchorEl(anchorEl);
|
219 | container = resolvedAnchorEl && isHTMLElement(resolvedAnchorEl) ? ownerDocument(resolvedAnchorEl).body : ownerDocument(null).body;
|
220 | }
|
221 | const display = !open && keepMounted && (!transition || exited) ? 'none' : undefined;
|
222 | const transitionProps = transition ? {
|
223 | in: open,
|
224 | onEnter: handleEnter,
|
225 | onExited: handleExited
|
226 | } : undefined;
|
227 | return _jsx(Portal, {
|
228 | disablePortal: disablePortal,
|
229 | container: container,
|
230 | children: _jsx(PopperTooltip, _extends({
|
231 | anchorEl: anchorEl,
|
232 | direction: direction,
|
233 | disablePortal: disablePortal,
|
234 | modifiers: modifiers,
|
235 | ref: forwardedRef,
|
236 | open: transition ? !exited : open,
|
237 | placement: placement,
|
238 | popperOptions: popperOptions,
|
239 | popperRef: popperRef,
|
240 | slotProps: slotProps,
|
241 | slots: slots
|
242 | }, other, {
|
243 | style: _extends({
|
244 |
|
245 | position: 'fixed',
|
246 |
|
247 | top: 0,
|
248 | left: 0,
|
249 | display
|
250 | }, style),
|
251 | TransitionProps: transitionProps,
|
252 | children: children
|
253 | }))
|
254 | });
|
255 | });
|
256 | process.env.NODE_ENV !== "production" ? Popper.propTypes = {
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | |
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | anchorEl: chainPropTypes(PropTypes.oneOfType([HTMLElementType, PropTypes.object, PropTypes.func]), props => {
|
268 | if (props.open) {
|
269 | const resolvedAnchorEl = resolveAnchorEl(props.anchorEl);
|
270 | if (resolvedAnchorEl && isHTMLElement(resolvedAnchorEl) && resolvedAnchorEl.nodeType === 1) {
|
271 | const box = resolvedAnchorEl.getBoundingClientRect();
|
272 | if (process.env.NODE_ENV !== 'test' && box.top === 0 && box.left === 0 && box.right === 0 && box.bottom === 0) {
|
273 | 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'));
|
274 | }
|
275 | } else if (!resolvedAnchorEl || typeof resolvedAnchorEl.getBoundingClientRect !== 'function' || isVirtualElement(resolvedAnchorEl) && resolvedAnchorEl.contextElement != null && resolvedAnchorEl.contextElement.nodeType !== 1) {
|
276 | 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'));
|
277 | }
|
278 | }
|
279 | return null;
|
280 | }),
|
281 | |
282 |
|
283 |
|
284 | children: PropTypes .oneOfType([PropTypes.node, PropTypes.func]),
|
285 | |
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 | container: PropTypes .oneOfType([HTMLElementType, PropTypes.func]),
|
296 | |
297 |
|
298 |
|
299 |
|
300 | direction: PropTypes.oneOf(['ltr', 'rtl']),
|
301 | |
302 |
|
303 |
|
304 |
|
305 | disablePortal: PropTypes.bool,
|
306 | |
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | keepMounted: PropTypes.bool,
|
313 | |
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | modifiers: PropTypes.arrayOf(PropTypes.shape({
|
323 | data: PropTypes.object,
|
324 | effect: PropTypes.func,
|
325 | enabled: PropTypes.bool,
|
326 | fn: PropTypes.func,
|
327 | name: PropTypes.any,
|
328 | options: PropTypes.object,
|
329 | phase: PropTypes.oneOf(['afterMain', 'afterRead', 'afterWrite', 'beforeMain', 'beforeRead', 'beforeWrite', 'main', 'read', 'write']),
|
330 | requires: PropTypes.arrayOf(PropTypes.string),
|
331 | requiresIfExists: PropTypes.arrayOf(PropTypes.string)
|
332 | })),
|
333 | |
334 |
|
335 |
|
336 | open: PropTypes.bool.isRequired,
|
337 | |
338 |
|
339 |
|
340 |
|
341 | 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']),
|
342 | |
343 |
|
344 |
|
345 |
|
346 | popperOptions: PropTypes.shape({
|
347 | modifiers: PropTypes.array,
|
348 | onFirstUpdate: PropTypes.func,
|
349 | 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']),
|
350 | strategy: PropTypes.oneOf(['absolute', 'fixed'])
|
351 | }),
|
352 | |
353 |
|
354 |
|
355 | popperRef: refType,
|
356 | |
357 |
|
358 |
|
359 |
|
360 | slotProps: PropTypes.shape({
|
361 | root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
|
362 | }),
|
363 | |
364 |
|
365 |
|
366 |
|
367 |
|
368 | slots: PropTypes.shape({
|
369 | root: PropTypes.elementType
|
370 | }),
|
371 | |
372 |
|
373 |
|
374 |
|
375 | transition: PropTypes.bool
|
376 | } : void 0;
|
377 | export { Popper }; |
\ | No newline at end of file |