@zealakit/overlay-react 0.0.0-stage → 1.0.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 (185) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +547 -2
  3. package/dist/animation/index.d.ts +1 -0
  4. package/dist/animation/index.js +1 -0
  5. package/dist/animation/presence.d.ts +31 -0
  6. package/dist/animation/presence.js +104 -0
  7. package/dist/components/arrow.d.ts +30 -0
  8. package/dist/components/arrow.js +58 -0
  9. package/dist/components/context-menu/content.d.ts +37 -0
  10. package/dist/components/context-menu/content.js +52 -0
  11. package/dist/components/context-menu/context-menu.d.ts +20 -0
  12. package/dist/components/context-menu/context-menu.js +78 -0
  13. package/dist/components/context-menu/context.d.ts +47 -0
  14. package/dist/components/context-menu/context.js +27 -0
  15. package/dist/components/context-menu/index.d.ts +7 -0
  16. package/dist/components/context-menu/index.js +6 -0
  17. package/dist/components/context-menu/items.d.ts +71 -0
  18. package/dist/components/context-menu/items.js +207 -0
  19. package/dist/components/context-menu/menu-navigation.d.ts +3 -0
  20. package/dist/components/context-menu/menu-navigation.js +42 -0
  21. package/dist/components/context-menu/menu-surface.d.ts +58 -0
  22. package/dist/components/context-menu/menu-surface.js +172 -0
  23. package/dist/components/context-menu/sub.d.ts +45 -0
  24. package/dist/components/context-menu/sub.js +180 -0
  25. package/dist/components/context-menu/trigger.d.ts +17 -0
  26. package/dist/components/context-menu/trigger.js +62 -0
  27. package/dist/components/context-menu/types.d.ts +9 -0
  28. package/dist/components/context-menu/types.js +1 -0
  29. package/dist/components/dialog/content.d.ts +87 -0
  30. package/dist/components/dialog/content.js +238 -0
  31. package/dist/components/dialog/context.d.ts +41 -0
  32. package/dist/components/dialog/context.js +26 -0
  33. package/dist/components/dialog/dialog.d.ts +27 -0
  34. package/dist/components/dialog/dialog.js +88 -0
  35. package/dist/components/dialog/helpers.d.ts +24 -0
  36. package/dist/components/dialog/helpers.js +59 -0
  37. package/dist/components/dialog/index.d.ts +5 -0
  38. package/dist/components/dialog/index.js +5 -0
  39. package/dist/components/dialog/modal-background.d.ts +7 -0
  40. package/dist/components/dialog/modal-background.js +21 -0
  41. package/dist/components/dialog/trigger.d.ts +11 -0
  42. package/dist/components/dialog/trigger.js +29 -0
  43. package/dist/components/overlay-context.d.ts +13 -0
  44. package/dist/components/overlay-context.js +10 -0
  45. package/dist/components/popover/content.d.ts +131 -0
  46. package/dist/components/popover/content.js +261 -0
  47. package/dist/components/popover/context.d.ts +27 -0
  48. package/dist/components/popover/context.js +18 -0
  49. package/dist/components/popover/helpers.d.ts +23 -0
  50. package/dist/components/popover/helpers.js +58 -0
  51. package/dist/components/popover/index.d.ts +4 -0
  52. package/dist/components/popover/index.js +4 -0
  53. package/dist/components/popover/popover.d.ts +39 -0
  54. package/dist/components/popover/popover.js +98 -0
  55. package/dist/components/portal.d.ts +14 -0
  56. package/dist/components/portal.js +19 -0
  57. package/dist/components/slot.d.ts +12 -0
  58. package/dist/components/slot.js +60 -0
  59. package/dist/components/tooltip/content.d.ts +21 -0
  60. package/dist/components/tooltip/content.js +95 -0
  61. package/dist/components/tooltip/context.d.ts +22 -0
  62. package/dist/components/tooltip/context.js +10 -0
  63. package/dist/components/tooltip/index.d.ts +5 -0
  64. package/dist/components/tooltip/index.js +5 -0
  65. package/dist/components/tooltip/provider.d.ts +29 -0
  66. package/dist/components/tooltip/provider.js +29 -0
  67. package/dist/components/tooltip/tooltip.d.ts +25 -0
  68. package/dist/components/tooltip/tooltip.js +268 -0
  69. package/dist/components/tooltip/trigger.d.ts +11 -0
  70. package/dist/components/tooltip/trigger.js +67 -0
  71. package/dist/hooks/use-escape-key.d.ts +12 -0
  72. package/dist/hooks/use-escape-key.js +30 -0
  73. package/dist/hooks/use-layer.d.ts +33 -0
  74. package/dist/hooks/use-layer.js +84 -0
  75. package/dist/hooks/use-overlay.d.ts +31 -0
  76. package/dist/hooks/use-overlay.js +69 -0
  77. package/dist/hooks/use-positioner.d.ts +28 -0
  78. package/dist/hooks/use-positioner.js +127 -0
  79. package/dist/index.d.ts +12 -0
  80. package/dist/index.js +12 -0
  81. package/dist/vendor/core/animation/index.d.ts +2 -0
  82. package/dist/vendor/core/animation/index.js +2 -0
  83. package/dist/vendor/core/animation/keyframes.d.ts +56 -0
  84. package/dist/vendor/core/animation/keyframes.js +155 -0
  85. package/dist/vendor/core/animation/run-animation.d.ts +27 -0
  86. package/dist/vendor/core/animation/run-animation.js +175 -0
  87. package/dist/vendor/core/controller/animator.d.ts +19 -0
  88. package/dist/vendor/core/controller/animator.js +117 -0
  89. package/dist/vendor/core/controller/context-menu-behavior.d.ts +15 -0
  90. package/dist/vendor/core/controller/context-menu-behavior.js +123 -0
  91. package/dist/vendor/core/controller/host.d.ts +10 -0
  92. package/dist/vendor/core/controller/host.js +1 -0
  93. package/dist/vendor/core/controller/index.d.ts +2 -0
  94. package/dist/vendor/core/controller/index.js +1 -0
  95. package/dist/vendor/core/controller/overlay-controller.d.ts +9 -0
  96. package/dist/vendor/core/controller/overlay-controller.js +723 -0
  97. package/dist/vendor/core/controller/tooltip-behavior.d.ts +9 -0
  98. package/dist/vendor/core/controller/tooltip-behavior.js +123 -0
  99. package/dist/vendor/core/controller/types.d.ts +92 -0
  100. package/dist/vendor/core/controller/types.js +1 -0
  101. package/dist/vendor/core/events/index.d.ts +3 -0
  102. package/dist/vendor/core/events/index.js +3 -0
  103. package/dist/vendor/core/events/keyboard.d.ts +2 -0
  104. package/dist/vendor/core/events/keyboard.js +4 -0
  105. package/dist/vendor/core/events/outside.d.ts +8 -0
  106. package/dist/vendor/core/events/outside.js +33 -0
  107. package/dist/vendor/core/events/pointer.d.ts +13 -0
  108. package/dist/vendor/core/events/pointer.js +18 -0
  109. package/dist/vendor/core/focus/focus.d.ts +73 -0
  110. package/dist/vendor/core/focus/focus.js +312 -0
  111. package/dist/vendor/core/focus/index.d.ts +1 -0
  112. package/dist/vendor/core/focus/index.js +1 -0
  113. package/dist/vendor/core/index.d.ts +13 -0
  114. package/dist/vendor/core/index.js +13 -0
  115. package/dist/vendor/core/layer/create-layer-stack.d.ts +25 -0
  116. package/dist/vendor/core/layer/create-layer-stack.js +247 -0
  117. package/dist/vendor/core/layer/create-layer.d.ts +10 -0
  118. package/dist/vendor/core/layer/create-layer.js +78 -0
  119. package/dist/vendor/core/layer/index.d.ts +3 -0
  120. package/dist/vendor/core/layer/index.js +3 -0
  121. package/dist/vendor/core/layer/manager.d.ts +9 -0
  122. package/dist/vendor/core/layer/manager.js +157 -0
  123. package/dist/vendor/core/lifecycle/events.d.ts +8 -0
  124. package/dist/vendor/core/lifecycle/events.js +21 -0
  125. package/dist/vendor/core/lifecycle/index.d.ts +1 -0
  126. package/dist/vendor/core/lifecycle/index.js +1 -0
  127. package/dist/vendor/core/menu/index.d.ts +1 -0
  128. package/dist/vendor/core/menu/index.js +1 -0
  129. package/dist/vendor/core/menu/navigation.d.ts +22 -0
  130. package/dist/vendor/core/menu/navigation.js +107 -0
  131. package/dist/vendor/core/modal/background.d.ts +10 -0
  132. package/dist/vendor/core/modal/background.js +115 -0
  133. package/dist/vendor/core/modal/index.d.ts +1 -0
  134. package/dist/vendor/core/modal/index.js +1 -0
  135. package/dist/vendor/core/portal/index.d.ts +1 -0
  136. package/dist/vendor/core/portal/index.js +1 -0
  137. package/dist/vendor/core/portal/portal.d.ts +13 -0
  138. package/dist/vendor/core/portal/portal.js +25 -0
  139. package/dist/vendor/core/positioning/arrow.d.ts +34 -0
  140. package/dist/vendor/core/positioning/arrow.js +142 -0
  141. package/dist/vendor/core/positioning/compute.d.ts +9 -0
  142. package/dist/vendor/core/positioning/compute.js +22 -0
  143. package/dist/vendor/core/positioning/coordinates.d.ts +11 -0
  144. package/dist/vendor/core/positioning/coordinates.js +45 -0
  145. package/dist/vendor/core/positioning/create-positioner.d.ts +8 -0
  146. package/dist/vendor/core/positioning/create-positioner.js +251 -0
  147. package/dist/vendor/core/positioning/index.d.ts +6 -0
  148. package/dist/vendor/core/positioning/index.js +6 -0
  149. package/dist/vendor/core/positioning/placement.d.ts +8 -0
  150. package/dist/vendor/core/positioning/placement.js +30 -0
  151. package/dist/vendor/core/positioning/same-result.d.ts +8 -0
  152. package/dist/vendor/core/positioning/same-result.js +13 -0
  153. package/dist/vendor/core/positioning/strategy.d.ts +12 -0
  154. package/dist/vendor/core/positioning/strategy.js +46 -0
  155. package/dist/vendor/core/scroll-lock/index.d.ts +1 -0
  156. package/dist/vendor/core/scroll-lock/index.js +1 -0
  157. package/dist/vendor/core/scroll-lock/scroll-lock.d.ts +12 -0
  158. package/dist/vendor/core/scroll-lock/scroll-lock.js +68 -0
  159. package/dist/vendor/core/types/animation.d.ts +81 -0
  160. package/dist/vendor/core/types/animation.js +93 -0
  161. package/dist/vendor/core/types/dismissal.d.ts +2 -0
  162. package/dist/vendor/core/types/dismissal.js +1 -0
  163. package/dist/vendor/core/types/element.d.ts +20 -0
  164. package/dist/vendor/core/types/element.js +1 -0
  165. package/dist/vendor/core/types/geometry.d.ts +22 -0
  166. package/dist/vendor/core/types/geometry.js +1 -0
  167. package/dist/vendor/core/types/index.d.ts +9 -0
  168. package/dist/vendor/core/types/index.js +8 -0
  169. package/dist/vendor/core/types/layer.d.ts +126 -0
  170. package/dist/vendor/core/types/layer.js +1 -0
  171. package/dist/vendor/core/types/lifecycle.d.ts +2 -0
  172. package/dist/vendor/core/types/lifecycle.js +1 -0
  173. package/dist/vendor/core/types/positioner.d.ts +74 -0
  174. package/dist/vendor/core/types/positioner.js +1 -0
  175. package/dist/vendor/core/types/positioning.d.ts +23 -0
  176. package/dist/vendor/core/types/positioning.js +1 -0
  177. package/dist/vendor/core/utils/dom.d.ts +11 -0
  178. package/dist/vendor/core/utils/dom.js +21 -0
  179. package/dist/vendor/core/utils/element.d.ts +15 -0
  180. package/dist/vendor/core/utils/element.js +21 -0
  181. package/dist/vendor/core/utils/global-state.d.ts +1 -0
  182. package/dist/vendor/core/utils/global-state.js +24 -0
  183. package/dist/vendor/core/utils/index.d.ts +2 -0
  184. package/dist/vendor/core/utils/index.js +2 -0
  185. package/package.json +74 -4
@@ -0,0 +1,142 @@
1
+ import { parsePlacement } from "./placement.js";
2
+ export const DEFAULT_ARROW_SIZE = 8;
3
+ /** Edge of the floating element the arrow sits on (opposite the reference). */
4
+ export function arrowStaticSide(side) {
5
+ switch (side) {
6
+ case "top":
7
+ return "bottom";
8
+ case "right":
9
+ return "left";
10
+ case "bottom":
11
+ return "top";
12
+ default:
13
+ return "right";
14
+ }
15
+ }
16
+ /** Outline drawn on one edge of the rotated square. */
17
+ const ARROW_BORDER = "var(--overlay-arrow-border-width, 0px) solid var(--overlay-arrow-border-color, transparent)";
18
+ /**
19
+ * Per placement side: which two edges of the (unrotated) square become the
20
+ * outer, visible edges after the 45deg rotation, plus the triangle that keeps
21
+ * only the half pointing at the anchor. `clip-path` runs in the square's own
22
+ * coordinates, so these polygons are given before rotation.
23
+ */
24
+ const ARROW_OUTWARD = {
25
+ // Popup above the anchor: arrow on the bottom edge, tip pointing down.
26
+ top: {
27
+ edges: ["Bottom", "Right"],
28
+ clip: "polygon(0 100%, 100% 100%, 100% 0)",
29
+ },
30
+ // Popup below the anchor: tip pointing up.
31
+ bottom: { edges: ["Top", "Left"], clip: "polygon(0 100%, 0 0, 100% 0)" },
32
+ // Popup left of the anchor: arrow on the right edge, tip pointing right.
33
+ left: { edges: ["Top", "Right"], clip: "polygon(0 0, 100% 0, 100% 100%)" },
34
+ // Popup right of the anchor: tip pointing left.
35
+ right: { edges: ["Left", "Bottom"], clip: "polygon(0 0, 0 100%, 100% 100%)" },
36
+ };
37
+ /**
38
+ * Compute arrow geometry for a resolved placement. The arrow is a rotated
39
+ * square clipped to the half that points at the anchor, so it never covers
40
+ * popup content. Only its two outer edges are outlined, which lets the outline
41
+ * continue the popup's own border. The popup controls the look through CSS
42
+ * variables: `--overlay-arrow-background`, `--overlay-arrow-border-width`
43
+ * and `--overlay-arrow-border-color`.
44
+ * Returns `null` while the positioner has no arrow measurement yet.
45
+ */
46
+ export function getArrowStyles({ placement, arrow, size = DEFAULT_ARROW_SIZE, }) {
47
+ if (!arrow) {
48
+ return null;
49
+ }
50
+ const side = parsePlacement(placement).side;
51
+ const outward = ARROW_OUTWARD[side];
52
+ const style = {
53
+ position: "absolute",
54
+ boxSizing: "border-box",
55
+ width: `${size}px`,
56
+ height: `${size}px`,
57
+ pointerEvents: "none",
58
+ backgroundColor: "var(--overlay-arrow-background, inherit)",
59
+ borderTop: "0",
60
+ borderRight: "0",
61
+ borderBottom: "0",
62
+ borderLeft: "0",
63
+ clipPath: outward.clip,
64
+ transform: "rotate(45deg)",
65
+ [arrowStaticSide(side)]: `${-size / 2}px`,
66
+ };
67
+ for (const edge of outward.edges) {
68
+ style[`border${edge}`] = ARROW_BORDER;
69
+ }
70
+ if (arrow.x !== undefined) {
71
+ style.left = `${arrow.x}px`;
72
+ }
73
+ if (arrow.y !== undefined) {
74
+ style.top = `${arrow.y}px`;
75
+ }
76
+ return { side, style };
77
+ }
78
+ const ARROW_PROPERTIES = [
79
+ "position",
80
+ "width",
81
+ "height",
82
+ "pointer-events",
83
+ "background-color",
84
+ "box-sizing",
85
+ "border-top",
86
+ "border-right",
87
+ "border-bottom",
88
+ "border-left",
89
+ "clip-path",
90
+ "transform",
91
+ "top",
92
+ "right",
93
+ "bottom",
94
+ "left",
95
+ "visibility",
96
+ ];
97
+ function kebab(name) {
98
+ return name.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
99
+ }
100
+ /** Capture inline styles so a controller can restore the element later. */
101
+ export function snapshotArrowStyles(element) {
102
+ const snapshot = { side: element.getAttribute("data-side") };
103
+ for (const name of ARROW_PROPERTIES) {
104
+ snapshot[name] = element.style.getPropertyValue(name);
105
+ }
106
+ return snapshot;
107
+ }
108
+ /** Write computed arrow styles and `data-side` to a consumer-owned element. */
109
+ export function applyArrowStyles(element, styles) {
110
+ if (!styles) {
111
+ element.style.visibility = "hidden";
112
+ return;
113
+ }
114
+ // Reset the alignment-axis coordinates so a flip never keeps stale values.
115
+ for (const name of ["top", "right", "bottom", "left"]) {
116
+ element.style.removeProperty(name);
117
+ }
118
+ for (const [name, value] of Object.entries(styles.style)) {
119
+ element.style.setProperty(kebab(name), String(value));
120
+ }
121
+ element.style.visibility = "visible";
122
+ element.setAttribute("data-side", styles.side);
123
+ element.setAttribute("aria-hidden", "true");
124
+ element.setAttribute("data-arrow", "");
125
+ }
126
+ export function resetArrowStyles(element, snapshot) {
127
+ for (const name of ARROW_PROPERTIES) {
128
+ if (snapshot[name]) {
129
+ element.style.setProperty(name, snapshot[name]);
130
+ }
131
+ else {
132
+ element.style.removeProperty(name);
133
+ }
134
+ }
135
+ if (snapshot.side === null) {
136
+ element.removeAttribute("data-side");
137
+ }
138
+ else {
139
+ element.setAttribute("data-side", snapshot.side);
140
+ }
141
+ element.removeAttribute("data-arrow");
142
+ }
@@ -0,0 +1,9 @@
1
+ import type { FloatingElement, ReferenceElement } from "../types/element.js";
2
+ import type { PlacementCoordinates, PlacementOptions } from "../types/positioning.js";
3
+ /**
4
+ * Measure a reference/floating pair and resolve its viewport coordinates.
5
+ *
6
+ * The elements must already be in the DOM; callers run this from effects or
7
+ * actions, never from SSR render paths.
8
+ */
9
+ export declare function computePosition(reference: ReferenceElement, floating: FloatingElement, options?: PlacementOptions): PlacementCoordinates;
@@ -0,0 +1,22 @@
1
+ import { getRect } from "../utils/dom.js";
2
+ import { resolvePlacementCoordinates } from "./coordinates.js";
3
+ /**
4
+ * Measure a reference/floating pair and resolve its viewport coordinates.
5
+ *
6
+ * The elements must already be in the DOM; callers run this from effects or
7
+ * actions, never from SSR render paths.
8
+ */
9
+ export function computePosition(reference, floating, options = {}) {
10
+ const referenceRect = getRect(reference);
11
+ const floatingRect = getRect(floating);
12
+ return resolvePlacementCoordinates({
13
+ reference: referenceRect,
14
+ floating: {
15
+ width: floatingRect.width,
16
+ height: floatingRect.height,
17
+ },
18
+ placement: options.placement,
19
+ offset: options.offset,
20
+ alignOffset: options.alignOffset,
21
+ });
22
+ }
@@ -0,0 +1,11 @@
1
+ import type { PlacementCoordinates, PlacementCoordinatesInput } from "../types/positioning.js";
2
+ export declare const DEFAULT_OFFSET = 0;
3
+ /**
4
+ * Resolve viewport coordinates for a floating element.
5
+ *
6
+ * This is deliberately a single-placement calculation: there is no collision
7
+ * detection, flipping, shifting, or arrow placement yet. Callers that need
8
+ * collision handling must add it above this contract rather than assuming it
9
+ * lives here.
10
+ */
11
+ export declare function resolvePlacementCoordinates(input: PlacementCoordinatesInput): PlacementCoordinates;
@@ -0,0 +1,45 @@
1
+ import { DEFAULT_PLACEMENT, parsePlacement } from "./placement.js";
2
+ export const DEFAULT_OFFSET = 0;
3
+ /**
4
+ * Resolve viewport coordinates for a floating element.
5
+ *
6
+ * This is deliberately a single-placement calculation: there is no collision
7
+ * detection, flipping, shifting, or arrow placement yet. Callers that need
8
+ * collision handling must add it above this contract rather than assuming it
9
+ * lives here.
10
+ */
11
+ export function resolvePlacementCoordinates(input) {
12
+ const { reference, floating, offset = DEFAULT_OFFSET, alignOffset = DEFAULT_OFFSET, } = input;
13
+ const { placement, side, align } = parsePlacement(input.placement ?? DEFAULT_PLACEMENT);
14
+ let x = 0;
15
+ let y = 0;
16
+ if (side === "top" || side === "bottom") {
17
+ x = reference.x + (reference.width - floating.width) / 2;
18
+ if (align === "start") {
19
+ x = reference.x;
20
+ }
21
+ else if (align === "end") {
22
+ x = reference.x + reference.width - floating.width;
23
+ }
24
+ x += alignOffset;
25
+ y =
26
+ side === "bottom"
27
+ ? reference.y + reference.height + offset
28
+ : reference.y - floating.height - offset;
29
+ }
30
+ else {
31
+ y = reference.y + (reference.height - floating.height) / 2;
32
+ if (align === "start") {
33
+ y = reference.y;
34
+ }
35
+ else if (align === "end") {
36
+ y = reference.y + reference.height - floating.height;
37
+ }
38
+ y += alignOffset;
39
+ x =
40
+ side === "right"
41
+ ? reference.x + reference.width + offset
42
+ : reference.x - floating.width - offset;
43
+ }
44
+ return { x, y, placement };
45
+ }
@@ -0,0 +1,8 @@
1
+ import type { CreatePositionerOptions, Positioner } from "../types/positioner.js";
2
+ /**
3
+ * Create a collision-aware positioning session.
4
+ *
5
+ * Collision handling (flip/shift) is delegated to Floating UI but never leaks
6
+ * through this API; adapters only see coordinates, placement, and strategy.
7
+ */
8
+ export declare function createPositioner(initial?: CreatePositionerOptions): Positioner;
@@ -0,0 +1,251 @@
1
+ import { arrow as arrowMiddleware, autoUpdate as createAutoUpdate, computePosition as computeEnginePosition, flip as flipMiddleware, offset as offsetMiddleware, shift as shiftMiddleware, } from "@floating-ui/dom";
2
+ import { isConnected } from "../utils/element.js";
3
+ import { isElement } from "../utils/dom.js";
4
+ import { parsePlacement } from "./placement.js";
5
+ import { resolveStrategy } from "./strategy.js";
6
+ const DEFAULT_OPTIONS = {
7
+ placement: "bottom",
8
+ offset: 0,
9
+ alignOffset: 0,
10
+ flip: true,
11
+ shift: true,
12
+ padding: 0,
13
+ boundary: "clippingAncestors",
14
+ strategy: undefined,
15
+ autoUpdate: true,
16
+ arrow: null,
17
+ };
18
+ function resolveOptions(patch) {
19
+ return {
20
+ placement: patch.placement ?? DEFAULT_OPTIONS.placement,
21
+ offset: patch.offset ?? DEFAULT_OPTIONS.offset,
22
+ alignOffset: patch.alignOffset ?? DEFAULT_OPTIONS.alignOffset,
23
+ flip: patch.flip ?? DEFAULT_OPTIONS.flip,
24
+ shift: patch.shift ?? DEFAULT_OPTIONS.shift,
25
+ padding: patch.padding ?? DEFAULT_OPTIONS.padding,
26
+ boundary: patch.boundary ?? DEFAULT_OPTIONS.boundary,
27
+ strategy: patch.strategy,
28
+ autoUpdate: patch.autoUpdate ?? DEFAULT_OPTIONS.autoUpdate,
29
+ arrow: patch.arrow ?? DEFAULT_OPTIONS.arrow,
30
+ };
31
+ }
32
+ /** Drop `undefined` entries so a partial update cannot reset unrelated options. */
33
+ function definedOptions(patch) {
34
+ const entries = Object.entries(patch).filter(([, value]) => value !== undefined);
35
+ return Object.fromEntries(entries);
36
+ }
37
+ function toClientRect(rect) {
38
+ return {
39
+ x: rect.x,
40
+ y: rect.y,
41
+ width: rect.width,
42
+ height: rect.height,
43
+ top: rect.y,
44
+ right: rect.x + rect.width,
45
+ bottom: rect.y + rect.height,
46
+ left: rect.x,
47
+ };
48
+ }
49
+ /**
50
+ * Adapt core's minimal `VirtualElement` contract to the engine's shape.
51
+ *
52
+ * The engine is an implementation detail; core only promises `x/y/width/height`.
53
+ */
54
+ function toEngineReference(reference) {
55
+ if (isElement(reference)) {
56
+ return reference;
57
+ }
58
+ return {
59
+ getBoundingClientRect: () => toClientRect(reference.getBoundingClientRect()),
60
+ ...(reference.contextElement
61
+ ? { contextElement: reference.contextElement }
62
+ : {}),
63
+ };
64
+ }
65
+ /**
66
+ * Create a collision-aware positioning session.
67
+ *
68
+ * Collision handling (flip/shift) is delegated to Floating UI but never leaks
69
+ * through this API; adapters only see coordinates, placement, and strategy.
70
+ */
71
+ export function createPositioner(initial = {}) {
72
+ let reference = initial.reference ?? null;
73
+ let floating = initial.floating ?? null;
74
+ let options = resolveOptions(initial);
75
+ let disposed = false;
76
+ let updateId = 0;
77
+ let stopAutoUpdate = null;
78
+ const listeners = new Set();
79
+ const compute = async () => {
80
+ if (disposed) {
81
+ return null;
82
+ }
83
+ const currentReference = reference;
84
+ const currentFloating = floating;
85
+ if (!currentReference || !currentFloating) {
86
+ return null;
87
+ }
88
+ if (!isConnected(currentFloating)) {
89
+ return null;
90
+ }
91
+ if (isElement(currentReference) && !isConnected(currentReference)) {
92
+ return null;
93
+ }
94
+ try {
95
+ const placement = parsePlacement(options.placement).placement;
96
+ const boundary = options.boundary === "viewport"
97
+ ? "clippingAncestors"
98
+ : options.boundary;
99
+ const rootBoundary = "viewport";
100
+ const middleware = [
101
+ offsetMiddleware({
102
+ mainAxis: options.offset,
103
+ crossAxis: options.alignOffset,
104
+ }),
105
+ ];
106
+ if (options.flip) {
107
+ middleware.push(flipMiddleware({
108
+ padding: options.padding,
109
+ boundary,
110
+ rootBoundary,
111
+ }));
112
+ }
113
+ if (options.shift) {
114
+ middleware.push(shiftMiddleware({
115
+ padding: options.padding,
116
+ boundary,
117
+ rootBoundary,
118
+ }));
119
+ }
120
+ if (options.arrow?.element && isConnected(options.arrow.element)) {
121
+ middleware.push(arrowMiddleware({
122
+ element: options.arrow.element,
123
+ padding: options.arrow.padding ?? 0,
124
+ }));
125
+ }
126
+ const result = await computeEnginePosition(toEngineReference(currentReference), currentFloating, {
127
+ placement,
128
+ strategy: resolveStrategy(currentReference, options.strategy),
129
+ middleware,
130
+ });
131
+ const arrow = result.middlewareData.arrow;
132
+ return {
133
+ x: result.x,
134
+ y: result.y,
135
+ placement: result.placement,
136
+ strategy: result.strategy,
137
+ ...(arrow
138
+ ? {
139
+ arrow: {
140
+ ...(typeof arrow.x === "number" ? { x: arrow.x } : {}),
141
+ ...(typeof arrow.y === "number" ? { y: arrow.y } : {}),
142
+ centerOffset: arrow.centerOffset ?? 0,
143
+ },
144
+ }
145
+ : {}),
146
+ };
147
+ }
148
+ catch {
149
+ return null;
150
+ }
151
+ };
152
+ const update = async () => {
153
+ if (disposed) {
154
+ return null;
155
+ }
156
+ const id = ++updateId;
157
+ const currentReference = reference;
158
+ const currentFloating = floating;
159
+ const result = await compute();
160
+ if (disposed || id !== updateId) {
161
+ return null;
162
+ }
163
+ if (reference !== currentReference || floating !== currentFloating) {
164
+ return null;
165
+ }
166
+ if (!result) {
167
+ return null;
168
+ }
169
+ for (const listener of [...listeners]) {
170
+ listener(result);
171
+ }
172
+ return result;
173
+ };
174
+ const stopWatching = () => {
175
+ stopAutoUpdate?.();
176
+ stopAutoUpdate = null;
177
+ };
178
+ const startWatching = () => {
179
+ stopWatching();
180
+ if (disposed || !options.autoUpdate) {
181
+ return;
182
+ }
183
+ if (!reference || !floating || !isConnected(floating)) {
184
+ return;
185
+ }
186
+ if (isElement(reference) && !isConnected(reference)) {
187
+ return;
188
+ }
189
+ stopAutoUpdate = createAutoUpdate(toEngineReference(reference), floating, () => {
190
+ void update();
191
+ });
192
+ };
193
+ const refresh = () => {
194
+ if (disposed) {
195
+ return;
196
+ }
197
+ startWatching();
198
+ void update();
199
+ };
200
+ refresh();
201
+ return {
202
+ get isDisposed() {
203
+ return disposed;
204
+ },
205
+ setReference(next) {
206
+ if (disposed) {
207
+ return;
208
+ }
209
+ reference = next;
210
+ refresh();
211
+ },
212
+ setFloating(next) {
213
+ if (disposed) {
214
+ return;
215
+ }
216
+ floating = next;
217
+ refresh();
218
+ },
219
+ setOptions(patch) {
220
+ if (disposed) {
221
+ return;
222
+ }
223
+ options = resolveOptions({
224
+ ...options,
225
+ ...definedOptions(patch),
226
+ });
227
+ refresh();
228
+ },
229
+ update,
230
+ subscribe(listener) {
231
+ if (disposed) {
232
+ return () => { };
233
+ }
234
+ listeners.add(listener);
235
+ return () => {
236
+ listeners.delete(listener);
237
+ };
238
+ },
239
+ dispose() {
240
+ if (disposed) {
241
+ return;
242
+ }
243
+ disposed = true;
244
+ updateId += 1;
245
+ stopWatching();
246
+ listeners.clear();
247
+ reference = null;
248
+ floating = null;
249
+ },
250
+ };
251
+ }
@@ -0,0 +1,6 @@
1
+ export { computePosition } from "./compute.js";
2
+ export { createPositioner } from "./create-positioner.js";
3
+ export { resolvePlacementCoordinates } from "./coordinates.js";
4
+ export { isSamePositionerResult } from "./same-result.js";
5
+ export { DEFAULT_PLACEMENT, parsePlacement } from "./placement.js";
6
+ export { DEFAULT_ARROW_SIZE, applyArrowStyles, arrowStaticSide, getArrowStyles, resetArrowStyles, snapshotArrowStyles, type ArrowSnapshot, type ArrowStyles, } from "./arrow.js";
@@ -0,0 +1,6 @@
1
+ export { computePosition } from "./compute.js";
2
+ export { createPositioner } from "./create-positioner.js";
3
+ export { resolvePlacementCoordinates } from "./coordinates.js";
4
+ export { isSamePositionerResult } from "./same-result.js";
5
+ export { DEFAULT_PLACEMENT, parsePlacement } from "./placement.js";
6
+ export { DEFAULT_ARROW_SIZE, applyArrowStyles, arrowStaticSide, getArrowStyles, resetArrowStyles, snapshotArrowStyles, } from "./arrow.js";
@@ -0,0 +1,8 @@
1
+ import type { Placement } from "../types/geometry.js";
2
+ import type { ParsedPlacement } from "../types/positioning.js";
3
+ export declare const DEFAULT_PLACEMENT: Placement;
4
+ /**
5
+ * Split a placement into its side and alignment and normalize `center`
6
+ * aliases (`"top"` and `"top-center"` are the same placement).
7
+ */
8
+ export declare function parsePlacement(placement?: Placement): ParsedPlacement;
@@ -0,0 +1,30 @@
1
+ export const DEFAULT_PLACEMENT = "bottom";
2
+ const SIDES = ["top", "right", "bottom", "left"];
3
+ const ALIGNS = ["start", "center", "end"];
4
+ function isSide(value) {
5
+ return SIDES.includes(value);
6
+ }
7
+ function isAlign(value) {
8
+ return ALIGNS.includes(value);
9
+ }
10
+ /**
11
+ * Split a placement into its side and alignment and normalize `center`
12
+ * aliases (`"top"` and `"top-center"` are the same placement).
13
+ */
14
+ export function parsePlacement(placement = DEFAULT_PLACEMENT) {
15
+ const [rawSide = "", rawAlign] = placement.split("-");
16
+ if (!isSide(rawSide)) {
17
+ throw new Error(`[overlay-core] Unknown placement side: "${rawSide}"`);
18
+ }
19
+ const align = rawAlign ?? "center";
20
+ if (!isAlign(align)) {
21
+ throw new Error(`[overlay-core] Unknown placement alignment: "${align}"`);
22
+ }
23
+ return {
24
+ placement: align === "center"
25
+ ? rawSide
26
+ : `${rawSide}-${align}`,
27
+ side: rawSide,
28
+ align,
29
+ };
30
+ }
@@ -0,0 +1,8 @@
1
+ import type { PositionerResult } from "../types/positioner.js";
2
+ /**
3
+ * True when `next` places the floating element exactly where `previous` did.
4
+ *
5
+ * Adapters use it to skip redundant state updates. `previous` is typically the
6
+ * adapter's own last-applied result, so it may be `null`.
7
+ */
8
+ export declare function isSamePositionerResult(previous: PositionerResult | null, next: PositionerResult): boolean;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * True when `next` places the floating element exactly where `previous` did.
3
+ *
4
+ * Adapters use it to skip redundant state updates. `previous` is typically the
5
+ * adapter's own last-applied result, so it may be `null`.
6
+ */
7
+ export function isSamePositionerResult(previous, next) {
8
+ return (previous !== null &&
9
+ previous.x === next.x &&
10
+ previous.y === next.y &&
11
+ previous.placement === next.placement &&
12
+ previous.strategy === next.strategy);
13
+ }
@@ -0,0 +1,12 @@
1
+ import type { ReferenceElement } from "../types/element.js";
2
+ import type { Strategy } from "../types/index.js";
3
+ /**
4
+ * Pick the CSS strategy when the caller did not choose one.
5
+ *
6
+ * `absolute` is page-relative, so the browser moves the popup with its anchor
7
+ * in the same frame while the page scrolls. `fixed` is viewport-relative and
8
+ * would be painted at its old position until a scroll handler re-measures.
9
+ * `fixed` is still right when the anchor itself does not scroll with the page
10
+ * (fixed/sticky ancestors) or when the anchor is a bare viewport point.
11
+ */
12
+ export declare function resolveStrategy(reference: ReferenceElement | null, explicit: Strategy | undefined): Strategy;
@@ -0,0 +1,46 @@
1
+ import { isElement } from "../utils/dom.js";
2
+ /** True when an ancestor is `position: fixed` or `position: sticky`. */
3
+ function hasFixedOrStickyAncestor(element) {
4
+ const view = element.ownerDocument?.defaultView ?? null;
5
+ if (!view) {
6
+ return false;
7
+ }
8
+ let node = element;
9
+ while (node) {
10
+ const position = view.getComputedStyle(node).position;
11
+ if (position === "fixed" || position === "sticky") {
12
+ return true;
13
+ }
14
+ if (node.parentElement) {
15
+ node = node.parentElement;
16
+ continue;
17
+ }
18
+ // Cross shadow-root boundaries.
19
+ const root = node.getRootNode();
20
+ node = "host" in root ? root.host : null;
21
+ }
22
+ return false;
23
+ }
24
+ /**
25
+ * Pick the CSS strategy when the caller did not choose one.
26
+ *
27
+ * `absolute` is page-relative, so the browser moves the popup with its anchor
28
+ * in the same frame while the page scrolls. `fixed` is viewport-relative and
29
+ * would be painted at its old position until a scroll handler re-measures.
30
+ * `fixed` is still right when the anchor itself does not scroll with the page
31
+ * (fixed/sticky ancestors) or when the anchor is a bare viewport point.
32
+ */
33
+ export function resolveStrategy(reference, explicit) {
34
+ if (explicit) {
35
+ return explicit;
36
+ }
37
+ const anchor = reference
38
+ ? isElement(reference)
39
+ ? reference
40
+ : (reference.contextElement ?? null)
41
+ : null;
42
+ if (!anchor?.isConnected) {
43
+ return "fixed";
44
+ }
45
+ return hasFixedOrStickyAncestor(anchor) ? "fixed" : "absolute";
46
+ }
@@ -0,0 +1 @@
1
+ export { lockBodyScroll } from "./scroll-lock.js";
@@ -0,0 +1 @@
1
+ export { lockBodyScroll } from "./scroll-lock.js";
@@ -0,0 +1,12 @@
1
+ import type { Cleanup } from "../types/lifecycle.js";
2
+ /**
3
+ * Reference-counted body scroll lock for modal overlays.
4
+ *
5
+ * The first lock saves the current inline overflow/padding styles and applies
6
+ * `overflow: hidden` plus scrollbar-width compensation. Nested locks only
7
+ * increment a counter; the final release restores the original styles.
8
+ *
9
+ * This intentionally does not attach global event listeners. Callers own the
10
+ * lock lifetime and must call the returned cleanup on close/unmount.
11
+ */
12
+ export declare function lockBodyScroll(documentRef?: Document): Cleanup;