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