@multiplatform.one/forms 7.21.0 → 7.22.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.
Files changed (48) hide show
  1. package/dist/cjs/FloatingPanel/index.cjs +85 -29
  2. package/dist/cjs/FloatingPanel/index.native.js +108 -11
  3. package/dist/cjs/FloatingPanel/index.native.js.map +1 -1
  4. package/dist/cjs/FloatingPanel/pointAnchor.cjs +88 -0
  5. package/dist/cjs/FloatingPanel/pointAnchor.native.js +98 -0
  6. package/dist/cjs/FloatingPanel/pointAnchor.native.js.map +1 -0
  7. package/dist/cjs/FloatingPanel/useFloatingPanel.cjs +4 -3
  8. package/dist/cjs/FloatingPanel/useFloatingPanel.native.js +4 -3
  9. package/dist/cjs/FloatingPanel/useFloatingPanel.native.js.map +1 -1
  10. package/dist/esm/FloatingPanel/index.mjs +86 -30
  11. package/dist/esm/FloatingPanel/index.mjs.map +1 -1
  12. package/dist/esm/FloatingPanel/index.native.js +107 -10
  13. package/dist/esm/FloatingPanel/index.native.js.map +1 -1
  14. package/dist/esm/FloatingPanel/pointAnchor.mjs +59 -0
  15. package/dist/esm/FloatingPanel/pointAnchor.mjs.map +1 -0
  16. package/dist/esm/FloatingPanel/pointAnchor.native.js +66 -0
  17. package/dist/esm/FloatingPanel/pointAnchor.native.js.map +1 -0
  18. package/dist/esm/FloatingPanel/useFloatingPanel.mjs +4 -3
  19. package/dist/esm/FloatingPanel/useFloatingPanel.mjs.map +1 -1
  20. package/dist/esm/FloatingPanel/useFloatingPanel.native.js +4 -3
  21. package/dist/esm/FloatingPanel/useFloatingPanel.native.js.map +1 -1
  22. package/dist/jsx/FloatingPanel/index.mjs +86 -30
  23. package/dist/jsx/FloatingPanel/index.mjs.map +1 -1
  24. package/dist/jsx/FloatingPanel/index.native.js +108 -11
  25. package/dist/jsx/FloatingPanel/index.native.js.map +1 -1
  26. package/dist/jsx/FloatingPanel/pointAnchor.mjs +59 -0
  27. package/dist/jsx/FloatingPanel/pointAnchor.mjs.map +1 -0
  28. package/dist/jsx/FloatingPanel/pointAnchor.native.js +98 -0
  29. package/dist/jsx/FloatingPanel/pointAnchor.native.js.map +1 -0
  30. package/dist/jsx/FloatingPanel/useFloatingPanel.mjs +4 -3
  31. package/dist/jsx/FloatingPanel/useFloatingPanel.mjs.map +1 -1
  32. package/dist/jsx/FloatingPanel/useFloatingPanel.native.js +4 -3
  33. package/dist/jsx/FloatingPanel/useFloatingPanel.native.js.map +1 -1
  34. package/package.json +8 -8
  35. package/src/FloatingPanel/FloatingPanel.pointMenu.native.spec.tsx +126 -0
  36. package/src/FloatingPanel/index.native.tsx +199 -81
  37. package/src/FloatingPanel/index.tsx +130 -42
  38. package/src/FloatingPanel/pointAnchor.spec.ts +141 -0
  39. package/src/FloatingPanel/pointAnchor.ts +122 -0
  40. package/src/FloatingPanel/useFloatingPanel.ts +9 -2
  41. package/types/FloatingPanel/index.d.ts +4 -1
  42. package/types/FloatingPanel/index.d.ts.map +1 -1
  43. package/types/FloatingPanel/index.native.d.ts +5 -0
  44. package/types/FloatingPanel/index.native.d.ts.map +1 -1
  45. package/types/FloatingPanel/pointAnchor.d.ts +66 -0
  46. package/types/FloatingPanel/pointAnchor.d.ts.map +1 -0
  47. package/types/FloatingPanel/useFloatingPanel.d.ts +7 -1
  48. package/types/FloatingPanel/useFloatingPanel.d.ts.map +1 -1
@@ -43,9 +43,15 @@
43
43
  * and the sheet never appears. Fields should not also toggle open on native.
44
44
  * - Opening a Modal under the same finger can synthesize a backdrop press;
45
45
  * backdrop dismiss is armed a short delay after the present begins.
46
+ *
47
+ * ContextMenu (`openOn="contextmenu"`): long-press opens it. On a window wider
48
+ * than OVERLAY_BREAKPOINT (a tablet) it opens at the long-press point, the
49
+ * same free overlay the web draws at the pointer; a phone keeps the sheet,
50
+ * as web does at ≤640.
46
51
  */
47
52
 
48
53
  import {
54
+ type ReactNode,
49
55
  useCallback,
50
56
  useContext,
51
57
  useEffect,
@@ -57,6 +63,7 @@ import {
57
63
  import {
58
64
  Animated,
59
65
  Easing,
66
+ I18nManager,
60
67
  Modal,
61
68
  PanResponder,
62
69
  Platform,
@@ -65,13 +72,16 @@ import {
65
72
  StyleSheet,
66
73
  useWindowDimensions,
67
74
  View as RNView,
75
+ type GestureResponderEvent,
68
76
  } from "react-native";
69
77
  import { SafeAreaInsetsContext, initialWindowMetrics } from "react-native-safe-area-context";
70
- import { Surface, useResolvedKnobs } from "@multiplatform.one/theme";
78
+ import { OVERLAY_BREAKPOINT, Surface, useResolvedKnobs } from "@multiplatform.one/theme";
71
79
  import { View, YStack } from "tamagui";
72
80
  import { t } from "../shared/t";
73
81
  import { useKeyboardInset } from "../shared/useKeyboardInset";
74
82
  import type { FloatingPanelProps } from "./index";
83
+ import { placeAtPoint, type PanelBounds, type PanelPoint } from "./pointAnchor";
84
+ import { panelViewportPadding } from "./useFloatingPanel";
75
85
 
76
86
  // Re-export the hook + constants so Select / Combobox / external consumers
77
87
  // resolve identically on native (their floating cores are unused on device
@@ -127,20 +137,31 @@ export function FloatingPanel({
127
137
  openOn = "press",
128
138
  }: FloatingPanelProps) {
129
139
  const { knobProps } = useResolvedKnobs();
130
- const { height: windowHeight } = useWindowDimensions();
140
+ const { width: windowWidth, height: windowHeight } = useWindowDimensions();
131
141
  // MPO-64: the sheet must end ABOVE the keyboard, not run under it.
132
142
  const keyboardInset = useKeyboardInset();
133
- const topInset = useContext(SafeAreaInsetsContext)?.top ?? initialWindowMetrics?.insets.top ?? 0;
143
+ const safeArea = useContext(SafeAreaInsetsContext) ?? initialWindowMetrics?.insets;
144
+ const topInset = safeArea?.top ?? 0;
145
+ const [pressPoint, setPressPoint] = useState<PanelPoint | null>(null);
134
146
  const [backdropArmed, setBackdropArmed] = useState(false);
135
147
  const armTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
136
148
  const close = useCallback(() => onOpenChange(false), [onOpenChange]);
137
149
  const handleTriggerPress = useCallback(() => {
138
150
  if (!disabled) onOpenChange(!open);
139
151
  }, [disabled, onOpenChange, open]);
140
- const handleTriggerLongPress = useCallback(() => {
141
- if (!disabled) onOpenChange(true);
142
- }, [disabled, onOpenChange]);
152
+ const handleTriggerLongPress = useCallback(
153
+ (event?: GestureResponderEvent) => {
154
+ if (disabled) return;
155
+ const { pageX, pageY } = event?.nativeEvent ?? {};
156
+ setPressPoint(
157
+ typeof pageX === "number" && typeof pageY === "number" ? { x: pageX, y: pageY } : null,
158
+ );
159
+ onOpenChange(true);
160
+ },
161
+ [disabled, onOpenChange],
162
+ );
143
163
  const isContextMenu = openOn === "contextmenu";
164
+ const atPoint = isContextMenu && pressPoint !== null && windowWidth > OVERLAY_BREAKPOINT;
144
165
 
145
166
  const handleBackdropPress = useCallback(() => {
146
167
  if (backdropArmed) close();
@@ -480,96 +501,193 @@ export function FloatingPanel({
480
501
  onNativeShow?.();
481
502
  }}
482
503
  >
483
- {/*
504
+ {atPoint && pressPoint ? (
505
+ <PointPanel
506
+ key={`${pressPoint.x},${pressPoint.y}`}
507
+ point={pressPoint}
508
+ bounds={{
509
+ top: topInset + panelViewportPadding,
510
+ left: (safeArea?.left ?? 0) + panelViewportPadding,
511
+ right: windowWidth - (safeArea?.right ?? 0) - panelViewportPadding,
512
+ bottom:
513
+ windowHeight -
514
+ Math.max(safeArea?.bottom ?? 0, keyboardInset) -
515
+ panelViewportPadding,
516
+ }}
517
+ framePadding={framePadding}
518
+ frameGap={frameGap}
519
+ onBackdropPress={handleBackdropPress}
520
+ onEscape={close}
521
+ >
522
+ {header}
523
+ {children}
524
+ </PointPanel>
525
+ ) : (
526
+ /*
484
527
  A plain padded column, not KeyboardAvoidingView: inside a Modal the
485
528
  latter pads the container while the sheet keeps its own fixed height,
486
529
  so the sheet overflows off the TOP instead of shrinking (MPO-64).
487
- */}
488
- <RNView style={{ flex: 1, justifyContent: "flex-end", paddingBottom: keyboardInset }}>
489
- <Pressable
490
- style={StyleSheet.absoluteFill}
491
- onPress={handleBackdropPress}
492
- accessibilityRole="button"
493
- accessibilityLabel={t("Close")}
494
- >
495
- <View flex={1} backgroundColor="$shadow6" />
496
- </Pressable>
497
- {/*
530
+ */
531
+ <RNView style={{ flex: 1, justifyContent: "flex-end", paddingBottom: keyboardInset }}>
532
+ <Pressable
533
+ style={StyleSheet.absoluteFill}
534
+ onPress={handleBackdropPress}
535
+ accessibilityRole="button"
536
+ accessibilityLabel={t("Close")}
537
+ >
538
+ <View flex={1} backgroundColor="$shadow6" />
539
+ </Pressable>
540
+ {/*
498
541
  Sheet sits above the absoluteFill backdrop in z-order (later sibling).
499
542
  Do NOT claim the responder on a wrapper here — that steals presses
500
543
  from option rows / calendar days and blocks commit.
501
544
  */}
502
- <Animated.View
503
- testID="floating-panel-sheet"
504
- onLayout={(e) => {
505
- // Travel distance for a dismiss, and the basis for the detents.
506
- // A content-sized picker is shorter than maxHeight, and sliding
507
- // it by maxHeight would overshoot into a visible lag.
508
- if (restHeightRef.current !== null) return;
509
- const h = Math.round(e.nativeEvent.layout.height);
510
- if (h <= 0) return;
511
- naturalRef.current = h;
512
- filledRef.current = h >= maxHeightRef.current - 1;
513
- if (h !== measuredHeight) setMeasuredHeight(h);
514
- }}
515
- style={{
516
- // MPO-64: a flex child defaults to flexShrink 0, so without this
517
- // the sheet cannot give way to the keyboard at all.
518
- flexShrink: 1,
519
- minHeight: 0,
520
- maxHeight: frameCap,
521
- transform: [{ translateY }],
522
- }}
523
- >
524
- <YStack
525
- {...knobProps.elevatedSurface}
526
- borderBottomLeftRadius={0}
527
- borderBottomRightRadius={0}
528
- maxHeight={maxHeight}
529
- flexShrink={1}
530
- minHeight={0}
531
- // sheetFill opens at full sheet height (flexShrink keeps the
532
- // keyboard from pushing the top of the sheet off-screen).
533
- {...(sheetFill ? { height: maxHeight } : undefined)}
534
- paddingTop="$2"
535
- paddingBottom={sheetBottomPadding}
536
- // VoiceOver escape gesture (two-finger Z) dismisses the sheet.
537
- onAccessibilityEscape={close}
545
+ <Animated.View
546
+ testID="floating-panel-sheet"
547
+ onLayout={(e) => {
548
+ // Travel distance for a dismiss, and the basis for the detents.
549
+ // A content-sized picker is shorter than maxHeight, and sliding
550
+ // it by maxHeight would overshoot into a visible lag.
551
+ if (restHeightRef.current !== null) return;
552
+ const h = Math.round(e.nativeEvent.layout.height);
553
+ if (h <= 0) return;
554
+ naturalRef.current = h;
555
+ filledRef.current = h >= maxHeightRef.current - 1;
556
+ if (h !== measuredHeight) setMeasuredHeight(h);
557
+ }}
558
+ style={{
559
+ // MPO-64: a flex child defaults to flexShrink 0, so without this
560
+ // the sheet cannot give way to the keyboard at all.
561
+ flexShrink: 1,
562
+ minHeight: 0,
563
+ maxHeight: frameCap,
564
+ transform: [{ translateY }],
565
+ }}
538
566
  >
539
- {/*
567
+ <YStack
568
+ {...knobProps.elevatedSurface}
569
+ borderBottomLeftRadius={0}
570
+ borderBottomRightRadius={0}
571
+ maxHeight={maxHeight}
572
+ flexShrink={1}
573
+ minHeight={0}
574
+ // sheetFill opens at full sheet height (flexShrink keeps the
575
+ // keyboard from pushing the top of the sheet off-screen).
576
+ {...(sheetFill ? { height: maxHeight } : undefined)}
577
+ paddingTop="$2"
578
+ paddingBottom={sheetBottomPadding}
579
+ // VoiceOver escape gesture (two-finger Z) dismisses the sheet.
580
+ onAccessibilityEscape={close}
581
+ >
582
+ {/*
540
583
  The grab ZONE, not just the 4pt pill: a 4pt tall target is
541
584
  unhittable. The gesture lives here rather than on the whole
542
585
  sheet so it never fights the ScrollView inside it, which is the
543
586
  same division iOS uses.
544
587
  */}
545
- <RNView
546
- {...panResponder.panHandlers}
547
- accessibilityRole="adjustable"
548
- accessibilityLabel={t("Drag to resize")}
549
- style={{
550
- alignSelf: "stretch",
551
- alignItems: "center",
552
- paddingTop: 4,
553
- paddingBottom: 12,
554
- }}
555
- >
556
- <View width={40} height={4} borderRadius={2} backgroundColor="$color8" />
557
- </RNView>
558
- <YStack
559
- padding={framePadding}
560
- gap={frameGap}
561
- flexShrink={1}
562
- {...(sheetFill ? { flex: 1, minHeight: 0 } : undefined)}
563
- >
564
- <Surface size="md">
565
- {header}
566
- {content}
567
- </Surface>
588
+ <RNView
589
+ {...panResponder.panHandlers}
590
+ accessibilityRole="adjustable"
591
+ accessibilityLabel={t("Drag to resize")}
592
+ style={{
593
+ alignSelf: "stretch",
594
+ alignItems: "center",
595
+ paddingTop: 4,
596
+ paddingBottom: 12,
597
+ }}
598
+ >
599
+ <View width={40} height={4} borderRadius={2} backgroundColor="$color8" />
600
+ </RNView>
601
+ <YStack
602
+ padding={framePadding}
603
+ gap={frameGap}
604
+ flexShrink={1}
605
+ {...(sheetFill ? { flex: 1, minHeight: 0 } : undefined)}
606
+ >
607
+ <Surface size="md">
608
+ {header}
609
+ {content}
610
+ </Surface>
611
+ </YStack>
568
612
  </YStack>
569
- </YStack>
570
- </Animated.View>
571
- </RNView>
613
+ </Animated.View>
614
+ </RNView>
615
+ )}
572
616
  </Modal>
573
617
  </>
574
618
  );
575
619
  }
620
+
621
+ /**
622
+ * The menu at the long-press point: measured once per open at its natural
623
+ * size, then placed by the same rule as the web pointer menu. Later layouts
624
+ * are its own capped height and must not re-decide the side it opened on.
625
+ */
626
+ function PointPanel({
627
+ point,
628
+ bounds,
629
+ framePadding,
630
+ frameGap,
631
+ onBackdropPress,
632
+ onEscape,
633
+ children,
634
+ }: {
635
+ point: PanelPoint;
636
+ bounds: PanelBounds;
637
+ framePadding: string;
638
+ frameGap: string | undefined;
639
+ onBackdropPress: () => void;
640
+ onEscape: () => void;
641
+ children: ReactNode;
642
+ }) {
643
+ const { knobProps } = useResolvedKnobs();
644
+ const [size, setSize] = useState<{ width: number; height: number } | null>(null);
645
+ const placement = size
646
+ ? placeAtPoint({
647
+ point,
648
+ width: size.width,
649
+ height: size.height,
650
+ bounds,
651
+ rtl: I18nManager.isRTL,
652
+ })
653
+ : null;
654
+ return (
655
+ <RNView style={StyleSheet.absoluteFill}>
656
+ <Pressable
657
+ style={StyleSheet.absoluteFill}
658
+ onPress={onBackdropPress}
659
+ accessibilityRole="button"
660
+ accessibilityLabel={t("Close")}
661
+ />
662
+ <RNView
663
+ testID="floating-panel-point"
664
+ onLayout={(e) => {
665
+ if (size) return;
666
+ const { width, height } = e.nativeEvent.layout;
667
+ if (width > 0 && height > 0) setSize({ width, height });
668
+ }}
669
+ style={{
670
+ position: "absolute",
671
+ left: placement?.left ?? bounds.left,
672
+ top: placement?.top ?? bounds.top,
673
+ maxWidth: bounds.right - bounds.left,
674
+ maxHeight: placement?.maxHeight ?? bounds.bottom - bounds.top,
675
+ opacity: placement ? 1 : 0,
676
+ }}
677
+ >
678
+ <YStack
679
+ {...knobProps.elevatedSurface}
680
+ padding={framePadding}
681
+ gap={frameGap}
682
+ flexShrink={1}
683
+ minHeight={0}
684
+ onAccessibilityEscape={onEscape}
685
+ >
686
+ <RNScrollView style={{ flexGrow: 0, flexShrink: 1 }} keyboardShouldPersistTaps="handled">
687
+ {children}
688
+ </RNScrollView>
689
+ </YStack>
690
+ </RNView>
691
+ </RNView>
692
+ );
693
+ }
@@ -8,6 +8,7 @@ import {
8
8
  useSyncExternalStore,
9
9
  } from "react";
10
10
  import {
11
+ OVERLAY_ANCHOR_GAP,
11
12
  OVERLAY_ATTACH_GAP,
12
13
  OVERLAY_BREAKPOINT,
13
14
  useResolvedKnobs,
@@ -24,6 +25,14 @@ import type { PanelWidthMode } from "./useFloatingPanel";
24
25
  import { zIndex } from "../shared/zIndex";
25
26
  import { isNestedPanelAction, type PanelActionEvent } from "./nestedAction";
26
27
  import { PanelPortal } from "./PanelPortal";
28
+ import {
29
+ contextMenuPoint,
30
+ pointOpensUp,
31
+ pointPanelLeft,
32
+ referenceCornerPoint,
33
+ type ContextMenuEventLike,
34
+ type PanelPoint,
35
+ } from "./pointAnchor";
27
36
 
28
37
  type PointerOpenEvent = PanelActionEvent & { button?: number; target?: unknown };
29
38
 
@@ -152,7 +161,10 @@ export interface FloatingPanelProps {
152
161
  * How the trigger opens the panel. `"press"` (default) is click/tap.
153
162
  * `"contextmenu"` is the ContextMenu gesture: web `contextmenu` (right-click
154
163
  * / Shift+F10), native long-press. Primary click does not open. Same overlay
155
- * stack — not a second positioning engine.
164
+ * stack — not a second positioning engine. The floating panel opens AT the
165
+ * gesture's point as a free overlay (`OVERLAY_ANCHOR_GAP` from it, flipping
166
+ * at the viewport edges); a keyboard open has no point and opens from the
167
+ * trigger's bottom start corner.
156
168
  */
157
169
  openOn?: "press" | "contextmenu";
158
170
  }
@@ -202,15 +214,15 @@ function useKeyboardFocusIntoPanel(
202
214
  }
203
215
 
204
216
  /**
205
- * Viewport y of the box a `position: fixed` panel is laid out against. The
206
- * top math above is in viewport coordinates, which is only right while that
217
+ * Viewport origin of the box a `position: fixed` panel is laid out against.
218
+ * The top/left math is in viewport coordinates, which is only right while that
207
219
  * box is the viewport. An ancestor with a transform, filter, perspective,
208
220
  * paint containment or container query becomes the containing block instead.
209
221
  * PanelPortal lifts the panel out of scroll views, but a panel hosted in a
210
222
  * transformed dialog still lays out against that dialog. A `display: contents`
211
223
  * node has no box, so its computed `contain` (Tamagui's theme span) is ignored.
212
224
  */
213
- function fixedContainingBlockTop(element: HTMLElement): number {
225
+ function fixedContainingBlockOrigin(element: HTMLElement): { top: number; left: number } {
214
226
  for (let node = element.parentElement; node; node = node.parentElement) {
215
227
  const cs = getComputedStyle(node);
216
228
  if (cs.display === "contents") continue;
@@ -223,16 +235,27 @@ function fixedContainingBlockTop(element: HTMLElement): number {
223
235
  set(cs.containerType, "normal") ||
224
236
  /transform|perspective|filter/.test(cs.willChange) ||
225
237
  /paint|layout|strict|content/.test(cs.contain);
226
- if (captures) return node.getBoundingClientRect().top + node.clientTop - node.scrollTop;
238
+ if (captures) {
239
+ const rect = node.getBoundingClientRect();
240
+ return {
241
+ top: rect.top + node.clientTop - node.scrollTop,
242
+ left: rect.left + node.clientLeft - node.scrollLeft,
243
+ };
244
+ }
227
245
  }
228
- return 0;
246
+ return { top: 0, left: 0 };
229
247
  }
230
248
 
231
- function panelTransformOrigin(placement: string): string {
232
- const rtl =
249
+ function isRtl(): boolean {
250
+ return (
233
251
  isWeb &&
234
252
  typeof document !== "undefined" &&
235
- document.documentElement.getAttribute("dir") === "rtl";
253
+ document.documentElement.getAttribute("dir") === "rtl"
254
+ );
255
+ }
256
+
257
+ function panelTransformOrigin(placement: string): string {
258
+ const rtl = isRtl();
236
259
  const atStart = !placement.endsWith("end");
237
260
  const originX = atStart === !rtl ? "left" : "right";
238
261
  const originY = placement.startsWith("top") ? "bottom" : "top";
@@ -457,7 +480,8 @@ function FloatingPanelWeb({
457
480
  openOn = "press",
458
481
  }: FloatingPanelProps) {
459
482
  const isFill = sizing === "fill";
460
- const overlayStyleRef = useRef<{ top: number; maxHeight: number } | null>(null);
483
+ const isContextMenu = openOn === "contextmenu";
484
+ const overlayStyleRef = useRef<{ top: number; left?: number; maxHeight: number } | null>(null);
461
485
  const pinnedTopRef = useRef(0);
462
486
  const pinnedHeightRef = useRef(0);
463
487
  const grownRef = useRef(false);
@@ -466,27 +490,33 @@ function FloatingPanelWeb({
466
490
  const lastScrollTopRef = useRef(0);
467
491
  const keyboardOpenRef = useRef(false);
468
492
  const contentRef = useRef<HTMLElement | null>(null);
493
+ const anchorPointRef = useRef<PanelPoint | null>(null);
494
+ const pointPlacementRef = useRef<{ dropup: boolean; alignEnd: boolean } | null>(null);
469
495
 
470
496
  const repositionFnRef = useRef<(() => void) | null>(null);
471
497
 
498
+ const resetPosition = () => {
499
+ overlayStyleRef.current = null;
500
+ pinnedTopRef.current = 0;
501
+ pinnedHeightRef.current = 0;
502
+ grownRef.current = false;
503
+ growUpOnlyRef.current = false;
504
+ growDownOnlyRef.current = false;
505
+ lastScrollTopRef.current = 0;
506
+ pointPlacementRef.current = null;
507
+ };
508
+
472
509
  const panel = useFloatingPanel({
473
510
  open,
474
511
  onOpenChange,
475
512
  disabled,
476
513
  repositionRef: repositionFnRef,
477
514
  wheelGrowRef: isFill ? undefined : grownRef,
478
- widthMode,
515
+ widthMode: widthMode ?? (isContextMenu && fitContent === undefined ? "fit-content" : undefined),
479
516
  fitContent,
480
517
  compact,
481
- onBeforeOpen: () => {
482
- overlayStyleRef.current = null;
483
- pinnedTopRef.current = 0;
484
- pinnedHeightRef.current = 0;
485
- grownRef.current = false;
486
- growUpOnlyRef.current = false;
487
- growDownOnlyRef.current = false;
488
- lastScrollTopRef.current = 0;
489
- },
518
+ freeOverlay: isContextMenu,
519
+ onBeforeOpen: resetPosition,
490
520
  });
491
521
 
492
522
  // DG-A11Y-04 / Axiom 7: floating-ui useDismiss has no focus manager — when
@@ -496,7 +526,9 @@ function FloatingPanelWeb({
496
526
  useReturnFocusOnClose(open, skipReturnFocusRef);
497
527
  useKeyboardFocusIntoPanel(open && panel.mounted, panel.refs.floating, keyboardOpenRef);
498
528
  useEffect(() => {
499
- if (!open) keyboardOpenRef.current = false;
529
+ if (open) return;
530
+ keyboardOpenRef.current = false;
531
+ anchorPointRef.current = null;
500
532
  }, [open]);
501
533
 
502
534
  const repositionOverlay = useCallback(() => {
@@ -511,7 +543,10 @@ function FloatingPanelWeb({
511
543
  dropdownElement.scrollTop = 0;
512
544
  }
513
545
 
514
- const triggerBounds = trig.getBoundingClientRect();
546
+ const point = isContextMenu
547
+ ? (anchorPointRef.current ?? referenceCornerPoint(trig, isRtl()))
548
+ : null;
549
+ const triggerBounds = point ? { top: point.y, bottom: point.y } : trig.getBoundingClientRect();
515
550
  const viewportHeight = window.innerHeight;
516
551
  const cs = getComputedStyle(dropdownElement);
517
552
  const borderTopWidth = parseFloat(cs.borderTopWidth) || 0;
@@ -520,13 +555,39 @@ function FloatingPanelWeb({
520
555
  const scrollContentHeight = dropdownElement.scrollHeight;
521
556
  const totalContentHeight = scrollContentHeight + totalBorderWidth;
522
557
  const liveTransition = panel.knobProps.transition ? panelTransition : "none";
523
-
524
- const isDropup = panel.placement.startsWith("top");
558
+ const containingBlock = fixedContainingBlockOrigin(dropdownElement);
559
+
560
+ let isDropup = panel.placement.startsWith("top");
561
+ let cssLeft: number | undefined;
562
+ if (point) {
563
+ const bounds = {
564
+ top: panelViewportPadding,
565
+ left: panelViewportPadding,
566
+ right: (document.documentElement.clientWidth || window.innerWidth) - panelViewportPadding,
567
+ bottom: viewportHeight - panelViewportPadding,
568
+ };
569
+ // Measured at the viewport's left edge: a fixed box near the right edge
570
+ // shrinks to the room left of it, and that is not the menu's width.
571
+ // offsetWidth, not the client rect: the opening frame is scaled 0.96.
572
+ dropdownElement.style.left = `${-containingBlock.left}px`;
573
+ const width = dropdownElement.offsetWidth;
574
+ const { left, alignEnd } = pointPanelLeft(point, width, bounds, isRtl());
575
+ cssLeft = left - containingBlock.left;
576
+ dropdownElement.style.left = `${cssLeft}px`;
577
+ pointPlacementRef.current = {
578
+ dropup:
579
+ pointPlacementRef.current?.dropup ??
580
+ pointOpensUp(point, totalContentHeight, bounds, OVERLAY_ANCHOR_GAP),
581
+ alignEnd,
582
+ };
583
+ isDropup = pointPlacementRef.current.dropup;
584
+ }
525
585
  // Cover ruling (MPO-38): the panel ATTACHES to the trigger it was opened
526
586
  // from. `OVERLAY_ATTACH_GAP` is 0, so the panel's top edge coincides with
527
587
  // the trigger's bottom edge (and its bottom with the trigger's top when
528
- // dropping up) instead of floating 4px clear of it.
529
- const gap = OVERLAY_ATTACH_GAP;
588
+ // dropping up) instead of floating 4px clear of it. A context menu opened
589
+ // at a point is a FREE overlay and keeps `OVERLAY_ANCHOR_GAP` from it.
590
+ const gap = point ? OVERLAY_ANCHOR_GAP : OVERLAY_ATTACH_GAP;
530
591
  const belowTop = triggerBounds.bottom + gap;
531
592
  const aboveBottom = triggerBounds.top - gap;
532
593
 
@@ -537,14 +598,19 @@ function FloatingPanelWeb({
537
598
  let height = Math.min(totalContentHeight, Math.max(0, available));
538
599
  height = Math.max(Math.min(120, Math.max(0, available)), height);
539
600
  const top = isDropup ? aboveBottom - height : belowTop;
540
- const cssTop = top - fixedContainingBlockTop(dropdownElement);
601
+ const cssTop = top - containingBlock.top;
541
602
 
542
603
  const cached = overlayStyleRef.current;
543
- if (!cached || cached.top !== cssTop || cached.maxHeight !== height) {
604
+ if (
605
+ !cached ||
606
+ cached.top !== cssTop ||
607
+ cached.left !== cssLeft ||
608
+ cached.maxHeight !== height
609
+ ) {
544
610
  dropdownElement.style.top = `${cssTop}px`;
545
611
  dropdownElement.style.maxHeight = `${height}px`;
546
612
  dropdownElement.style.height = "";
547
- overlayStyleRef.current = { top: cssTop, maxHeight: height };
613
+ overlayStyleRef.current = { top: cssTop, left: cssLeft, maxHeight: height };
548
614
  void dropdownElement.offsetHeight;
549
615
  }
550
616
 
@@ -611,13 +677,18 @@ function FloatingPanelWeb({
611
677
  }
612
678
  }
613
679
 
614
- const cssTop = top - fixedContainingBlockTop(dropdownElement);
680
+ const cssTop = top - containingBlock.top;
615
681
  const cached = overlayStyleRef.current;
616
- if (!cached || cached.top !== cssTop || cached.maxHeight !== height) {
682
+ if (
683
+ !cached ||
684
+ cached.top !== cssTop ||
685
+ cached.left !== cssLeft ||
686
+ cached.maxHeight !== height
687
+ ) {
617
688
  dropdownElement.style.top = `${cssTop}px`;
618
689
  dropdownElement.style.maxHeight = `${height}px`;
619
690
  dropdownElement.style.height = "";
620
- overlayStyleRef.current = { top: cssTop, maxHeight: height };
691
+ overlayStyleRef.current = { top: cssTop, left: cssLeft, maxHeight: height };
621
692
  void dropdownElement.offsetHeight;
622
693
  }
623
694
 
@@ -634,6 +705,7 @@ function FloatingPanelWeb({
634
705
  });
635
706
  }, [
636
707
  isFill,
708
+ isContextMenu,
637
709
  panel.placement,
638
710
  panel.refs,
639
711
  panel.triggerRef,
@@ -643,7 +715,6 @@ function FloatingPanelWeb({
643
715
 
644
716
  repositionFnRef.current = repositionOverlay;
645
717
 
646
- const isContextMenu = openOn === "contextmenu";
647
718
  const click = useClick(panel.context, {
648
719
  event: "mousedown",
649
720
  enabled: !isContextMenu,
@@ -756,14 +827,27 @@ function FloatingPanelWeb({
756
827
  [disabled, isContextMenu, onOpenChange],
757
828
  );
758
829
 
759
- const openFromContextMenu = useCallback(
760
- (event: { preventDefault: () => void }) => {
761
- event.preventDefault();
762
- if (disabled) return;
763
- onOpenChange(true);
830
+ const openFromContextMenu = (
831
+ event: ContextMenuEventLike & {
832
+ preventDefault: () => void;
833
+ nativeEvent?: ContextMenuEventLike;
764
834
  },
765
- [disabled, onOpenChange],
766
- );
835
+ ) => {
836
+ event.preventDefault();
837
+ if (disabled) return;
838
+ anchorPointRef.current = contextMenuPoint(
839
+ event.nativeEvent ?? event,
840
+ panel.triggerRef.current,
841
+ isRtl(),
842
+ );
843
+ if (!open) {
844
+ onOpenChange(true);
845
+ return;
846
+ }
847
+ resetPosition();
848
+ panel.initializingRef.current = true;
849
+ repositionOverlay();
850
+ };
767
851
 
768
852
  // useClick's mousedown opener calls preventDefault, so a pointer open never
769
853
  // focuses the trigger the way a pressed button is focused. The render that
@@ -827,7 +911,7 @@ function FloatingPanelWeb({
827
911
  ...elevatedSurfaceStyle,
828
912
  position: panel.strategy,
829
913
  top: overlayStyleRef.current?.top ?? panel.y ?? 0,
830
- left: panel.x ?? 0,
914
+ left: overlayStyleRef.current?.left ?? panel.x ?? 0,
831
915
  scrollbarWidth: "none" as any,
832
916
  zIndex: zIndex.dropdown,
833
917
  overflow: "auto",
@@ -841,7 +925,11 @@ function FloatingPanelWeb({
841
925
  // then positions against this box and is clipped by its scroll.
842
926
  transform: panel.visible ? "none" : "scale(0.96)",
843
927
  transition: webTransition,
844
- transformOrigin: panelTransformOrigin(panel.placement),
928
+ transformOrigin: panelTransformOrigin(
929
+ pointPlacementRef.current
930
+ ? `${pointPlacementRef.current.dropup ? "top" : "bottom"}-${pointPlacementRef.current.alignEnd ? "end" : "start"}`
931
+ : panel.placement,
932
+ ),
845
933
  },
846
934
  onScroll: handleScroll,
847
935
  onTransitionEnd: (event) => {