@hanzogui/popper 3.0.2 → 7.3.0

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