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 = ["anchor", "children", "container", "disablePortal", "keepMounted", "middleware", "offset", "open", "placement", "slotProps", "slots", "strategy"];
|
6 | import * as React from 'react';
|
7 | import PropTypes from 'prop-types';
|
8 | import { autoUpdate, flip, offset, shift, useFloating } from '@floating-ui/react-dom';
|
9 | import { HTMLElementType, unstable_useEnhancedEffect as useEnhancedEffect, unstable_useForkRef as useForkRef } from '@mui/utils';
|
10 | import { unstable_composeClasses as composeClasses } from '../composeClasses';
|
11 | import { Portal } from '../Portal';
|
12 | import { useSlotProps } from '../utils';
|
13 | import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
|
14 | import { getPopupUtilityClass } from './popupClasses';
|
15 | import { useTransitionTrigger, TransitionContext } from '../useTransition';
|
16 | import { PopupContext } from './PopupContext';
|
17 | import { jsx as _jsx } from "react/jsx-runtime";
|
18 | function useUtilityClasses(ownerState) {
|
19 | const {
|
20 | open
|
21 | } = ownerState;
|
22 | const slots = {
|
23 | root: ['root', open && 'open']
|
24 | };
|
25 | return composeClasses(slots, useClassNamesOverride(getPopupUtilityClass));
|
26 | }
|
27 | function resolveAnchor(anchor) {
|
28 | return typeof anchor === 'function' ? anchor() : anchor;
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | const Popup = React.forwardRef(function Popup(props, forwardedRef) {
|
42 | var _slots$root;
|
43 | const {
|
44 | anchor: anchorProp,
|
45 | children,
|
46 | container,
|
47 | disablePortal = false,
|
48 | keepMounted = false,
|
49 | middleware,
|
50 | offset: offsetProp = 0,
|
51 | open = false,
|
52 | placement = 'bottom',
|
53 | slotProps = {},
|
54 | slots = {},
|
55 | strategy = 'absolute'
|
56 | } = props,
|
57 | other = _objectWithoutPropertiesLoose(props, _excluded);
|
58 | const {
|
59 | refs,
|
60 | elements,
|
61 | floatingStyles,
|
62 | update,
|
63 | placement: finalPlacement
|
64 | } = useFloating({
|
65 | elements: {
|
66 | reference: resolveAnchor(anchorProp)
|
67 | },
|
68 | open,
|
69 | middleware: middleware != null ? middleware : [offset(offsetProp != null ? offsetProp : 0), flip(), shift()],
|
70 | placement,
|
71 | strategy,
|
72 | whileElementsMounted: !keepMounted ? autoUpdate : undefined
|
73 | });
|
74 | const handleRef = useForkRef(refs.setFloating, forwardedRef);
|
75 | useEnhancedEffect(() => {
|
76 | if (keepMounted && open && elements.reference && elements.floating) {
|
77 | const cleanup = autoUpdate(elements.reference, elements.floating, update);
|
78 | return cleanup;
|
79 | }
|
80 | return undefined;
|
81 | }, [keepMounted, open, elements, update]);
|
82 | const ownerState = _extends({}, props, {
|
83 | disablePortal,
|
84 | keepMounted,
|
85 | offset,
|
86 | open,
|
87 | placement,
|
88 | finalPlacement,
|
89 | strategy
|
90 | });
|
91 | const {
|
92 | contextValue,
|
93 | hasExited: hasTransitionExited
|
94 | } = useTransitionTrigger(open);
|
95 | const visibility = keepMounted && hasTransitionExited ? 'hidden' : undefined;
|
96 | const classes = useUtilityClasses(ownerState);
|
97 | const Root = (_slots$root = slots == null ? void 0 : slots.root) != null ? _slots$root : 'div';
|
98 | const rootProps = useSlotProps({
|
99 | elementType: Root,
|
100 | externalSlotProps: slotProps.root,
|
101 | externalForwardedProps: other,
|
102 | ownerState,
|
103 | className: classes.root,
|
104 | additionalProps: {
|
105 | ref: handleRef,
|
106 | role: 'tooltip',
|
107 | style: _extends({}, floatingStyles, {
|
108 | visibility
|
109 | })
|
110 | }
|
111 | });
|
112 | const popupContextValue = React.useMemo(() => ({
|
113 | placement: finalPlacement
|
114 | }), [finalPlacement]);
|
115 | const shouldRender = keepMounted || !hasTransitionExited;
|
116 | if (!shouldRender) {
|
117 | return null;
|
118 | }
|
119 | return _jsx(Portal, {
|
120 | disablePortal: disablePortal,
|
121 | container: container,
|
122 | children: _jsx(PopupContext.Provider, {
|
123 | value: popupContextValue,
|
124 | children: _jsx(TransitionContext.Provider, {
|
125 | value: contextValue,
|
126 | children: _jsx(Root, _extends({}, rootProps, {
|
127 | children: children
|
128 | }))
|
129 | })
|
130 | })
|
131 | });
|
132 | });
|
133 | process.env.NODE_ENV !== "production" ? Popup.propTypes = {
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 | anchor: PropTypes .oneOfType([HTMLElementType, PropTypes.object, PropTypes.func]),
|
144 | |
145 |
|
146 |
|
147 | children: PropTypes .oneOfType([PropTypes.node, PropTypes.func]),
|
148 | |
149 |
|
150 |
|
151 |
|
152 | container: PropTypes .oneOfType([HTMLElementType, PropTypes.func]),
|
153 | |
154 |
|
155 |
|
156 |
|
157 | disablePortal: PropTypes.bool,
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | keepMounted: PropTypes.bool,
|
167 | |
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | middleware: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.oneOf([false]), PropTypes.shape({
|
175 | fn: PropTypes.func.isRequired,
|
176 | name: PropTypes.string.isRequired,
|
177 | options: PropTypes.any
|
178 | })])),
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | offset: PropTypes.oneOfType([PropTypes.func, PropTypes.number, PropTypes.shape({
|
187 | alignmentAxis: PropTypes.number,
|
188 | crossAxis: PropTypes.number,
|
189 | mainAxis: PropTypes.number
|
190 | })]),
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 | open: PropTypes.bool,
|
197 | |
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | placement: PropTypes.oneOf(['bottom-end', 'bottom-start', 'bottom', 'left-end', 'left-start', 'left', 'right-end', 'right-start', 'right', 'top-end', 'top-start', 'top']),
|
204 | |
205 |
|
206 |
|
207 |
|
208 |
|
209 | slotProps: PropTypes.shape({
|
210 | root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
|
211 | }),
|
212 | |
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | slots: PropTypes.shape({
|
219 | root: PropTypes.elementType
|
220 | }),
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | strategy: PropTypes.oneOf(['absolute', 'fixed'])
|
228 | } : void 0;
|
229 | export { Popup }; |
\ | No newline at end of file |