@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,78 @@
1
+ export const DEFAULT_LAYER_CONFIG = {
2
+ defaultOpen: false,
3
+ dismissOnEscape: true,
4
+ dismissOnOutsidePress: true,
5
+ };
6
+ function resolveConfig(config) {
7
+ return {
8
+ defaultOpen: config.defaultOpen ?? DEFAULT_LAYER_CONFIG.defaultOpen,
9
+ dismissOnEscape: config.dismissOnEscape ?? DEFAULT_LAYER_CONFIG.dismissOnEscape,
10
+ dismissOnOutsidePress: config.dismissOnOutsidePress ??
11
+ DEFAULT_LAYER_CONFIG.dismissOnOutsidePress,
12
+ };
13
+ }
14
+ /**
15
+ * Create a framework-free open/closed layer.
16
+ *
17
+ * The layer does not attach DOM listeners; adapters translate `config` into
18
+ * their own dismissal wiring and call `close(reason)` when a dismissal happens.
19
+ */
20
+ export function createLayer(config = {}) {
21
+ const resolved = Object.freeze(resolveConfig(config));
22
+ const listeners = new Set();
23
+ let status = resolved.defaultOpen ? "open" : "closed";
24
+ let destroyed = false;
25
+ const emit = (event) => {
26
+ for (const listener of [...listeners]) {
27
+ listener(event);
28
+ }
29
+ };
30
+ const open = () => {
31
+ if (destroyed || status === "open") {
32
+ return;
33
+ }
34
+ status = "open";
35
+ emit({ type: "open", status: "open" });
36
+ };
37
+ const close = (reason = "imperative") => {
38
+ if (status === "closed") {
39
+ return;
40
+ }
41
+ status = "closed";
42
+ emit({ type: "close", status: "closed", reason });
43
+ };
44
+ const toggle = () => {
45
+ if (status === "open") {
46
+ close();
47
+ }
48
+ else {
49
+ open();
50
+ }
51
+ };
52
+ return {
53
+ get status() {
54
+ return status;
55
+ },
56
+ config: resolved,
57
+ open,
58
+ close,
59
+ toggle,
60
+ subscribe(listener) {
61
+ if (destroyed) {
62
+ return () => { };
63
+ }
64
+ listeners.add(listener);
65
+ return () => {
66
+ listeners.delete(listener);
67
+ };
68
+ },
69
+ destroy() {
70
+ if (destroyed) {
71
+ return;
72
+ }
73
+ close();
74
+ destroyed = true;
75
+ listeners.clear();
76
+ },
77
+ };
78
+ }
@@ -0,0 +1,3 @@
1
+ export { createLayer, DEFAULT_LAYER_CONFIG, type CreateLayerOptions, } from "./create-layer.js";
2
+ export { createLayerManager } from "./manager.js";
3
+ export { createLayerStack, createLayerStackRegistry, acquireSharedLayerStack, } from "./create-layer-stack.js";
@@ -0,0 +1,3 @@
1
+ export { createLayer, DEFAULT_LAYER_CONFIG, } from "./create-layer.js";
2
+ export { createLayerManager } from "./manager.js";
3
+ export { createLayerStack, createLayerStackRegistry, acquireSharedLayerStack, } from "./create-layer-stack.js";
@@ -0,0 +1,9 @@
1
+ import type { LayerManager } from "../types/layer.js";
2
+ /**
3
+ * Create a minimal layer stack.
4
+ *
5
+ * Layers are ordered by registration (bottom → top). A child layer belongs to
6
+ * an existing parent; unregistering a parent also removes its descendants.
7
+ * The manager does not open, close, focus, or render layers.
8
+ */
9
+ export declare function createLayerManager(): LayerManager;
@@ -0,0 +1,157 @@
1
+ import { isOutsidePress } from "../events/outside.js";
2
+ import { resolveElement } from "../utils/element.js";
3
+ function normalizeElement(source) {
4
+ if (!source) {
5
+ return null;
6
+ }
7
+ if (typeof source === "function") {
8
+ return source;
9
+ }
10
+ const element = source;
11
+ return () => element;
12
+ }
13
+ /**
14
+ * Create a minimal layer stack.
15
+ *
16
+ * Layers are ordered by registration (bottom → top). A child layer belongs to
17
+ * an existing parent; unregistering a parent also removes its descendants.
18
+ * The manager does not open, close, focus, or render layers.
19
+ */
20
+ export function createLayerManager() {
21
+ const layers = [];
22
+ const listeners = new Set();
23
+ let nextId = 0;
24
+ let disposed = false;
25
+ const toEntry = (layer) => Object.freeze({ id: layer.id, parentId: layer.parentId });
26
+ const getStack = () => Object.freeze(layers.map(toEntry));
27
+ const emit = () => {
28
+ const stack = getStack();
29
+ for (const listener of [...listeners]) {
30
+ listener(stack);
31
+ }
32
+ };
33
+ const find = (id) => layers.find((layer) => layer.id === id);
34
+ const collectElements = (id) => {
35
+ const root = find(id);
36
+ if (!root) {
37
+ return [];
38
+ }
39
+ const ids = new Set([root.id]);
40
+ for (const layer of layers) {
41
+ if (layer.parentId !== null && ids.has(layer.parentId)) {
42
+ ids.add(layer.id);
43
+ }
44
+ }
45
+ const elements = [];
46
+ for (const layer of layers) {
47
+ if (!ids.has(layer.id)) {
48
+ continue;
49
+ }
50
+ const element = resolveElement(layer.element);
51
+ if (element) {
52
+ elements.push(element);
53
+ }
54
+ }
55
+ return elements;
56
+ };
57
+ const unregister = (id) => {
58
+ if (!find(id)) {
59
+ return false;
60
+ }
61
+ const removed = new Set([id]);
62
+ for (const layer of layers) {
63
+ if (layer.parentId !== null && removed.has(layer.parentId)) {
64
+ removed.add(layer.id);
65
+ }
66
+ }
67
+ for (let index = layers.length - 1; index >= 0; index -= 1) {
68
+ const layer = layers[index];
69
+ if (layer && removed.has(layer.id)) {
70
+ layers.splice(index, 1);
71
+ }
72
+ }
73
+ emit();
74
+ return true;
75
+ };
76
+ const isOutside = (id, event) => {
77
+ const elements = collectElements(id);
78
+ if (elements.length === 0) {
79
+ return false;
80
+ }
81
+ return isOutsidePress(event, elements);
82
+ };
83
+ return {
84
+ get size() {
85
+ return layers.length;
86
+ },
87
+ register(options = {}) {
88
+ if (disposed) {
89
+ throw new Error("[overlay-core] Layer manager is disposed.");
90
+ }
91
+ const parentId = options.parentId ?? null;
92
+ if (parentId !== null && !find(parentId)) {
93
+ throw new Error(`[overlay-core] Unknown parent layer: "${parentId}".`);
94
+ }
95
+ nextId += 1;
96
+ const id = options.id ?? `layer-${nextId}`;
97
+ if (find(id)) {
98
+ throw new Error(`[overlay-core] Duplicate layer id: "${id}".`);
99
+ }
100
+ const layer = {
101
+ id,
102
+ parentId,
103
+ element: normalizeElement(options.element),
104
+ };
105
+ layers.push(layer);
106
+ emit();
107
+ return {
108
+ id,
109
+ parentId,
110
+ get isRegistered() {
111
+ return find(id) !== undefined;
112
+ },
113
+ dispose() {
114
+ unregister(id);
115
+ },
116
+ };
117
+ },
118
+ unregister,
119
+ get(id) {
120
+ const layer = find(id);
121
+ return layer ? toEntry(layer) : null;
122
+ },
123
+ getStack,
124
+ getTopmost() {
125
+ const top = layers[layers.length - 1];
126
+ return top ? toEntry(top) : null;
127
+ },
128
+ isTopmost(id) {
129
+ return layers[layers.length - 1]?.id === id;
130
+ },
131
+ isOutside,
132
+ isOutsideActive(event) {
133
+ const top = layers[layers.length - 1];
134
+ if (!top) {
135
+ return false;
136
+ }
137
+ return isOutside(top.id, event);
138
+ },
139
+ subscribe(listener) {
140
+ if (disposed) {
141
+ return () => { };
142
+ }
143
+ listeners.add(listener);
144
+ return () => {
145
+ listeners.delete(listener);
146
+ };
147
+ },
148
+ dispose() {
149
+ if (disposed) {
150
+ return;
151
+ }
152
+ disposed = true;
153
+ layers.length = 0;
154
+ listeners.clear();
155
+ },
156
+ };
157
+ }
@@ -0,0 +1,8 @@
1
+ import type { Cleanup } from "../types/lifecycle.js";
2
+ /**
3
+ * Add an event listener and return an idempotent cleanup.
4
+ *
5
+ * Nullish targets (missing refs during SSR or unmount) are a no-op so callers
6
+ * don't need to branch.
7
+ */
8
+ export declare function listen<E extends Event = Event>(target: EventTarget | null | undefined, type: string, handler: (event: E) => void, options?: AddEventListenerOptions | boolean): Cleanup;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Add an event listener and return an idempotent cleanup.
3
+ *
4
+ * Nullish targets (missing refs during SSR or unmount) are a no-op so callers
5
+ * don't need to branch.
6
+ */
7
+ export function listen(target, type, handler, options) {
8
+ if (!target) {
9
+ return () => { };
10
+ }
11
+ const listener = handler;
12
+ target.addEventListener(type, listener, options);
13
+ let active = true;
14
+ return () => {
15
+ if (!active) {
16
+ return;
17
+ }
18
+ active = false;
19
+ target.removeEventListener(type, listener, options);
20
+ };
21
+ }
@@ -0,0 +1 @@
1
+ export { listen } from "./events.js";
@@ -0,0 +1 @@
1
+ export { listen } from "./events.js";
@@ -0,0 +1 @@
1
+ export { createTypeaheadBuffer, focusFirstMenuItem, focusLastMenuItem, focusMenuItem, focusMenuTypeahead, getMenuItems, moveMenuFocus, type TypeaheadBuffer, } from "./navigation.js";
@@ -0,0 +1 @@
1
+ export { createTypeaheadBuffer, focusFirstMenuItem, focusLastMenuItem, focusMenuItem, focusMenuTypeahead, getMenuItems, moveMenuFocus, } from "./navigation.js";
@@ -0,0 +1,22 @@
1
+ /** Enabled, visible menu items that directly belong to `container`. */
2
+ export declare function getMenuItems(container: HTMLElement): HTMLElement[];
3
+ /** Focus a menu item. Returns whether it ended up as the active element. */
4
+ export declare function focusMenuItem(element: HTMLElement | null): boolean;
5
+ /** Focus the first item, or the container itself when it has none. */
6
+ export declare function focusFirstMenuItem(container: HTMLElement): HTMLElement | null;
7
+ /** Focus the last item, or the container itself when it has none. */
8
+ export declare function focusLastMenuItem(container: HTMLElement): HTMLElement | null;
9
+ /** Move focus to the next/previous item, wrapping around. */
10
+ export declare function moveMenuFocus(container: HTMLElement, delta: 1 | -1): void;
11
+ /**
12
+ * Focus the next item (after `active`, wrapping) whose text starts with
13
+ * `buffer`, case-insensitively.
14
+ */
15
+ export declare function focusMenuTypeahead(container: HTMLElement, buffer: string, active: Element | null): HTMLElement | null;
16
+ export type TypeaheadBuffer = {
17
+ /** Append a key and restart the reset timer; returns the current buffer. */
18
+ push(key: string): string;
19
+ clear(): void;
20
+ };
21
+ /** A character buffer that clears itself after `timeout` ms of inactivity. */
22
+ export declare function createTypeaheadBuffer(timeout?: number): TypeaheadBuffer;
@@ -0,0 +1,107 @@
1
+ import { getActiveElement } from "../focus/focus.js";
2
+ const ITEM_SELECTOR = [
3
+ '[role="menuitem"]',
4
+ '[role="menuitemcheckbox"]',
5
+ '[role="menuitemradio"]',
6
+ ].join(",");
7
+ function isNavigable(element, container) {
8
+ // Items of a nested menu belong to that menu, not to this one.
9
+ if (element.closest('[role="menu"]') !== container) {
10
+ return false;
11
+ }
12
+ return !(element.getAttribute("aria-disabled") === "true" ||
13
+ element.hasAttribute("disabled") ||
14
+ element.hidden);
15
+ }
16
+ /** Enabled, visible menu items that directly belong to `container`. */
17
+ export function getMenuItems(container) {
18
+ return [...container.querySelectorAll(ITEM_SELECTOR)].filter((element) => isNavigable(element, container));
19
+ }
20
+ /** Focus a menu item. Returns whether it ended up as the active element. */
21
+ export function focusMenuItem(element) {
22
+ if (!element?.isConnected) {
23
+ return false;
24
+ }
25
+ element.focus();
26
+ return getActiveElement(element.ownerDocument) === element;
27
+ }
28
+ /** Focus the first item, or the container itself when it has none. */
29
+ export function focusFirstMenuItem(container) {
30
+ const first = getMenuItems(container)[0] ?? null;
31
+ if (first) {
32
+ focusMenuItem(first);
33
+ }
34
+ else if (container.isConnected) {
35
+ container.focus();
36
+ }
37
+ return first;
38
+ }
39
+ /** Focus the last item, or the container itself when it has none. */
40
+ export function focusLastMenuItem(container) {
41
+ const last = getMenuItems(container).at(-1) ?? null;
42
+ if (last) {
43
+ focusMenuItem(last);
44
+ }
45
+ else if (container.isConnected) {
46
+ container.focus();
47
+ }
48
+ return last;
49
+ }
50
+ /** Move focus to the next/previous item, wrapping around. */
51
+ export function moveMenuFocus(container, delta) {
52
+ const items = getMenuItems(container);
53
+ if (items.length === 0) {
54
+ container.focus();
55
+ return;
56
+ }
57
+ const active = getActiveElement(container.ownerDocument);
58
+ const index = active ? items.indexOf(active) : -1;
59
+ const nextIndex = index === -1
60
+ ? delta > 0
61
+ ? 0
62
+ : items.length - 1
63
+ : (index + delta + items.length) % items.length;
64
+ focusMenuItem(items[nextIndex] ?? null);
65
+ }
66
+ /**
67
+ * Focus the next item (after `active`, wrapping) whose text starts with
68
+ * `buffer`, case-insensitively.
69
+ */
70
+ export function focusMenuTypeahead(container, buffer, active) {
71
+ const items = getMenuItems(container);
72
+ const normalized = buffer.toLocaleLowerCase();
73
+ const currentIndex = active ? items.indexOf(active) : -1;
74
+ const ordered = [
75
+ ...items.slice(currentIndex + 1),
76
+ ...items.slice(0, currentIndex + 1),
77
+ ];
78
+ const match = ordered.find((item) => (item.textContent ?? "")
79
+ .trim()
80
+ .toLocaleLowerCase()
81
+ .startsWith(normalized)) ?? null;
82
+ focusMenuItem(match);
83
+ return match;
84
+ }
85
+ /** A character buffer that clears itself after `timeout` ms of inactivity. */
86
+ export function createTypeaheadBuffer(timeout = 500) {
87
+ let value = "";
88
+ let timer = null;
89
+ const clear = () => {
90
+ if (timer !== null) {
91
+ clearTimeout(timer);
92
+ timer = null;
93
+ }
94
+ value = "";
95
+ };
96
+ return {
97
+ push(key) {
98
+ if (timer !== null) {
99
+ clearTimeout(timer);
100
+ }
101
+ value += key;
102
+ timer = setTimeout(clear, timeout);
103
+ return value;
104
+ },
105
+ clear,
106
+ };
107
+ }
@@ -0,0 +1,10 @@
1
+ import type { Cleanup } from "../types/lifecycle.js";
2
+ /**
3
+ * Mark every body-level sibling of `roots` inert and `aria-hidden` while a
4
+ * modal is open. Live regions and non-visual elements are left alone.
5
+ *
6
+ * Acquisitions stack per document: only the most recent one is enforced, and
7
+ * releasing it re-applies the previous one. Cleanup is idempotent and restores
8
+ * each element's original `inert`/`aria-hidden` attributes.
9
+ */
10
+ export declare function acquireModalBackground(documentRef: Document, roots: readonly Element[]): Cleanup;
@@ -0,0 +1,115 @@
1
+ import { getGlobalState } from "../utils/global-state.js";
2
+ const states = getGlobalState("modal-background", () => new WeakMap());
3
+ function topLevelBodyChild(root, body) {
4
+ let node = root;
5
+ while (node?.parentElement && node.parentElement !== body) {
6
+ node = node.parentElement;
7
+ }
8
+ return node?.parentElement === body ? node : null;
9
+ }
10
+ function isLiveRegion(element) {
11
+ if (element.hasAttribute("aria-live")) {
12
+ return true;
13
+ }
14
+ const role = element.getAttribute("role");
15
+ return role === "status" || role === "alert" || role === "log";
16
+ }
17
+ function isNonVisual(element) {
18
+ return ["SCRIPT", "STYLE", "LINK", "META", "TEMPLATE"].includes(element.tagName);
19
+ }
20
+ function restore(element, saved) {
21
+ if (saved.inert === null) {
22
+ element.removeAttribute("inert");
23
+ }
24
+ else {
25
+ element.setAttribute("inert", saved.inert);
26
+ }
27
+ if (saved.ariaHidden === null) {
28
+ element.removeAttribute("aria-hidden");
29
+ }
30
+ else {
31
+ element.setAttribute("aria-hidden", saved.ariaHidden);
32
+ }
33
+ }
34
+ function apply(state, documentRef) {
35
+ const body = documentRef.body;
36
+ const top = state.entries.at(-1);
37
+ if (!body || !top) {
38
+ return;
39
+ }
40
+ const active = new Set();
41
+ for (const root of top) {
42
+ if (!root.isConnected) {
43
+ continue;
44
+ }
45
+ const child = topLevelBodyChild(root, body);
46
+ if (child) {
47
+ active.add(child);
48
+ }
49
+ }
50
+ for (const [element, saved] of [...state.hidden]) {
51
+ if (!element.isConnected || active.has(element)) {
52
+ restore(element, saved);
53
+ state.hidden.delete(element);
54
+ }
55
+ }
56
+ for (const child of [...body.children]) {
57
+ if (!(child instanceof HTMLElement)) {
58
+ continue;
59
+ }
60
+ if (active.has(child) || isLiveRegion(child) || isNonVisual(child)) {
61
+ continue;
62
+ }
63
+ if (state.hidden.has(child)) {
64
+ continue;
65
+ }
66
+ state.hidden.set(child, {
67
+ inert: child.getAttribute("inert"),
68
+ ariaHidden: child.getAttribute("aria-hidden"),
69
+ });
70
+ child.setAttribute("inert", "");
71
+ child.setAttribute("aria-hidden", "true");
72
+ }
73
+ }
74
+ /**
75
+ * Mark every body-level sibling of `roots` inert and `aria-hidden` while a
76
+ * modal is open. Live regions and non-visual elements are left alone.
77
+ *
78
+ * Acquisitions stack per document: only the most recent one is enforced, and
79
+ * releasing it re-applies the previous one. Cleanup is idempotent and restores
80
+ * each element's original `inert`/`aria-hidden` attributes.
81
+ */
82
+ export function acquireModalBackground(documentRef, roots) {
83
+ let state = states.get(documentRef);
84
+ if (!state) {
85
+ state = { entries: [], hidden: new Map() };
86
+ states.set(documentRef, state);
87
+ }
88
+ const entry = [...roots];
89
+ state.entries.push(entry);
90
+ apply(state, documentRef);
91
+ let released = false;
92
+ return () => {
93
+ if (released) {
94
+ return;
95
+ }
96
+ released = true;
97
+ const current = states.get(documentRef);
98
+ if (!current) {
99
+ return;
100
+ }
101
+ const index = current.entries.lastIndexOf(entry);
102
+ if (index >= 0) {
103
+ current.entries.splice(index, 1);
104
+ }
105
+ if (current.entries.length === 0) {
106
+ for (const [element, saved] of current.hidden) {
107
+ restore(element, saved);
108
+ }
109
+ current.hidden.clear();
110
+ states.delete(documentRef);
111
+ return;
112
+ }
113
+ apply(current, documentRef);
114
+ };
115
+ }
@@ -0,0 +1 @@
1
+ export { acquireModalBackground } from "./background.js";
@@ -0,0 +1 @@
1
+ export { acquireModalBackground } from "./background.js";
@@ -0,0 +1 @@
1
+ export { moveToPortal, restorePortal, type PortalState, } from "./portal.js";
@@ -0,0 +1 @@
1
+ export { moveToPortal, restorePortal, } from "./portal.js";
@@ -0,0 +1,13 @@
1
+ /** Where a portaled element came from, so it can be put back. */
2
+ export type PortalState = {
3
+ parent: Node;
4
+ nextSibling: Node | null;
5
+ element: HTMLElement;
6
+ };
7
+ /**
8
+ * Move `element` into `target`, remembering its original position.
9
+ * Returns `null` when the element has no parent to return to.
10
+ */
11
+ export declare function moveToPortal(element: HTMLElement, target: Element): PortalState | null;
12
+ /** Put a portaled element back where it came from. Safe with `null`. */
13
+ export declare function restorePortal(state: PortalState | null): void;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Move `element` into `target`, remembering its original position.
3
+ * Returns `null` when the element has no parent to return to.
4
+ */
5
+ export function moveToPortal(element, target) {
6
+ const parent = element.parentNode;
7
+ if (!parent) {
8
+ return null;
9
+ }
10
+ const state = { parent, nextSibling: element.nextSibling, element };
11
+ target.appendChild(element);
12
+ return state;
13
+ }
14
+ /** Put a portaled element back where it came from. Safe with `null`. */
15
+ export function restorePortal(state) {
16
+ if (!state) {
17
+ return;
18
+ }
19
+ if (state.nextSibling?.parentNode === state.parent) {
20
+ state.parent.insertBefore(state.element, state.nextSibling);
21
+ }
22
+ else {
23
+ state.parent.appendChild(state.element);
24
+ }
25
+ }
@@ -0,0 +1,34 @@
1
+ import type { Placement, Side } from "../types/geometry.js";
2
+ import type { ArrowData } from "../types/positioner.js";
3
+ export declare const DEFAULT_ARROW_SIZE = 8;
4
+ /** Edge of the floating element the arrow sits on (opposite the reference). */
5
+ export declare function arrowStaticSide(side: Side): Side;
6
+ export type ArrowStyles = {
7
+ side: Side;
8
+ /** Inline style properties as plain camelCase values. */
9
+ style: Record<string, string | number>;
10
+ };
11
+ /**
12
+ * Compute arrow geometry for a resolved placement. The arrow is a rotated
13
+ * square clipped to the half that points at the anchor, so it never covers
14
+ * popup content. Only its two outer edges are outlined, which lets the outline
15
+ * continue the popup's own border. The popup controls the look through CSS
16
+ * variables: `--overlay-arrow-background`, `--overlay-arrow-border-width`
17
+ * and `--overlay-arrow-border-color`.
18
+ * Returns `null` while the positioner has no arrow measurement yet.
19
+ */
20
+ export declare function getArrowStyles({ placement, arrow, size, }: {
21
+ placement: Placement;
22
+ arrow: ArrowData | undefined;
23
+ size?: number;
24
+ }): ArrowStyles | null;
25
+ declare const ARROW_PROPERTIES: readonly ["position", "width", "height", "pointer-events", "background-color", "box-sizing", "border-top", "border-right", "border-bottom", "border-left", "clip-path", "transform", "top", "right", "bottom", "left", "visibility"];
26
+ export type ArrowSnapshot = Record<(typeof ARROW_PROPERTIES)[number], string> & {
27
+ side: string | null;
28
+ };
29
+ /** Capture inline styles so a controller can restore the element later. */
30
+ export declare function snapshotArrowStyles(element: HTMLElement): ArrowSnapshot;
31
+ /** Write computed arrow styles and `data-side` to a consumer-owned element. */
32
+ export declare function applyArrowStyles(element: HTMLElement, styles: ArrowStyles | null): void;
33
+ export declare function resetArrowStyles(element: HTMLElement, snapshot: ArrowSnapshot): void;
34
+ export {};