@tamagui/popper 2.7.7 → 3.0.0-beta.1091.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/dist/cjs/Popper.cjs +473 -607
  2. package/dist/cjs/Popper.native.cjs +498 -0
  3. package/dist/cjs/Popper.native.js +454 -611
  4. package/dist/cjs/Popper.native.js.map +1 -1
  5. package/dist/cjs/index.cjs +9 -11
  6. package/dist/cjs/index.native.cjs +20 -0
  7. package/dist/cjs/index.native.js +9 -10
  8. package/dist/cjs/index.native.js.map +1 -1
  9. package/dist/cjs/useRepositionOnNative.cjs +24 -0
  10. package/dist/cjs/useRepositionOnNative.native.cjs +63 -0
  11. package/dist/cjs/useRepositionOnNative.native.js +65 -0
  12. package/dist/cjs/useRepositionOnNative.native.js.map +1 -0
  13. package/dist/esm/Popper.mjs +443 -559
  14. package/dist/esm/Popper.mjs.map +1 -1
  15. package/dist/esm/Popper.native.js +423 -559
  16. package/dist/esm/Popper.native.js.map +1 -1
  17. package/dist/esm/index.js +1 -2
  18. package/dist/esm/index.mjs +1 -2
  19. package/dist/esm/index.native.js +1 -2
  20. package/dist/esm/useRepositionOnNative.mjs +4 -0
  21. package/dist/esm/useRepositionOnNative.mjs.map +1 -0
  22. package/dist/esm/useRepositionOnNative.native.js +36 -0
  23. package/dist/esm/useRepositionOnNative.native.js.map +1 -0
  24. package/dist/jsx/Popper.mjs +443 -559
  25. package/dist/jsx/Popper.mjs.map +1 -1
  26. package/dist/jsx/Popper.native.cjs +498 -0
  27. package/dist/jsx/Popper.native.js +454 -611
  28. package/dist/jsx/Popper.native.js.map +1 -1
  29. package/dist/jsx/index.js +1 -2
  30. package/dist/jsx/index.mjs +1 -2
  31. package/dist/jsx/index.native.cjs +20 -0
  32. package/dist/jsx/index.native.js +9 -10
  33. package/dist/jsx/index.native.js.map +1 -1
  34. package/dist/jsx/useRepositionOnNative.mjs +4 -0
  35. package/dist/jsx/useRepositionOnNative.mjs.map +1 -0
  36. package/dist/jsx/useRepositionOnNative.native.cjs +63 -0
  37. package/dist/jsx/useRepositionOnNative.native.js +65 -0
  38. package/dist/jsx/useRepositionOnNative.native.js.map +1 -0
  39. package/package.json +15 -13
  40. package/src/Popper.tsx +44 -129
  41. package/src/useRepositionOnNative.native.ts +40 -0
  42. package/src/useRepositionOnNative.ts +8 -0
  43. package/types/Popper.d.ts +42 -74
  44. package/types/Popper.d.ts.map +1 -1
  45. package/types/useRepositionOnNative.d.ts +6 -0
  46. package/types/useRepositionOnNative.d.ts.map +1 -0
  47. package/types/useRepositionOnNative.native.d.ts +7 -0
  48. package/types/useRepositionOnNative.native.d.ts.map +1 -0
  49. package/dist/esm/index.js.map +0 -1
  50. package/dist/esm/index.mjs.map +0 -1
  51. package/dist/esm/index.native.js.map +0 -1
  52. package/dist/jsx/index.js.map +0 -1
  53. package/dist/jsx/index.mjs.map +0 -1
@@ -1,590 +1,474 @@
1
1
  import { flushSync } from "react-dom";
2
2
  import { useComposedRefs } from "@tamagui/compose-refs";
3
3
  import { isWeb, useIsomorphicLayoutEffect } from "@tamagui/constants";
4
- import { LayoutMeasurementController, View, createStyledContext, getVariableValue, registerLayoutNode, styled } from "@tamagui/core";
4
+ import { LayoutMeasurementController, View, createRefComponent, createStyledContext, createStyledHOC, getVariableValue, registerLayoutNode, styled } from "@tamagui/core";
5
5
  import { FloatingOverrideContext, arrow, flip, getOverflowAncestors, offset, platform, shift, size, useFloating } from "@tamagui/floating";
6
- import { getSpace } from "@tamagui/get-token";
7
- import { YStack } from "@tamagui/stacks";
6
+ import { getSize } from "@tamagui/get-token";
8
7
  import { startTransition } from "@tamagui/start-transition";
9
8
  import * as React from "react";
9
+ import { useRepositionOnNative } from "./useRepositionOnNative.mjs";
10
10
  import { jsx } from "react/jsx-runtime";
11
+
11
12
  const PopperContextFast = createStyledContext({}, "Popper__");
12
13
  const PopperPositionContext = createStyledContext;
13
- const {
14
- useStyledContext: usePopperContext,
15
- Provider: PopperProviderFast
16
- } = PopperContextFast;
14
+ const { useStyledContext: usePopperContext, Provider: PopperProviderFast } = PopperContextFast;
17
15
  const PopperContextSlow = createStyledContext({}, "PopperSlow__");
18
- const {
19
- useStyledContext: usePopperContextSlow,
20
- Provider: PopperProviderSlow
21
- } = PopperContextSlow;
22
- const PopperProvider = ({
23
- scope,
24
- children,
25
- ...context
26
- }) => {
27
- const fns = React.useRef(context);
28
- fns.current = context;
29
- const [slowContext] = React.useState(() => ({
30
- refs: context.refs,
31
- triggerElements: context.triggerElements,
32
- update(...a) {
33
- fns.current.update(...a);
34
- },
35
- getReferenceProps(p) {
36
- return fns.current.getReferenceProps?.(p);
37
- },
38
- onHoverReference(e) {
39
- fns.current.onHoverReference?.(e);
40
- },
41
- onLeaveReference() {
42
- fns.current.onLeaveReference?.();
43
- }
44
- }));
45
- return /* @__PURE__ */jsx(PopperProviderFast, {
46
- scope,
47
- ...context,
48
- children: /* @__PURE__ */jsx(PopperProviderSlow, {
49
- scope,
50
- ...slowContext,
51
- children
52
- })
53
- });
16
+ const { useStyledContext: usePopperContextSlow, Provider: PopperProviderSlow } = PopperContextSlow;
17
+ const PopperProvider = ({ scope, children, ...context }) => {
18
+ const fns = React.useRef(context);
19
+ fns.current = context;
20
+ const [slowContext] = React.useState(() => ({
21
+ refs: context.refs,
22
+ triggerElements: context.triggerElements,
23
+ update(...a) {
24
+ fns.current.update(...a);
25
+ },
26
+ getReferenceProps(p) {
27
+ return fns.current.getReferenceProps?.(p);
28
+ },
29
+ onHoverReference(e) {
30
+ fns.current.onHoverReference?.(e);
31
+ },
32
+ onLeaveReference() {
33
+ fns.current.onLeaveReference?.();
34
+ }
35
+ }));
36
+ return /* @__PURE__ */ jsx(PopperProviderFast, {
37
+ scope,
38
+ ...context,
39
+ children: /* @__PURE__ */ jsx(PopperProviderSlow, {
40
+ scope,
41
+ ...slowContext,
42
+ children
43
+ })
44
+ });
54
45
  };
55
46
  const checkFloating = void 0;
56
47
  const setupOptions = {};
57
48
  function setupPopper(options) {
58
- Object.assign(setupOptions, options);
49
+ Object.assign(setupOptions, options);
59
50
  }
60
51
  function getSideAndAlignFromPlacement(placement) {
61
- const [side, align = "center"] = placement.split("-");
62
- return [side, align];
52
+ const [side, align = "center"] = placement.split("-");
53
+ return [side, align];
63
54
  }
64
- const transformOriginMiddleware = options => ({
65
- name: "transformOrigin",
66
- options,
67
- fn(data) {
68
- const {
69
- placement,
70
- rects,
71
- middlewareData
72
- } = data;
73
- const isArrowHidden = middlewareData.arrow?.centerOffset !== 0;
74
- const arrowWidth = isArrowHidden ? 0 : options.arrowWidth;
75
- const arrowHeight = isArrowHidden ? 0 : options.arrowHeight;
76
- const [placedSide, placedAlign] = getSideAndAlignFromPlacement(placement);
77
- const noArrowAlign = {
78
- start: "0%",
79
- center: "50%",
80
- end: "100%"
81
- }[placedAlign];
82
- const arrowXCenter = (middlewareData.arrow?.x ?? 0) + arrowWidth / 2;
83
- const arrowYCenter = (middlewareData.arrow?.y ?? 0) + arrowHeight / 2;
84
- let x = "";
85
- let y = "";
86
- if (placedSide === "bottom") {
87
- x = isArrowHidden ? noArrowAlign : `${arrowXCenter}px`;
88
- y = `${-arrowHeight}px`;
89
- } else if (placedSide === "top") {
90
- x = isArrowHidden ? noArrowAlign : `${arrowXCenter}px`;
91
- y = `${rects.floating.height + arrowHeight}px`;
92
- } else if (placedSide === "right") {
93
- x = `${-arrowHeight}px`;
94
- y = isArrowHidden ? noArrowAlign : `${arrowYCenter}px`;
95
- } else if (placedSide === "left") {
96
- x = `${rects.floating.width + arrowHeight}px`;
97
- y = isArrowHidden ? noArrowAlign : `${arrowYCenter}px`;
98
- }
99
- return {
100
- data: {
101
- x,
102
- y
103
- }
104
- };
105
- }
55
+ const transformOriginMiddleware = (options) => ({
56
+ name: "transformOrigin",
57
+ options,
58
+ fn(data) {
59
+ const { placement, rects, middlewareData } = data;
60
+ const isArrowHidden = middlewareData.arrow?.centerOffset !== 0;
61
+ const arrowWidth = isArrowHidden ? 0 : options.arrowWidth;
62
+ const arrowHeight = isArrowHidden ? 0 : options.arrowHeight;
63
+ const [placedSide, placedAlign] = getSideAndAlignFromPlacement(placement);
64
+ const noArrowAlign = {
65
+ start: "0%",
66
+ center: "50%",
67
+ end: "100%"
68
+ }[placedAlign];
69
+ const arrowXCenter = (middlewareData.arrow?.x ?? 0) + arrowWidth / 2;
70
+ const arrowYCenter = (middlewareData.arrow?.y ?? 0) + arrowHeight / 2;
71
+ let x = "";
72
+ let y = "";
73
+ if (placedSide === "bottom") {
74
+ x = isArrowHidden ? noArrowAlign : `${arrowXCenter}px`;
75
+ y = `${-arrowHeight}px`;
76
+ } else if (placedSide === "top") {
77
+ x = isArrowHidden ? noArrowAlign : `${arrowXCenter}px`;
78
+ y = `${rects.floating.height + arrowHeight}px`;
79
+ } else if (placedSide === "right") {
80
+ x = `${-arrowHeight}px`;
81
+ y = isArrowHidden ? noArrowAlign : `${arrowYCenter}px`;
82
+ } else if (placedSide === "left") {
83
+ x = `${rects.floating.width + arrowHeight}px`;
84
+ y = isArrowHidden ? noArrowAlign : `${arrowYCenter}px`;
85
+ }
86
+ return { data: {
87
+ x,
88
+ y
89
+ } };
90
+ }
106
91
  });
107
92
  function tamaguiAutoUpdate(reference, floating, update) {
108
- update();
109
- let rafId = requestAnimationFrame(() => {
110
- update();
111
- rafId = 0;
112
- });
113
- const cleanups = [() => {
114
- if (rafId) cancelAnimationFrame(rafId);
115
- }];
116
- if (reference instanceof HTMLElement) cleanups.push(registerLayoutNode(reference, update));
117
- if (typeof ResizeObserver !== "undefined") {
118
- let lastWidth = -1;
119
- let lastHeight = -1;
120
- const ro = new ResizeObserver(entries => {
121
- const entry = entries[entries.length - 1];
122
- if (!entry) return;
123
- const {
124
- width,
125
- height
126
- } = entry.contentRect;
127
- if (lastWidth === -1) {
128
- lastWidth = width;
129
- lastHeight = height;
130
- return;
131
- }
132
- if (width !== lastWidth || height !== lastHeight) {
133
- lastWidth = width;
134
- lastHeight = height;
135
- update();
136
- }
137
- });
138
- ro.observe(floating);
139
- cleanups.push(() => ro.disconnect());
140
- }
141
- const ancestors = [...(reference instanceof Element ? getOverflowAncestors(reference) : []), ...getOverflowAncestors(floating)];
142
- const uniqueAncestors = [...new Set(ancestors)];
143
- for (const ancestor of uniqueAncestors) ancestor.addEventListener("scroll", update, {
144
- passive: true
145
- });
146
- window.addEventListener("resize", update);
147
- cleanups.push(() => {
148
- for (const ancestor of uniqueAncestors) ancestor.removeEventListener("scroll", update);
149
- window.removeEventListener("resize", update);
150
- });
151
- return () => cleanups.forEach(fn => fn());
93
+ update();
94
+ let rafId = requestAnimationFrame(() => {
95
+ update();
96
+ rafId = 0;
97
+ });
98
+ const cleanups = [() => {
99
+ if (rafId) cancelAnimationFrame(rafId);
100
+ }];
101
+ if (reference instanceof HTMLElement) cleanups.push(registerLayoutNode(reference, update));
102
+ if (typeof ResizeObserver !== "undefined") {
103
+ let lastWidth = -1;
104
+ let lastHeight = -1;
105
+ const ro = new ResizeObserver((entries) => {
106
+ const entry = entries[entries.length - 1];
107
+ if (!entry) return;
108
+ const { width, height } = entry.contentRect;
109
+ if (lastWidth === -1) {
110
+ lastWidth = width;
111
+ lastHeight = height;
112
+ return;
113
+ }
114
+ if (width !== lastWidth || height !== lastHeight) {
115
+ lastWidth = width;
116
+ lastHeight = height;
117
+ update();
118
+ }
119
+ });
120
+ ro.observe(floating);
121
+ cleanups.push(() => ro.disconnect());
122
+ }
123
+ const ancestors = [...reference instanceof Element ? getOverflowAncestors(reference) : [], ...getOverflowAncestors(floating)];
124
+ const uniqueAncestors = [...new Set(ancestors)];
125
+ for (const ancestor of uniqueAncestors) ancestor.addEventListener("scroll", update, { passive: true });
126
+ window.addEventListener("resize", update);
127
+ cleanups.push(() => {
128
+ for (const ancestor of uniqueAncestors) ancestor.removeEventListener("scroll", update);
129
+ window.removeEventListener("resize", update);
130
+ });
131
+ return () => cleanups.forEach((fn) => fn());
152
132
  }
153
133
  function Popper(props) {
154
- const {
155
- children,
156
- size: size$1,
157
- strategy = "absolute",
158
- placement = "bottom",
159
- stayInFrame,
160
- allowFlip,
161
- offset: offset$1,
162
- disableRTL,
163
- resize,
164
- passThrough,
165
- open,
166
- scope
167
- } = props;
168
- const [arrowEl, setArrow] = React.useState(null);
169
- const [arrowSize, setArrowSize] = React.useState(0);
170
- const offsetOptions = offset$1 ?? arrowSize;
171
- const floatingStyle = React.useRef({});
172
- const isOpen = passThrough ? false : open ?? true;
173
- const middlewareRef = React.useRef([]);
174
- if (isOpen) middlewareRef.current = [typeof offsetOptions !== "undefined" ? offset(offsetOptions) : null, allowFlip ? flip(typeof allowFlip === "boolean" ? {} : allowFlip) : null, stayInFrame ? shift({
175
- padding: 10,
176
- mainAxis: true,
177
- crossAxis: false,
178
- ...(typeof stayInFrame === "object" ? stayInFrame : null)
179
- }) : null, arrowEl ? arrow({
180
- element: arrowEl
181
- }) : null, checkFloating, resize ? size({
182
- padding: typeof stayInFrame === "object" ? stayInFrame.padding : 10,
183
- apply({
184
- availableHeight,
185
- availableWidth
186
- }) {
187
- if (passThrough) return;
188
- Object.assign(floatingStyle.current, {
189
- maxHeight: `${availableHeight}px`,
190
- maxWidth: `${availableWidth}px`
191
- });
192
- const floatingChild = floating.refs.floating.current?.firstChild;
193
- if (floatingChild && floatingChild instanceof HTMLElement) Object.assign(floatingChild.style, floatingStyle.current);
194
- },
195
- ...(typeof resize === "object" && resize)
196
- }) : null, size({
197
- apply({
198
- elements,
199
- rects,
200
- availableWidth,
201
- availableHeight
202
- }) {
203
- const {
204
- width: anchorWidth,
205
- height: anchorHeight
206
- } = rects.reference;
207
- const contentStyle = elements.floating.style;
208
- contentStyle.setProperty("--tamagui-popper-available-width", `${availableWidth}px`);
209
- contentStyle.setProperty("--tamagui-popper-available-height", `${availableHeight}px`);
210
- contentStyle.setProperty("--tamagui-popper-anchor-width", `${anchorWidth}px`);
211
- contentStyle.setProperty("--tamagui-popper-anchor-height", `${anchorHeight}px`);
212
- }
213
- }), transformOriginMiddleware({
214
- arrowHeight: arrowSize,
215
- arrowWidth: arrowSize
216
- })].filter(Boolean);
217
- let floating = useFloating({
218
- open: isOpen,
219
- strategy,
220
- placement,
221
- sameScrollView: false,
222
- whileElementsMounted: !isOpen ? void 0 : tamaguiAutoUpdate,
223
- platform: disableRTL ?? setupOptions.disableRTL ? {
224
- ...platform,
225
- isRTL(element) {
226
- return false;
227
- }
228
- } : platform,
229
- middleware: middlewareRef.current
230
- });
231
- floating = React.useMemo(() => {
232
- const og = floating.getFloatingProps;
233
- if (resize && og) floating.getFloatingProps = props2 => {
234
- return og({
235
- ...props2,
236
- style: {
237
- ...props2.style,
238
- ...floatingStyle.current
239
- }
240
- });
241
- };
242
- return floating;
243
- }, [floating, resize ? JSON.stringify(resize) : null]);
244
- const {
245
- middlewareData
246
- } = floating;
247
- return /* @__PURE__ */jsx(PopperProvider, {
248
- scope,
249
- ...React.useMemo(() => {
250
- return {
251
- size: size$1,
252
- arrowRef: setArrow,
253
- arrowStyle: middlewareData.arrow,
254
- onArrowSize: setArrowSize,
255
- hasFloating: middlewareData.checkFloating?.hasFloating,
256
- transformOrigin: middlewareData.transformOrigin,
257
- open: !!open,
258
- ...floating
259
- };
260
- }, [open, size$1, floating, JSON.stringify(middlewareData.arrow || null), JSON.stringify(middlewareData.transformOrigin || null)]),
261
- children: /* @__PURE__ */jsx(FloatingOverrideContext.Provider, {
262
- value: null,
263
- children
264
- })
265
- });
134
+ const { children, size: size$1, strategy = "absolute", placement = "bottom", stayInFrame, allowFlip, offset: offset$1, disableRTL, resize, passThrough, open, scope } = props;
135
+ const [arrowEl, setArrow] = React.useState(null);
136
+ const [arrowSize, setArrowSize] = React.useState(0);
137
+ const offsetOptions = offset$1 ?? arrowSize;
138
+ const floatingStyle = React.useRef({});
139
+ const isOpen = passThrough ? false : open ?? true;
140
+ const middlewareRef = React.useRef([]);
141
+ if (isOpen) middlewareRef.current = [
142
+ typeof offsetOptions !== "undefined" ? offset(offsetOptions) : null,
143
+ allowFlip ? flip(typeof allowFlip === "boolean" ? {} : allowFlip) : null,
144
+ stayInFrame ? shift({
145
+ padding: 10,
146
+ mainAxis: true,
147
+ crossAxis: false,
148
+ ...typeof stayInFrame === "object" ? stayInFrame : null
149
+ }) : null,
150
+ arrowEl ? arrow({ element: arrowEl }) : null,
151
+ checkFloating,
152
+ resize ? size({
153
+ padding: typeof stayInFrame === "object" ? stayInFrame.padding : 10,
154
+ apply({ availableHeight, availableWidth }) {
155
+ if (passThrough) return;
156
+ Object.assign(floatingStyle.current, {
157
+ maxHeight: `${availableHeight}px`,
158
+ maxWidth: `${availableWidth}px`
159
+ });
160
+ const floatingChild = floating.refs.floating.current?.firstChild;
161
+ if (floatingChild && floatingChild instanceof HTMLElement) Object.assign(floatingChild.style, floatingStyle.current);
162
+ },
163
+ ...typeof resize === "object" && resize
164
+ }) : null,
165
+ size({ apply({ elements, rects, availableWidth, availableHeight }) {
166
+ const { width: anchorWidth, height: anchorHeight } = rects.reference;
167
+ const contentStyle = elements.floating.style;
168
+ contentStyle.setProperty("--tamagui-popper-available-width", `${availableWidth}px`);
169
+ contentStyle.setProperty("--tamagui-popper-available-height", `${availableHeight}px`);
170
+ contentStyle.setProperty("--tamagui-popper-anchor-width", `${anchorWidth}px`);
171
+ contentStyle.setProperty("--tamagui-popper-anchor-height", `${anchorHeight}px`);
172
+ } }),
173
+ transformOriginMiddleware({
174
+ arrowHeight: arrowSize,
175
+ arrowWidth: arrowSize
176
+ })
177
+ ].filter(Boolean);
178
+ let floating = useFloating({
179
+ open: isOpen,
180
+ strategy,
181
+ placement,
182
+ sameScrollView: false,
183
+ whileElementsMounted: !isOpen ? void 0 : tamaguiAutoUpdate,
184
+ platform: disableRTL ?? setupOptions.disableRTL ? {
185
+ ...platform,
186
+ isRTL(element) {
187
+ return false;
188
+ }
189
+ } : platform,
190
+ middleware: middlewareRef.current
191
+ });
192
+ floating = React.useMemo(() => {
193
+ const og = floating.getFloatingProps;
194
+ if (resize && og) floating.getFloatingProps = (props2) => {
195
+ return og({
196
+ ...props2,
197
+ style: {
198
+ ...props2.style,
199
+ ...floatingStyle.current
200
+ }
201
+ });
202
+ };
203
+ return floating;
204
+ }, [floating, resize ? JSON.stringify(resize) : null]);
205
+ const { middlewareData } = floating;
206
+ useRepositionOnNative(floating.update, passThrough);
207
+ return /* @__PURE__ */ jsx(PopperProvider, {
208
+ scope,
209
+ ...React.useMemo(() => {
210
+ return {
211
+ size: size$1,
212
+ arrowRef: setArrow,
213
+ arrowStyle: middlewareData.arrow,
214
+ onArrowSize: setArrowSize,
215
+ hasFloating: middlewareData.checkFloating?.hasFloating,
216
+ transformOrigin: middlewareData.transformOrigin,
217
+ open: !!open,
218
+ ...floating
219
+ };
220
+ }, [
221
+ open,
222
+ size$1,
223
+ floating,
224
+ JSON.stringify(middlewareData.arrow || null),
225
+ JSON.stringify(middlewareData.transformOrigin || null)
226
+ ]),
227
+ children: /* @__PURE__ */ jsx(FloatingOverrideContext.Provider, {
228
+ value: null,
229
+ children
230
+ })
231
+ });
266
232
  }
267
- const PopperAnchor = YStack.styleable(function PopperAnchor2(props, forwardedRef) {
268
- const {
269
- virtualRef,
270
- scope,
271
- ...rest
272
- } = props;
273
- const context = usePopperContextSlow(scope);
274
- const {
275
- getReferenceProps,
276
- refs,
277
- update
278
- } = context;
279
- const ref = React.useRef(null);
280
- const triggerId = React.useId();
281
- React.useEffect(() => {
282
- if (!scope || !context.triggerElements || !ref.current) return;
283
- if (!(ref.current instanceof Element)) return;
284
- const el = ref.current;
285
- context.triggerElements.add(triggerId, el);
286
- return () => {
287
- context.triggerElements?.delete(triggerId);
288
- };
289
- }, [scope, triggerId, context.triggerElements]);
290
- React.useEffect(() => {
291
- if (virtualRef) {
292
- refs.setReference(virtualRef.current);
293
- update();
294
- }
295
- }, [virtualRef]);
296
- const refProps = getReferenceProps?.({
297
- ...rest,
298
- ref
299
- }) || null;
300
- const safeSetReference = React.useCallback(node => {
301
- startTransition(() => {
302
- refs.setReference(node);
303
- });
304
- }, [refs]);
305
- const shouldHandleInHover = isWeb && scope;
306
- const composedRefs = useComposedRefs(forwardedRef, ref, shouldHandleInHover ? void 0 : safeSetReference);
307
- return /* @__PURE__ */jsx(View, {
308
- ...rest,
309
- ...refProps,
310
- ref: composedRefs,
311
- ...(shouldHandleInHover && {
312
- onMouseEnter: e => {
313
- const el = e.currentTarget ?? ref.current;
314
- if (el instanceof HTMLElement) {
315
- flushSync(() => refs.setReference(el));
316
- update();
317
- if (!refProps) return;
318
- refProps.onPointerEnter?.(e);
319
- context.onHoverReference?.(e.nativeEvent);
320
- }
321
- },
322
- onMouseLeave: e => {
323
- context.onLeaveReference?.();
324
- refProps?.onMouseLeave?.(e);
325
- }
326
- })
327
- });
233
+ const PopperAnchor = createStyledHOC(View, function PopperAnchor2(props, forwardedRef) {
234
+ const { virtualRef, scope, ...rest } = props;
235
+ const context = usePopperContextSlow(scope);
236
+ const { getReferenceProps, refs, update } = context;
237
+ const ref = React.useRef(null);
238
+ const triggerId = React.useId();
239
+ React.useEffect(() => {
240
+ if (!scope || !context.triggerElements || !ref.current) return;
241
+ if (!(ref.current instanceof Element)) return;
242
+ const el = ref.current;
243
+ context.triggerElements.add(triggerId, el);
244
+ return () => {
245
+ context.triggerElements?.delete(triggerId);
246
+ };
247
+ }, [
248
+ scope,
249
+ triggerId,
250
+ context.triggerElements
251
+ ]);
252
+ React.useEffect(() => {
253
+ if (virtualRef) {
254
+ refs.setReference(virtualRef.current);
255
+ update();
256
+ }
257
+ }, [virtualRef]);
258
+ const refProps = getReferenceProps?.({
259
+ ...rest,
260
+ ref
261
+ }) || null;
262
+ const safeSetReference = React.useCallback((node) => {
263
+ startTransition(() => {
264
+ refs.setReference(node);
265
+ });
266
+ }, [refs]);
267
+ const shouldHandleInHover = isWeb && scope;
268
+ const composedRefs = useComposedRefs(forwardedRef, ref, shouldHandleInHover ? void 0 : safeSetReference);
269
+ return /* @__PURE__ */ jsx(View, {
270
+ ...rest,
271
+ ...refProps,
272
+ ref: composedRefs,
273
+ ...shouldHandleInHover && {
274
+ onMouseEnter: (e) => {
275
+ const el = e.currentTarget ?? ref.current;
276
+ if (el instanceof HTMLElement) {
277
+ flushSync(() => refs.setReference(el));
278
+ update();
279
+ if (!refProps) return;
280
+ refProps.onPointerEnter?.(e);
281
+ context.onHoverReference?.(e.nativeEvent);
282
+ }
283
+ },
284
+ onMouseLeave: (e) => {
285
+ context.onLeaveReference?.();
286
+ refProps?.onMouseLeave?.(e);
287
+ }
288
+ }
289
+ });
328
290
  });
329
- const PopperContentFrame = styled(YStack, {
330
- name: "PopperContent",
331
- variants: {
332
- unstyled: {
333
- true: {}
334
- },
335
- size: {
336
- "...size": (val, {
337
- tokens
338
- }) => {
339
- return {
340
- padding: tokens.space[val],
341
- borderRadius: tokens.radius[val]
342
- };
343
- }
344
- }
345
- }
291
+ const PopperContentFrame = styled(View, { displayName: "PopperContent" });
292
+ const PopperContent = createRefComponent(function PopperContent2(props, forwardedRef) {
293
+ const isAnimatePosControlled = "animatePosition" in props;
294
+ const { scope, animatePosition, children, passThrough, ...rest } = props;
295
+ const animatePos = animatePosition;
296
+ const context = usePopperContext(scope);
297
+ const { strategy, placement, refs, x, y, getFloatingProps, isPositioned, transformOrigin, update } = context;
298
+ const updateRef = React.useRef(update);
299
+ updateRef.current = update;
300
+ const lastNodeRef = React.useRef(null);
301
+ const safeSetFloating = React.useCallback((node) => {
302
+ const isNewNode = node !== lastNodeRef.current;
303
+ if (node) {
304
+ lastNodeRef.current = node;
305
+ refs.setFloating(node);
306
+ if (!isNewNode) updateRef.current?.();
307
+ }
308
+ }, [refs.setFloating]);
309
+ React.useEffect(() => {
310
+ return () => {
311
+ const ourNode = lastNodeRef.current;
312
+ if (ourNode && refs.floating.current === ourNode) refs.setFloating(null);
313
+ lastNodeRef.current = null;
314
+ };
315
+ }, []);
316
+ const contentRefs = useComposedRefs(safeSetFloating, forwardedRef);
317
+ const [needsMeasure, setNeedsMeasure] = React.useState(animatePos);
318
+ useIsomorphicLayoutEffect(() => {
319
+ if (needsMeasure && x && y) setNeedsMeasure(false);
320
+ }, [
321
+ needsMeasure,
322
+ animatePos,
323
+ x,
324
+ y
325
+ ]);
326
+ const hasBeenPositioned = React.useRef(false);
327
+ const lastGoodPosition = React.useRef({
328
+ x: 0,
329
+ y: 0
330
+ });
331
+ if (x !== 0 || y !== 0) {
332
+ lastGoodPosition.current = {
333
+ x,
334
+ y
335
+ };
336
+ if (isPositioned) hasBeenPositioned.current = true;
337
+ }
338
+ const brieflyZero = hasBeenPositioned.current && x === 0 && y === 0;
339
+ const effectiveX = brieflyZero ? lastGoodPosition.current.x : x;
340
+ const effectiveY = brieflyZero ? lastGoodPosition.current.y : y;
341
+ const hide = !hasBeenPositioned.current && effectiveX === 0 && effectiveY === 0;
342
+ const disableAnimationProp = animatePos === "even-when-repositioning" ? needsMeasure : !hasBeenPositioned.current && !isPositioned || needsMeasure;
343
+ const [disableAnimation, setDisableAnimation] = React.useState(disableAnimationProp);
344
+ React.useEffect(() => {
345
+ setDisableAnimation(disableAnimationProp);
346
+ }, [disableAnimationProp]);
347
+ const frameProps = {
348
+ ref: contentRefs,
349
+ ...hide ? {} : {
350
+ x: effectiveX || 0,
351
+ y: effectiveY || 0
352
+ },
353
+ top: 0,
354
+ left: 0,
355
+ position: strategy,
356
+ opacity: hide ? 0 : 1,
357
+ ...isAnimatePosControlled && {
358
+ transition: animatePos && !disableAnimation ? rest.transition : "none",
359
+ animatePresence: false
360
+ }
361
+ };
362
+ const { style, ...floatingProps } = getFloatingProps ? getFloatingProps(frameProps) : frameProps;
363
+ const transformOriginStyle = isWeb && transformOrigin ? { transformOrigin: `${transformOrigin.x} ${transformOrigin.y}` } : void 0;
364
+ return /* @__PURE__ */ jsx(LayoutMeasurementController, {
365
+ disable: !context.open,
366
+ children: /* @__PURE__ */ jsx(View, {
367
+ passThrough,
368
+ ref: contentRefs,
369
+ direction: rest.direction,
370
+ ...passThrough ? null : floatingProps,
371
+ ...!passThrough && animatePos && { "data-popper-animate-position": "true" },
372
+ children: /* @__PURE__ */ jsx(PopperContentFrame, {
373
+ passThrough,
374
+ ...!passThrough && {
375
+ "data-placement": placement,
376
+ "data-strategy": strategy,
377
+ ...style,
378
+ ...transformOriginStyle,
379
+ ...rest
380
+ },
381
+ children
382
+ }, "popper-content-frame")
383
+ })
384
+ });
346
385
  });
347
- const PopperContent = React.forwardRef(function PopperContent2(props, forwardedRef) {
348
- const isAnimatePosControlled = "animatePosition" in props || "enableAnimationForPositionChange" in props;
349
- const {
350
- scope,
351
- animatePosition,
352
- enableAnimationForPositionChange,
353
- children,
354
- passThrough,
355
- unstyled,
356
- ...rest
357
- } = props;
358
- const animatePos = animatePosition ?? enableAnimationForPositionChange;
359
- const context = usePopperContext(scope);
360
- const {
361
- strategy,
362
- placement,
363
- refs,
364
- x,
365
- y,
366
- getFloatingProps,
367
- size,
368
- isPositioned,
369
- transformOrigin,
370
- update
371
- } = context;
372
- const updateRef = React.useRef(update);
373
- updateRef.current = update;
374
- const lastNodeRef = React.useRef(null);
375
- const safeSetFloating = React.useCallback(node => {
376
- const isNewNode = node !== lastNodeRef.current;
377
- if (node) {
378
- lastNodeRef.current = node;
379
- refs.setFloating(node);
380
- if (!isNewNode) updateRef.current?.();
381
- }
382
- }, [refs.setFloating]);
383
- React.useEffect(() => {
384
- return () => {
385
- const ourNode = lastNodeRef.current;
386
- if (ourNode && refs.floating.current === ourNode) refs.setFloating(null);
387
- lastNodeRef.current = null;
388
- };
389
- }, []);
390
- const contentRefs = useComposedRefs(safeSetFloating, forwardedRef);
391
- const [needsMeasure, setNeedsMeasure] = React.useState(animatePos);
392
- useIsomorphicLayoutEffect(() => {
393
- if (needsMeasure && x && y) setNeedsMeasure(false);
394
- }, [needsMeasure, animatePos, x, y]);
395
- const hasBeenPositioned = React.useRef(false);
396
- const lastGoodPosition = React.useRef({
397
- x: 0,
398
- y: 0
399
- });
400
- if (x !== 0 || y !== 0) {
401
- lastGoodPosition.current = {
402
- x,
403
- y
404
- };
405
- if (isPositioned) hasBeenPositioned.current = true;
406
- }
407
- const brieflyZero = hasBeenPositioned.current && x === 0 && y === 0;
408
- const effectiveX = brieflyZero ? lastGoodPosition.current.x : x;
409
- const effectiveY = brieflyZero ? lastGoodPosition.current.y : y;
410
- const hide = !hasBeenPositioned.current && effectiveX === 0 && effectiveY === 0;
411
- const disableAnimationProp = animatePos === "even-when-repositioning" ? needsMeasure : !hasBeenPositioned.current && !isPositioned || needsMeasure;
412
- const [disableAnimation, setDisableAnimation] = React.useState(disableAnimationProp);
413
- React.useEffect(() => {
414
- setDisableAnimation(disableAnimationProp);
415
- }, [disableAnimationProp]);
416
- const frameProps = {
417
- ref: contentRefs,
418
- ...(hide ? {} : {
419
- x: effectiveX || 0,
420
- y: effectiveY || 0
421
- }),
422
- top: 0,
423
- left: 0,
424
- position: strategy,
425
- opacity: hide ? 0 : 1,
426
- ...(isAnimatePosControlled && {
427
- transition: animatePos ? rest.transition : void 0,
428
- animateOnly: animatePos && !disableAnimation ? rest.animateOnly : [],
429
- animatePresence: false
430
- })
431
- };
432
- const {
433
- style,
434
- ...floatingProps
435
- } = getFloatingProps ? getFloatingProps(frameProps) : frameProps;
436
- const transformOriginStyle = isWeb && transformOrigin ? {
437
- transformOrigin: `${transformOrigin.x} ${transformOrigin.y}`
438
- } : void 0;
439
- return /* @__PURE__ */jsx(LayoutMeasurementController, {
440
- disable: !context.open,
441
- children: /* @__PURE__ */jsx(View, {
442
- passThrough,
443
- ref: contentRefs,
444
- direction: rest.direction,
445
- ...(passThrough ? null : floatingProps),
446
- ...(!passThrough && animatePos && {
447
- "data-popper-animate-position": "true"
448
- }),
449
- children: /* @__PURE__ */jsx(PopperContentFrame, {
450
- passThrough,
451
- unstyled,
452
- ...(!passThrough && {
453
- "data-placement": placement,
454
- "data-strategy": strategy,
455
- size,
456
- ...style,
457
- ...transformOriginStyle,
458
- ...rest
459
- }),
460
- children
461
- }, "popper-content-frame")
462
- })
463
- });
386
+ const PopperArrowFrame = styled(View, {
387
+ displayName: "PopperArrow",
388
+ position: "relative"
464
389
  });
465
- const PopperArrowFrame = styled(YStack, {
466
- name: "PopperArrow",
467
- variants: {
468
- unstyled: {
469
- false: {
470
- borderColor: "$borderColor",
471
- backgroundColor: "$background",
472
- position: "relative"
473
- }
474
- }
475
- },
476
- defaultVariants: {
477
- unstyled: process.env.TAMAGUI_HEADLESS === "1"
478
- }
479
- });
480
- const PopperArrowOuterFrame = styled(YStack, {
481
- name: "PopperArrowOuter",
482
- variants: {
483
- unstyled: {
484
- false: {
485
- position: "absolute",
486
- zIndex: 1e6,
487
- pointerEvents: "none",
488
- overflow: "hidden",
489
- alignItems: "center",
490
- justifyContent: "center"
491
- }
492
- }
493
- },
494
- defaultVariants: {
495
- unstyled: process.env.TAMAGUI_HEADLESS === "1"
496
- }
390
+ const PopperArrowOuterFrame = styled(View, {
391
+ displayName: "PopperArrowOuter",
392
+ position: "absolute",
393
+ zIndex: 1e6,
394
+ pointerEvents: "none",
395
+ overflow: "hidden",
396
+ alignItems: "center",
397
+ justifyContent: "center"
497
398
  });
498
399
  const opposites = {
499
- top: "bottom",
500
- right: "left",
501
- bottom: "top",
502
- left: "right"
400
+ top: "bottom",
401
+ right: "left",
402
+ bottom: "top",
403
+ left: "right"
503
404
  };
504
- const PopperArrow = React.forwardRef(function PopperArrow2(propsIn, forwardedRef) {
505
- const isAnimatePosControlled = "animatePosition" in propsIn;
506
- const {
507
- scope,
508
- animatePosition,
509
- transition,
510
- ...rest
511
- } = propsIn;
512
- const {
513
- offset,
514
- size: sizeProp,
515
- borderWidth = 0,
516
- ...arrowProps
517
- } = rest;
518
- const context = usePopperContext(scope);
519
- const sizeVal = typeof sizeProp === "number" ? sizeProp : getVariableValue(getSpace(sizeProp ?? context.size, {
520
- shift: -2,
521
- bounds: [2]
522
- }));
523
- const size = Math.max(0, +sizeVal);
524
- const {
525
- placement
526
- } = context;
527
- const refs = useComposedRefs(context.arrowRef, forwardedRef);
528
- const x = context.arrowStyle?.x || 0;
529
- const y = context.arrowStyle?.y || 0;
530
- const arrowPositioned = context.arrowStyle != null;
531
- const primaryPlacement = placement ? placement.split("-")[0] : "top";
532
- const arrowStyle = {
533
- x,
534
- y,
535
- width: size,
536
- height: size
537
- };
538
- const innerArrowStyle = {};
539
- const isVertical = primaryPlacement === "bottom" || primaryPlacement === "top";
540
- if (primaryPlacement) {
541
- arrowStyle[isVertical ? "width" : "height"] = size * 2;
542
- const oppSide = opposites[primaryPlacement];
543
- if (oppSide) {
544
- arrowStyle[oppSide] = -size;
545
- innerArrowStyle[oppSide] = size / 2;
546
- }
547
- if (oppSide === "top" || oppSide === "bottom") arrowStyle.left = 0;
548
- if (oppSide === "left" || oppSide === "right") arrowStyle.top = 0;
549
- useIsomorphicLayoutEffect(() => {
550
- context.onArrowSize?.(size);
551
- }, [size, context.onArrowSize]);
552
- }
553
- return /* @__PURE__ */jsx(PopperArrowOuterFrame, {
554
- ref: refs,
555
- ...arrowStyle,
556
- ...(!arrowPositioned && {
557
- opacity: 0
558
- }),
559
- ...(isAnimatePosControlled && {
560
- transition: animatePosition ? transition : void 0,
561
- animateOnly: animatePosition ? ["transform"] : [],
562
- animatePresence: false
563
- }),
564
- children: /* @__PURE__ */jsx(PopperArrowFrame, {
565
- width: size,
566
- height: size,
567
- ...arrowProps,
568
- ...innerArrowStyle,
569
- rotate: "45deg",
570
- ...(primaryPlacement === "bottom" && {
571
- borderLeftWidth: borderWidth,
572
- borderTopWidth: borderWidth
573
- }),
574
- ...(primaryPlacement === "top" && {
575
- borderBottomWidth: borderWidth,
576
- borderRightWidth: borderWidth
577
- }),
578
- ...(primaryPlacement === "right" && {
579
- borderLeftWidth: borderWidth,
580
- borderBottomWidth: borderWidth
581
- }),
582
- ...(primaryPlacement === "left" && {
583
- borderTopWidth: borderWidth,
584
- borderRightWidth: borderWidth
585
- })
586
- })
587
- });
405
+ const PopperArrow = createRefComponent(function PopperArrow2(propsIn, forwardedRef) {
406
+ const isAnimatePosControlled = "animatePosition" in propsIn;
407
+ const { scope, animatePosition, transition, ...rest } = propsIn;
408
+ const { offset, size: sizeProp, borderWidth = 0, ...arrowProps } = rest;
409
+ const context = usePopperContext(scope);
410
+ const sizeVal = typeof sizeProp === "number" ? sizeProp : getVariableValue(getSize(sizeProp ?? context.size ?? true)) * .52 - 11.5;
411
+ const size = Math.max(0, +sizeVal);
412
+ const { placement } = context;
413
+ const refs = useComposedRefs(context.arrowRef, forwardedRef);
414
+ const x = context.arrowStyle?.x || 0;
415
+ const y = context.arrowStyle?.y || 0;
416
+ const arrowPositioned = context.arrowStyle != null;
417
+ const primaryPlacement = placement ? placement.split("-")[0] : "top";
418
+ const arrowStyle = {
419
+ x,
420
+ y,
421
+ width: size,
422
+ height: size
423
+ };
424
+ const innerArrowStyle = {};
425
+ const isVertical = primaryPlacement === "bottom" || primaryPlacement === "top";
426
+ if (primaryPlacement) {
427
+ arrowStyle[isVertical ? "width" : "height"] = size * 2;
428
+ const oppSide = opposites[primaryPlacement];
429
+ if (oppSide) {
430
+ arrowStyle[oppSide] = -size;
431
+ innerArrowStyle[oppSide] = size / 2;
432
+ }
433
+ if (oppSide === "top" || oppSide === "bottom") arrowStyle.left = 0;
434
+ if (oppSide === "left" || oppSide === "right") arrowStyle.top = 0;
435
+ useIsomorphicLayoutEffect(() => {
436
+ context.onArrowSize?.(size);
437
+ }, [size, context.onArrowSize]);
438
+ }
439
+ return /* @__PURE__ */ jsx(PopperArrowOuterFrame, {
440
+ ref: refs,
441
+ ...arrowStyle,
442
+ ...!arrowPositioned && { opacity: 0 },
443
+ ...isAnimatePosControlled && {
444
+ transition: animatePosition ? { transform: transition } : "none",
445
+ animatePresence: false
446
+ },
447
+ children: /* @__PURE__ */ jsx(PopperArrowFrame, {
448
+ width: size,
449
+ height: size,
450
+ ...arrowProps,
451
+ ...innerArrowStyle,
452
+ rotate: "45deg",
453
+ ...primaryPlacement === "bottom" && {
454
+ borderLeftWidth: borderWidth,
455
+ borderTopWidth: borderWidth
456
+ },
457
+ ...primaryPlacement === "top" && {
458
+ borderBottomWidth: borderWidth,
459
+ borderRightWidth: borderWidth
460
+ },
461
+ ...primaryPlacement === "right" && {
462
+ borderLeftWidth: borderWidth,
463
+ borderBottomWidth: borderWidth
464
+ },
465
+ ...primaryPlacement === "left" && {
466
+ borderTopWidth: borderWidth,
467
+ borderRightWidth: borderWidth
468
+ }
469
+ })
470
+ });
588
471
  });
472
+
589
473
  export { Popper, PopperAnchor, PopperArrow, PopperArrowFrame, PopperContent, PopperContentFrame, PopperContextFast, PopperContextSlow, PopperPositionContext, PopperProvider, PopperProviderFast, PopperProviderSlow, setupPopper, usePopperContext, usePopperContextSlow };
590
474
  //# sourceMappingURL=Popper.mjs.map