@zealakit/overlay-react 0.0.0-stage → 1.0.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.
- package/LICENSE +21 -0
- package/README.md +547 -2
- package/dist/animation/index.d.ts +1 -0
- package/dist/animation/index.js +1 -0
- package/dist/animation/presence.d.ts +31 -0
- package/dist/animation/presence.js +104 -0
- package/dist/components/arrow.d.ts +30 -0
- package/dist/components/arrow.js +58 -0
- package/dist/components/context-menu/content.d.ts +37 -0
- package/dist/components/context-menu/content.js +52 -0
- package/dist/components/context-menu/context-menu.d.ts +20 -0
- package/dist/components/context-menu/context-menu.js +78 -0
- package/dist/components/context-menu/context.d.ts +47 -0
- package/dist/components/context-menu/context.js +27 -0
- package/dist/components/context-menu/index.d.ts +7 -0
- package/dist/components/context-menu/index.js +6 -0
- package/dist/components/context-menu/items.d.ts +71 -0
- package/dist/components/context-menu/items.js +207 -0
- package/dist/components/context-menu/menu-navigation.d.ts +3 -0
- package/dist/components/context-menu/menu-navigation.js +42 -0
- package/dist/components/context-menu/menu-surface.d.ts +58 -0
- package/dist/components/context-menu/menu-surface.js +172 -0
- package/dist/components/context-menu/sub.d.ts +45 -0
- package/dist/components/context-menu/sub.js +180 -0
- package/dist/components/context-menu/trigger.d.ts +17 -0
- package/dist/components/context-menu/trigger.js +62 -0
- package/dist/components/context-menu/types.d.ts +9 -0
- package/dist/components/context-menu/types.js +1 -0
- package/dist/components/dialog/content.d.ts +87 -0
- package/dist/components/dialog/content.js +238 -0
- package/dist/components/dialog/context.d.ts +41 -0
- package/dist/components/dialog/context.js +26 -0
- package/dist/components/dialog/dialog.d.ts +27 -0
- package/dist/components/dialog/dialog.js +88 -0
- package/dist/components/dialog/helpers.d.ts +24 -0
- package/dist/components/dialog/helpers.js +59 -0
- package/dist/components/dialog/index.d.ts +5 -0
- package/dist/components/dialog/index.js +5 -0
- package/dist/components/dialog/modal-background.d.ts +7 -0
- package/dist/components/dialog/modal-background.js +21 -0
- package/dist/components/dialog/trigger.d.ts +11 -0
- package/dist/components/dialog/trigger.js +29 -0
- package/dist/components/overlay-context.d.ts +13 -0
- package/dist/components/overlay-context.js +10 -0
- package/dist/components/popover/content.d.ts +131 -0
- package/dist/components/popover/content.js +261 -0
- package/dist/components/popover/context.d.ts +27 -0
- package/dist/components/popover/context.js +18 -0
- package/dist/components/popover/helpers.d.ts +23 -0
- package/dist/components/popover/helpers.js +58 -0
- package/dist/components/popover/index.d.ts +4 -0
- package/dist/components/popover/index.js +4 -0
- package/dist/components/popover/popover.d.ts +39 -0
- package/dist/components/popover/popover.js +98 -0
- package/dist/components/portal.d.ts +14 -0
- package/dist/components/portal.js +19 -0
- package/dist/components/slot.d.ts +12 -0
- package/dist/components/slot.js +60 -0
- package/dist/components/tooltip/content.d.ts +21 -0
- package/dist/components/tooltip/content.js +95 -0
- package/dist/components/tooltip/context.d.ts +22 -0
- package/dist/components/tooltip/context.js +10 -0
- package/dist/components/tooltip/index.d.ts +5 -0
- package/dist/components/tooltip/index.js +5 -0
- package/dist/components/tooltip/provider.d.ts +29 -0
- package/dist/components/tooltip/provider.js +29 -0
- package/dist/components/tooltip/tooltip.d.ts +25 -0
- package/dist/components/tooltip/tooltip.js +268 -0
- package/dist/components/tooltip/trigger.d.ts +11 -0
- package/dist/components/tooltip/trigger.js +67 -0
- package/dist/hooks/use-escape-key.d.ts +12 -0
- package/dist/hooks/use-escape-key.js +30 -0
- package/dist/hooks/use-layer.d.ts +33 -0
- package/dist/hooks/use-layer.js +84 -0
- package/dist/hooks/use-overlay.d.ts +31 -0
- package/dist/hooks/use-overlay.js +69 -0
- package/dist/hooks/use-positioner.d.ts +28 -0
- package/dist/hooks/use-positioner.js +127 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/dist/vendor/core/animation/index.d.ts +2 -0
- package/dist/vendor/core/animation/index.js +2 -0
- package/dist/vendor/core/animation/keyframes.d.ts +47 -0
- package/dist/vendor/core/animation/keyframes.js +105 -0
- package/dist/vendor/core/animation/run-animation.d.ts +27 -0
- package/dist/vendor/core/animation/run-animation.js +175 -0
- package/dist/vendor/core/controller/animator.d.ts +19 -0
- package/dist/vendor/core/controller/animator.js +117 -0
- package/dist/vendor/core/controller/context-menu-behavior.d.ts +15 -0
- package/dist/vendor/core/controller/context-menu-behavior.js +123 -0
- package/dist/vendor/core/controller/host.d.ts +10 -0
- package/dist/vendor/core/controller/host.js +1 -0
- package/dist/vendor/core/controller/index.d.ts +2 -0
- package/dist/vendor/core/controller/index.js +1 -0
- package/dist/vendor/core/controller/overlay-controller.d.ts +9 -0
- package/dist/vendor/core/controller/overlay-controller.js +723 -0
- package/dist/vendor/core/controller/tooltip-behavior.d.ts +9 -0
- package/dist/vendor/core/controller/tooltip-behavior.js +123 -0
- package/dist/vendor/core/controller/types.d.ts +92 -0
- package/dist/vendor/core/controller/types.js +1 -0
- package/dist/vendor/core/events/index.d.ts +3 -0
- package/dist/vendor/core/events/index.js +3 -0
- package/dist/vendor/core/events/keyboard.d.ts +2 -0
- package/dist/vendor/core/events/keyboard.js +4 -0
- package/dist/vendor/core/events/outside.d.ts +8 -0
- package/dist/vendor/core/events/outside.js +33 -0
- package/dist/vendor/core/events/pointer.d.ts +13 -0
- package/dist/vendor/core/events/pointer.js +18 -0
- package/dist/vendor/core/focus/focus.d.ts +73 -0
- package/dist/vendor/core/focus/focus.js +312 -0
- package/dist/vendor/core/focus/index.d.ts +1 -0
- package/dist/vendor/core/focus/index.js +1 -0
- package/dist/vendor/core/index.d.ts +13 -0
- package/dist/vendor/core/index.js +13 -0
- package/dist/vendor/core/layer/create-layer-stack.d.ts +25 -0
- package/dist/vendor/core/layer/create-layer-stack.js +247 -0
- package/dist/vendor/core/layer/create-layer.d.ts +10 -0
- package/dist/vendor/core/layer/create-layer.js +78 -0
- package/dist/vendor/core/layer/index.d.ts +3 -0
- package/dist/vendor/core/layer/index.js +3 -0
- package/dist/vendor/core/layer/manager.d.ts +9 -0
- package/dist/vendor/core/layer/manager.js +157 -0
- package/dist/vendor/core/lifecycle/events.d.ts +8 -0
- package/dist/vendor/core/lifecycle/events.js +21 -0
- package/dist/vendor/core/lifecycle/index.d.ts +1 -0
- package/dist/vendor/core/lifecycle/index.js +1 -0
- package/dist/vendor/core/menu/index.d.ts +1 -0
- package/dist/vendor/core/menu/index.js +1 -0
- package/dist/vendor/core/menu/navigation.d.ts +22 -0
- package/dist/vendor/core/menu/navigation.js +107 -0
- package/dist/vendor/core/modal/background.d.ts +10 -0
- package/dist/vendor/core/modal/background.js +115 -0
- package/dist/vendor/core/modal/index.d.ts +1 -0
- package/dist/vendor/core/modal/index.js +1 -0
- package/dist/vendor/core/portal/index.d.ts +1 -0
- package/dist/vendor/core/portal/index.js +1 -0
- package/dist/vendor/core/portal/portal.d.ts +13 -0
- package/dist/vendor/core/portal/portal.js +25 -0
- package/dist/vendor/core/positioning/arrow.d.ts +34 -0
- package/dist/vendor/core/positioning/arrow.js +142 -0
- package/dist/vendor/core/positioning/compute.d.ts +9 -0
- package/dist/vendor/core/positioning/compute.js +22 -0
- package/dist/vendor/core/positioning/coordinates.d.ts +11 -0
- package/dist/vendor/core/positioning/coordinates.js +45 -0
- package/dist/vendor/core/positioning/create-positioner.d.ts +8 -0
- package/dist/vendor/core/positioning/create-positioner.js +251 -0
- package/dist/vendor/core/positioning/index.d.ts +6 -0
- package/dist/vendor/core/positioning/index.js +6 -0
- package/dist/vendor/core/positioning/placement.d.ts +8 -0
- package/dist/vendor/core/positioning/placement.js +30 -0
- package/dist/vendor/core/positioning/same-result.d.ts +8 -0
- package/dist/vendor/core/positioning/same-result.js +13 -0
- package/dist/vendor/core/positioning/strategy.d.ts +12 -0
- package/dist/vendor/core/positioning/strategy.js +46 -0
- package/dist/vendor/core/scroll-lock/index.d.ts +1 -0
- package/dist/vendor/core/scroll-lock/index.js +1 -0
- package/dist/vendor/core/scroll-lock/scroll-lock.d.ts +12 -0
- package/dist/vendor/core/scroll-lock/scroll-lock.js +68 -0
- package/dist/vendor/core/types/animation.d.ts +81 -0
- package/dist/vendor/core/types/animation.js +93 -0
- package/dist/vendor/core/types/dismissal.d.ts +2 -0
- package/dist/vendor/core/types/dismissal.js +1 -0
- package/dist/vendor/core/types/element.d.ts +20 -0
- package/dist/vendor/core/types/element.js +1 -0
- package/dist/vendor/core/types/geometry.d.ts +22 -0
- package/dist/vendor/core/types/geometry.js +1 -0
- package/dist/vendor/core/types/index.d.ts +9 -0
- package/dist/vendor/core/types/index.js +8 -0
- package/dist/vendor/core/types/layer.d.ts +126 -0
- package/dist/vendor/core/types/layer.js +1 -0
- package/dist/vendor/core/types/lifecycle.d.ts +2 -0
- package/dist/vendor/core/types/lifecycle.js +1 -0
- package/dist/vendor/core/types/positioner.d.ts +74 -0
- package/dist/vendor/core/types/positioner.js +1 -0
- package/dist/vendor/core/types/positioning.d.ts +23 -0
- package/dist/vendor/core/types/positioning.js +1 -0
- package/dist/vendor/core/utils/dom.d.ts +11 -0
- package/dist/vendor/core/utils/dom.js +21 -0
- package/dist/vendor/core/utils/element.d.ts +15 -0
- package/dist/vendor/core/utils/element.js +21 -0
- package/dist/vendor/core/utils/global-state.d.ts +1 -0
- package/dist/vendor/core/utils/global-state.js +24 -0
- package/dist/vendor/core/utils/index.d.ts +2 -0
- package/dist/vendor/core/utils/index.js +2 -0
- package/package.json +74 -4
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
import { createFocusContainment, focusElement, focusFirst, getActiveElement, restoreFocus, } from "../focus/focus.js";
|
|
2
|
+
import { acquireSharedLayerStack } from "../layer/create-layer-stack.js";
|
|
3
|
+
import { isEscapeKey } from "../events/keyboard.js";
|
|
4
|
+
import { isPointerOutside } from "../events/pointer.js";
|
|
5
|
+
import { applyArrowStyles, createPositioner, DEFAULT_ARROW_SIZE, getArrowStyles, resetArrowStyles, snapshotArrowStyles, } from "../positioning/index.js";
|
|
6
|
+
import { parsePlacement } from "../positioning/placement.js";
|
|
7
|
+
import { lockBodyScroll } from "../scroll-lock/scroll-lock.js";
|
|
8
|
+
import { isBrowser } from "../utils/dom.js";
|
|
9
|
+
import { listen } from "../lifecycle/index.js";
|
|
10
|
+
import { getMenuItems } from "../menu/navigation.js";
|
|
11
|
+
import { acquireModalBackground } from "../modal/background.js";
|
|
12
|
+
import { moveToPortal, restorePortal, } from "../portal/portal.js";
|
|
13
|
+
import { getGlobalState } from "../utils/global-state.js";
|
|
14
|
+
import { createOverlayAnimator } from "./animator.js";
|
|
15
|
+
import { createContextMenuBehavior } from "./context-menu-behavior.js";
|
|
16
|
+
import { createTooltipBehavior } from "./tooltip-behavior.js";
|
|
17
|
+
const idCounter = getGlobalState("overlay-id", () => ({ value: 0 }));
|
|
18
|
+
function focusTarget(content, target) {
|
|
19
|
+
if (target === null || target === false) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (target === true || target === "first") {
|
|
23
|
+
if (!focusFirst(content)) {
|
|
24
|
+
focusElement(content);
|
|
25
|
+
}
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (target === "container") {
|
|
29
|
+
focusElement(content);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
focusElement(target);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Shared DOM controller used by the non-React adapters.
|
|
36
|
+
*
|
|
37
|
+
* It owns open state, positioning, layer registration, dismissal, focus, and
|
|
38
|
+
* component-specific ARIA. It does not render markup or impose styles.
|
|
39
|
+
*/
|
|
40
|
+
export function createOverlayController(initial) {
|
|
41
|
+
if (!isBrowser()) {
|
|
42
|
+
throw new Error("[overlay-core] createOverlayController requires a browser DOM.");
|
|
43
|
+
}
|
|
44
|
+
let options = initial;
|
|
45
|
+
let internalOpen = initial.defaultOpen ?? false;
|
|
46
|
+
let controlledOpen = initial.open ?? internalOpen;
|
|
47
|
+
let isControlled = initial.open !== undefined;
|
|
48
|
+
let disabled = initial.disabled ?? false;
|
|
49
|
+
const contentId = initial.content.id || `overlay-${++idCounter.value}-content`;
|
|
50
|
+
const doc = initial.content.ownerDocument;
|
|
51
|
+
initial.content.id = contentId;
|
|
52
|
+
if (initial.dir === "rtl") {
|
|
53
|
+
initial.content.dir = "rtl";
|
|
54
|
+
}
|
|
55
|
+
const listeners = new Set();
|
|
56
|
+
let previousActive = null;
|
|
57
|
+
let focusInside = false;
|
|
58
|
+
let layerHandle = null;
|
|
59
|
+
let lease = null;
|
|
60
|
+
let stack = null;
|
|
61
|
+
let portalStates = [];
|
|
62
|
+
let backgroundCleanup = null;
|
|
63
|
+
let scrollCleanup = null;
|
|
64
|
+
let containment = null;
|
|
65
|
+
let docCleanups = [];
|
|
66
|
+
let destroyed = false;
|
|
67
|
+
let latestResult = null;
|
|
68
|
+
let arrowElement = null;
|
|
69
|
+
let arrowSnapshot = null;
|
|
70
|
+
const arrowSupported = () => options.kind === "popover" || options.kind === "tooltip";
|
|
71
|
+
const arrowPositionerOption = () => arrowSupported() && options.arrow
|
|
72
|
+
? { element: options.arrow.element, padding: options.arrow.padding ?? 0 }
|
|
73
|
+
: null;
|
|
74
|
+
const syncArrowElement = () => {
|
|
75
|
+
const next = arrowSupported() ? (options.arrow ?? null) : null;
|
|
76
|
+
const nextElement = next?.element ?? null;
|
|
77
|
+
if (nextElement !== arrowElement) {
|
|
78
|
+
if (arrowElement && arrowSnapshot) {
|
|
79
|
+
resetArrowStyles(arrowElement, arrowSnapshot);
|
|
80
|
+
}
|
|
81
|
+
arrowElement = nextElement;
|
|
82
|
+
arrowSnapshot = nextElement ? snapshotArrowStyles(nextElement) : null;
|
|
83
|
+
}
|
|
84
|
+
if (arrowElement && next) {
|
|
85
|
+
// Size first so the positioner can measure the arrow before geometry arrives.
|
|
86
|
+
const size = `${next.size ?? DEFAULT_ARROW_SIZE}px`;
|
|
87
|
+
arrowElement.style.position = "absolute";
|
|
88
|
+
arrowElement.style.width = size;
|
|
89
|
+
arrowElement.style.height = size;
|
|
90
|
+
arrowElement.style.visibility = "hidden";
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
syncArrowElement();
|
|
94
|
+
const original = {
|
|
95
|
+
contentHidden: initial.content.hidden,
|
|
96
|
+
contentRole: initial.content.getAttribute("role"),
|
|
97
|
+
contentTabIndex: initial.content.getAttribute("tabindex"),
|
|
98
|
+
triggerExpanded: initial.trigger.getAttribute("aria-expanded"),
|
|
99
|
+
triggerControls: initial.trigger.getAttribute("aria-controls"),
|
|
100
|
+
triggerHasPopup: initial.trigger.getAttribute("aria-haspopup"),
|
|
101
|
+
triggerDescribedBy: initial.trigger.getAttribute("aria-describedby"),
|
|
102
|
+
backdropHidden: initial.backdrop?.hidden ?? null,
|
|
103
|
+
contentStyle: {
|
|
104
|
+
position: initial.content.style.position,
|
|
105
|
+
left: initial.content.style.left,
|
|
106
|
+
top: initial.content.style.top,
|
|
107
|
+
visibility: initial.content.style.visibility,
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
const currentOpen = () => disabled ? false : isControlled ? controlledOpen : internalOpen;
|
|
111
|
+
const host = {
|
|
112
|
+
doc,
|
|
113
|
+
getOptions: () => options,
|
|
114
|
+
isDisabled: () => disabled,
|
|
115
|
+
isOpen: currentOpen,
|
|
116
|
+
requestOpen: (reason) => requestOpen(reason),
|
|
117
|
+
requestClose: (reason) => requestClose(reason),
|
|
118
|
+
};
|
|
119
|
+
const animator = createOverlayAnimator(host, initial.animation, () => destroyed, () => finalizeClose());
|
|
120
|
+
const tooltip = createTooltipBehavior(host);
|
|
121
|
+
const contextMenu = createContextMenuBehavior(host);
|
|
122
|
+
const snapshot = () => ({
|
|
123
|
+
isOpen: currentOpen(),
|
|
124
|
+
placement: latestResult?.placement ?? options.placement ?? "bottom",
|
|
125
|
+
point: contextMenu.point,
|
|
126
|
+
});
|
|
127
|
+
const emitState = () => {
|
|
128
|
+
const state = snapshot();
|
|
129
|
+
for (const listener of [...listeners]) {
|
|
130
|
+
listener(state);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
const positioner = initial.kind === "dialog"
|
|
134
|
+
? createPositioner({ floating: null, reference: null })
|
|
135
|
+
: createPositioner({
|
|
136
|
+
reference: null,
|
|
137
|
+
floating: null,
|
|
138
|
+
placement: initial.placement ??
|
|
139
|
+
(initial.kind === "tooltip" ? "top" : "bottom-start"),
|
|
140
|
+
offset: initial.offset ?? 0,
|
|
141
|
+
alignOffset: initial.alignOffset ?? 0,
|
|
142
|
+
flip: initial.flip ?? true,
|
|
143
|
+
shift: initial.shift ?? true,
|
|
144
|
+
padding: initial.padding ?? 0,
|
|
145
|
+
boundary: initial.boundary,
|
|
146
|
+
strategy: initial.strategy,
|
|
147
|
+
autoUpdate: initial.autoUpdate ?? true,
|
|
148
|
+
arrow: arrowPositionerOption(),
|
|
149
|
+
});
|
|
150
|
+
const unsubscribePosition = positioner.subscribe((result) => {
|
|
151
|
+
latestResult = result;
|
|
152
|
+
if (!currentOpen() || options.kind === "dialog") {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const parsed = parsePlacement(result.placement);
|
|
156
|
+
const { style } = options.content;
|
|
157
|
+
style.position = result.strategy;
|
|
158
|
+
style.left = `${result.x}px`;
|
|
159
|
+
style.top = `${result.y}px`;
|
|
160
|
+
style.visibility = "visible";
|
|
161
|
+
options.content.dataset.side = parsed.side;
|
|
162
|
+
options.content.dataset.align = parsed.align;
|
|
163
|
+
options.content.dataset.placement = result.placement;
|
|
164
|
+
if (arrowElement) {
|
|
165
|
+
applyArrowStyles(arrowElement, getArrowStyles({
|
|
166
|
+
placement: result.placement,
|
|
167
|
+
arrow: result.arrow,
|
|
168
|
+
size: options.arrow?.size,
|
|
169
|
+
}));
|
|
170
|
+
}
|
|
171
|
+
if (animator.pendingEnter) {
|
|
172
|
+
animator.playEnter(result.placement);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
const setTriggerOpenState = (open) => {
|
|
176
|
+
const trigger = options.trigger;
|
|
177
|
+
trigger.dataset.state = open ? "open" : "closed";
|
|
178
|
+
if (options.kind !== "tooltip") {
|
|
179
|
+
trigger.setAttribute("aria-expanded", String(open));
|
|
180
|
+
}
|
|
181
|
+
if (open) {
|
|
182
|
+
trigger.setAttribute("aria-controls", contentId);
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
trigger.removeAttribute("aria-controls");
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
const setContentOpenState = (open) => {
|
|
189
|
+
const content = options.content;
|
|
190
|
+
content.dataset.state = open ? "open" : "closed";
|
|
191
|
+
content.hidden = !open;
|
|
192
|
+
if (open) {
|
|
193
|
+
content.style.visibility = "visible";
|
|
194
|
+
content.dataset.open = "";
|
|
195
|
+
delete content.dataset.closed;
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
delete content.dataset.open;
|
|
199
|
+
content.dataset.closed = "";
|
|
200
|
+
content.style.visibility = original.contentStyle.visibility;
|
|
201
|
+
}
|
|
202
|
+
const backdrop = options.backdrop;
|
|
203
|
+
if (backdrop) {
|
|
204
|
+
backdrop.hidden = !open;
|
|
205
|
+
backdrop.dataset.state = open ? "open" : "closed";
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
const ensureStack = () => {
|
|
209
|
+
if (initial.kind === "tooltip" || options.kind === "tooltip") {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
if (stack) {
|
|
213
|
+
return stack;
|
|
214
|
+
}
|
|
215
|
+
if (options.stack) {
|
|
216
|
+
stack = options.stack;
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
lease = acquireSharedLayerStack(doc);
|
|
220
|
+
stack = lease.stack;
|
|
221
|
+
}
|
|
222
|
+
return stack;
|
|
223
|
+
};
|
|
224
|
+
const registerLayer = () => {
|
|
225
|
+
const activeStack = ensureStack();
|
|
226
|
+
if (!activeStack || layerHandle) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
layerHandle = activeStack.register({
|
|
230
|
+
element: options.content,
|
|
231
|
+
reference: options.trigger,
|
|
232
|
+
boundaries: options.boundaries,
|
|
233
|
+
parent: options.parentLayer ?? null,
|
|
234
|
+
dismissOnEscape: options.dismissOnEscape ?? true,
|
|
235
|
+
dismissOnOutsidePress: options.dismissOnOutsidePress ?? true,
|
|
236
|
+
onDismiss: (reason) => {
|
|
237
|
+
requestClose(reason === "escape-key"
|
|
238
|
+
? "escape-key"
|
|
239
|
+
: reason === "outside-press"
|
|
240
|
+
? "outside-press"
|
|
241
|
+
: "reference-press");
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
};
|
|
245
|
+
const disposeLayer = () => {
|
|
246
|
+
layerHandle?.dispose();
|
|
247
|
+
layerHandle = null;
|
|
248
|
+
};
|
|
249
|
+
const syncPositionReference = () => {
|
|
250
|
+
if (options.kind === "dialog") {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const reference = options.kind === "context-menu"
|
|
254
|
+
? contextMenu.point
|
|
255
|
+
? {
|
|
256
|
+
getBoundingClientRect: () => ({
|
|
257
|
+
x: contextMenu.point?.x ?? 0,
|
|
258
|
+
y: contextMenu.point?.y ?? 0,
|
|
259
|
+
width: 0,
|
|
260
|
+
height: 0,
|
|
261
|
+
}),
|
|
262
|
+
}
|
|
263
|
+
: options.trigger
|
|
264
|
+
: options.trigger;
|
|
265
|
+
positioner.setReference(currentOpen() ? reference : null);
|
|
266
|
+
positioner.setFloating(currentOpen() ? options.content : null);
|
|
267
|
+
if (currentOpen()) {
|
|
268
|
+
void positioner.update();
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
const startDocumentListeners = () => {
|
|
272
|
+
if (docCleanups.length > 0) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (options.kind === "tooltip") {
|
|
276
|
+
docCleanups.push(listen(doc, "keydown", (event) => {
|
|
277
|
+
if (event.defaultPrevented || !isEscapeKey(event)) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
event.preventDefault();
|
|
281
|
+
requestClose("escape-key");
|
|
282
|
+
}, true), listen(doc, "pointerdown", (event) => {
|
|
283
|
+
if (options.content.contains(event.target) ||
|
|
284
|
+
options.trigger.contains(event.target) ||
|
|
285
|
+
!isPointerOutside(event, [options.content, options.trigger])) {
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
requestClose("outside-press");
|
|
289
|
+
}, true));
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (options.kind === "context-menu") {
|
|
293
|
+
docCleanups.push(listen(doc, "contextmenu", (event) => {
|
|
294
|
+
const target = event.target;
|
|
295
|
+
if (!target) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (options.content.contains(target) ||
|
|
299
|
+
options.trigger.contains(target)) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const element = target;
|
|
303
|
+
if (element.closest?.('[role="menu"]')) {
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
requestClose("outside-press");
|
|
307
|
+
}));
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
const stopDocumentListeners = () => {
|
|
311
|
+
for (const cleanup of docCleanups) {
|
|
312
|
+
cleanup();
|
|
313
|
+
}
|
|
314
|
+
docCleanups = [];
|
|
315
|
+
};
|
|
316
|
+
const restoreFocusAfterClose = () => {
|
|
317
|
+
if (options.kind === "tooltip" || options.restoreFocus === false) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
const active = getActiveElement(doc);
|
|
321
|
+
const inside = focusInside || (active !== null && options.content.contains(active));
|
|
322
|
+
focusInside = false;
|
|
323
|
+
if (!inside && active !== null && active !== doc.body) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const target = options.finalFocus ?? options.trigger ?? previousActive;
|
|
327
|
+
if (target) {
|
|
328
|
+
restoreFocus(target);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
const openState = () => {
|
|
332
|
+
if (destroyed || disabled) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
previousActive = getActiveElement(doc);
|
|
336
|
+
if (options.portal) {
|
|
337
|
+
const states = [];
|
|
338
|
+
// A backdrop that wraps the content moves as one unit.
|
|
339
|
+
const wrapped = options.backdrop?.contains(options.content) ?? false;
|
|
340
|
+
const contentState = wrapped
|
|
341
|
+
? null
|
|
342
|
+
: moveToPortal(options.content, options.portal);
|
|
343
|
+
if (contentState) {
|
|
344
|
+
states.push(contentState);
|
|
345
|
+
}
|
|
346
|
+
if (options.backdrop) {
|
|
347
|
+
const backdropState = moveToPortal(options.backdrop, options.portal);
|
|
348
|
+
if (backdropState) {
|
|
349
|
+
states.push(backdropState);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
portalStates = states;
|
|
353
|
+
}
|
|
354
|
+
animator.settleExit();
|
|
355
|
+
setContentOpenState(true);
|
|
356
|
+
if (options.dir === "rtl") {
|
|
357
|
+
options.content.dir = "rtl";
|
|
358
|
+
}
|
|
359
|
+
setTriggerOpenState(true);
|
|
360
|
+
if (options.kind === "tooltip") {
|
|
361
|
+
const base = original.triggerDescribedBy?.trim();
|
|
362
|
+
options.trigger.setAttribute("aria-describedby", [base, contentId].filter(Boolean).join(" "));
|
|
363
|
+
}
|
|
364
|
+
registerLayer();
|
|
365
|
+
startDocumentListeners();
|
|
366
|
+
animator.pendingEnter = animator.enabled && options.kind !== "dialog";
|
|
367
|
+
syncPositionReference();
|
|
368
|
+
if (animator.enabled && options.kind === "dialog") {
|
|
369
|
+
animator.playEnter();
|
|
370
|
+
}
|
|
371
|
+
if (options.kind === "dialog") {
|
|
372
|
+
const modal = options.modal ?? true;
|
|
373
|
+
options.content.setAttribute("role", "dialog");
|
|
374
|
+
if (modal) {
|
|
375
|
+
options.content.setAttribute("aria-modal", "true");
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
options.content.removeAttribute("aria-modal");
|
|
379
|
+
}
|
|
380
|
+
if (options.labelledBy) {
|
|
381
|
+
options.content.setAttribute("aria-labelledby", options.labelledBy);
|
|
382
|
+
}
|
|
383
|
+
if (options.describedBy) {
|
|
384
|
+
options.content.setAttribute("aria-describedby", options.describedBy);
|
|
385
|
+
}
|
|
386
|
+
if (!options.content.hasAttribute("tabindex")) {
|
|
387
|
+
options.content.setAttribute("tabindex", "-1");
|
|
388
|
+
}
|
|
389
|
+
if (modal) {
|
|
390
|
+
backgroundCleanup = acquireModalBackground(doc, [
|
|
391
|
+
options.content,
|
|
392
|
+
...(options.backdrop ? [options.backdrop] : []),
|
|
393
|
+
]);
|
|
394
|
+
if (options.lockScroll ?? true) {
|
|
395
|
+
scrollCleanup = lockBodyScroll(doc);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const initialTarget = options.initialFocus !== undefined
|
|
399
|
+
? options.initialFocus
|
|
400
|
+
: modal
|
|
401
|
+
? "container"
|
|
402
|
+
: null;
|
|
403
|
+
focusTarget(options.content, initialTarget);
|
|
404
|
+
if (options.trapFocus ?? modal) {
|
|
405
|
+
containment = createFocusContainment({
|
|
406
|
+
container: options.content,
|
|
407
|
+
enabled: true,
|
|
408
|
+
restoreFocus: false,
|
|
409
|
+
shouldContain: () => layerHandle?.isTopmost ?? true,
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
else if (options.kind === "context-menu") {
|
|
414
|
+
options.content.setAttribute("role", "menu");
|
|
415
|
+
if (!options.content.hasAttribute("tabindex")) {
|
|
416
|
+
options.content.setAttribute("tabindex", "-1");
|
|
417
|
+
}
|
|
418
|
+
const items = getMenuItems(options.content);
|
|
419
|
+
for (const item of items) {
|
|
420
|
+
if (!item.hasAttribute("tabindex")) {
|
|
421
|
+
item.setAttribute("tabindex", "-1");
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
const first = items[0];
|
|
425
|
+
if (options.initialFocus === null) {
|
|
426
|
+
// Explicitly opt out of initial focus.
|
|
427
|
+
}
|
|
428
|
+
else if (options.initialFocus instanceof Element) {
|
|
429
|
+
focusElement(options.initialFocus);
|
|
430
|
+
}
|
|
431
|
+
else if (first) {
|
|
432
|
+
first.focus();
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
options.content.focus();
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
else if (options.kind === "popover") {
|
|
439
|
+
const requested = options.initialFocus ?? false;
|
|
440
|
+
focusTarget(options.content, requested);
|
|
441
|
+
if (options.trapFocus) {
|
|
442
|
+
containment = createFocusContainment({
|
|
443
|
+
container: options.content,
|
|
444
|
+
enabled: true,
|
|
445
|
+
restoreFocus: false,
|
|
446
|
+
shouldContain: () => layerHandle?.isTopmost ?? true,
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
const finalizeClose = () => {
|
|
452
|
+
setContentOpenState(false);
|
|
453
|
+
delete options.content.dataset.animating;
|
|
454
|
+
for (const state of portalStates.reverse()) {
|
|
455
|
+
restorePortal(state);
|
|
456
|
+
}
|
|
457
|
+
portalStates = [];
|
|
458
|
+
};
|
|
459
|
+
const closeState = (immediate = false) => {
|
|
460
|
+
const wasShown = !options.content.hidden;
|
|
461
|
+
animator.cancelEnter();
|
|
462
|
+
animator.settleExit();
|
|
463
|
+
tooltip.reset();
|
|
464
|
+
contextMenu.reset();
|
|
465
|
+
stopDocumentListeners();
|
|
466
|
+
disposeLayer();
|
|
467
|
+
containment?.dispose();
|
|
468
|
+
containment = null;
|
|
469
|
+
backgroundCleanup?.();
|
|
470
|
+
backgroundCleanup = null;
|
|
471
|
+
scrollCleanup?.();
|
|
472
|
+
scrollCleanup = null;
|
|
473
|
+
restoreFocusAfterClose();
|
|
474
|
+
if (options.kind === "tooltip") {
|
|
475
|
+
const originalValue = original.triggerDescribedBy?.trim();
|
|
476
|
+
if (originalValue) {
|
|
477
|
+
options.trigger.setAttribute("aria-describedby", originalValue);
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
options.trigger.removeAttribute("aria-describedby");
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
setTriggerOpenState(false);
|
|
484
|
+
if (options.kind !== "dialog") {
|
|
485
|
+
positioner.setReference(null);
|
|
486
|
+
positioner.setFloating(null);
|
|
487
|
+
}
|
|
488
|
+
if (!immediate && animator.enabled && wasShown) {
|
|
489
|
+
animator.startExit(latestResult?.placement);
|
|
490
|
+
}
|
|
491
|
+
else {
|
|
492
|
+
finalizeClose();
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
const emitChange = (open, reason) => {
|
|
496
|
+
options.onOpenChange?.(open, reason);
|
|
497
|
+
emitState();
|
|
498
|
+
};
|
|
499
|
+
function requestOpen(reason = "programmatic") {
|
|
500
|
+
if (destroyed || disabled || currentOpen()) {
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
if (isControlled) {
|
|
504
|
+
emitChange(true, reason);
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
internalOpen = true;
|
|
508
|
+
openState();
|
|
509
|
+
emitChange(true, reason);
|
|
510
|
+
}
|
|
511
|
+
function requestClose(reason = "programmatic") {
|
|
512
|
+
if (destroyed || !currentOpen()) {
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
if (isControlled) {
|
|
516
|
+
emitChange(false, reason);
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
internalOpen = false;
|
|
520
|
+
closeState();
|
|
521
|
+
emitChange(false, reason);
|
|
522
|
+
}
|
|
523
|
+
function setOpen(next) {
|
|
524
|
+
if (destroyed || next === currentOpen()) {
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
if (isControlled) {
|
|
528
|
+
controlledOpen = next;
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
internalOpen = next;
|
|
532
|
+
}
|
|
533
|
+
if (next) {
|
|
534
|
+
openState();
|
|
535
|
+
}
|
|
536
|
+
else {
|
|
537
|
+
closeState();
|
|
538
|
+
}
|
|
539
|
+
emitState();
|
|
540
|
+
}
|
|
541
|
+
const open = (reason = "programmatic") => {
|
|
542
|
+
if (options.kind === "context-menu") {
|
|
543
|
+
contextMenu.point = null;
|
|
544
|
+
}
|
|
545
|
+
requestOpen(reason);
|
|
546
|
+
};
|
|
547
|
+
const close = (reason = "programmatic") => {
|
|
548
|
+
requestClose(reason);
|
|
549
|
+
};
|
|
550
|
+
const toggle = (reason = "trigger") => {
|
|
551
|
+
if (currentOpen()) {
|
|
552
|
+
requestClose(reason);
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
requestOpen(reason);
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
const update = async () => {
|
|
559
|
+
if (options.kind === "dialog" || !currentOpen()) {
|
|
560
|
+
return null;
|
|
561
|
+
}
|
|
562
|
+
return positioner.update();
|
|
563
|
+
};
|
|
564
|
+
const setOptions = (next) => {
|
|
565
|
+
options = { ...options, ...next };
|
|
566
|
+
if (next.animation !== undefined) {
|
|
567
|
+
animator.setConfig(next.animation);
|
|
568
|
+
}
|
|
569
|
+
if (next.arrow !== undefined) {
|
|
570
|
+
syncArrowElement();
|
|
571
|
+
positioner.setOptions({ arrow: arrowPositionerOption() });
|
|
572
|
+
}
|
|
573
|
+
if (next.disabled !== undefined) {
|
|
574
|
+
const wasOpen = currentOpen();
|
|
575
|
+
disabled = next.disabled;
|
|
576
|
+
if (disabled && wasOpen) {
|
|
577
|
+
closeState();
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
if (next.open !== undefined) {
|
|
581
|
+
isControlled = true;
|
|
582
|
+
if (next.open !== currentOpen()) {
|
|
583
|
+
controlledOpen = next.open;
|
|
584
|
+
if (next.open && !disabled) {
|
|
585
|
+
openState();
|
|
586
|
+
}
|
|
587
|
+
else {
|
|
588
|
+
closeState();
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
if (next.placement !== undefined ||
|
|
593
|
+
next.offset !== undefined ||
|
|
594
|
+
next.alignOffset !== undefined ||
|
|
595
|
+
next.flip !== undefined ||
|
|
596
|
+
next.shift !== undefined ||
|
|
597
|
+
next.padding !== undefined ||
|
|
598
|
+
next.boundary !== undefined ||
|
|
599
|
+
next.strategy !== undefined ||
|
|
600
|
+
next.autoUpdate !== undefined ||
|
|
601
|
+
next.dir !== undefined) {
|
|
602
|
+
if (next.dir === "rtl" && options.content.isConnected) {
|
|
603
|
+
options.content.dir = "rtl";
|
|
604
|
+
}
|
|
605
|
+
positioner.setOptions({
|
|
606
|
+
placement: next.placement ?? options.placement ?? "bottom",
|
|
607
|
+
offset: next.offset ?? options.offset,
|
|
608
|
+
alignOffset: next.alignOffset ?? options.alignOffset,
|
|
609
|
+
flip: next.flip ?? options.flip,
|
|
610
|
+
shift: next.shift ?? options.shift,
|
|
611
|
+
padding: next.padding ?? options.padding,
|
|
612
|
+
boundary: next.boundary ?? options.boundary,
|
|
613
|
+
strategy: next.strategy ?? options.strategy,
|
|
614
|
+
autoUpdate: next.autoUpdate ?? options.autoUpdate,
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
emitState();
|
|
618
|
+
};
|
|
619
|
+
const handleTriggerClick = () => {
|
|
620
|
+
if ((options.kind === "popover" || options.kind === "dialog") &&
|
|
621
|
+
options.toggleOnTriggerClick !== false) {
|
|
622
|
+
toggle("trigger");
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
const handleContentFocusIn = () => {
|
|
626
|
+
focusInside = true;
|
|
627
|
+
};
|
|
628
|
+
const handleContentFocusOut = (event) => {
|
|
629
|
+
const next = event.relatedTarget;
|
|
630
|
+
if (!next || !options.content.contains(next)) {
|
|
631
|
+
focusInside = false;
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
const triggerClick = options.kind === "popover" || options.kind === "dialog"
|
|
635
|
+
? handleTriggerClick
|
|
636
|
+
: null;
|
|
637
|
+
if (triggerClick) {
|
|
638
|
+
options.trigger.addEventListener("click", triggerClick);
|
|
639
|
+
}
|
|
640
|
+
if (options.kind === "tooltip") {
|
|
641
|
+
tooltip.attach();
|
|
642
|
+
}
|
|
643
|
+
if (options.kind === "context-menu") {
|
|
644
|
+
contextMenu.attach();
|
|
645
|
+
}
|
|
646
|
+
options.content.addEventListener("focusin", handleContentFocusIn);
|
|
647
|
+
options.content.addEventListener("focusout", handleContentFocusOut);
|
|
648
|
+
// Set kind-specific static semantics.
|
|
649
|
+
if (options.kind === "tooltip") {
|
|
650
|
+
if (!options.content.getAttribute("role")) {
|
|
651
|
+
options.content.setAttribute("role", "tooltip");
|
|
652
|
+
}
|
|
653
|
+
options.trigger.removeAttribute("aria-haspopup");
|
|
654
|
+
}
|
|
655
|
+
else if (options.kind === "dialog") {
|
|
656
|
+
options.trigger.setAttribute("aria-haspopup", "dialog");
|
|
657
|
+
}
|
|
658
|
+
else if (options.kind === "context-menu") {
|
|
659
|
+
options.trigger.setAttribute("aria-haspopup", "menu");
|
|
660
|
+
options.content.setAttribute("role", "menu");
|
|
661
|
+
}
|
|
662
|
+
if (options.trigger.id === "" && options.kind !== "tooltip") {
|
|
663
|
+
options.trigger.id = `${contentId}-trigger`;
|
|
664
|
+
}
|
|
665
|
+
setContentOpenState(false);
|
|
666
|
+
setTriggerOpenState(false);
|
|
667
|
+
syncPositionReference();
|
|
668
|
+
if (currentOpen()) {
|
|
669
|
+
openState();
|
|
670
|
+
}
|
|
671
|
+
else if (disabled) {
|
|
672
|
+
closeState();
|
|
673
|
+
}
|
|
674
|
+
emitState();
|
|
675
|
+
return {
|
|
676
|
+
contentId,
|
|
677
|
+
get isOpen() {
|
|
678
|
+
return currentOpen();
|
|
679
|
+
},
|
|
680
|
+
open,
|
|
681
|
+
openAt(nextPoint, reason = "programmatic") {
|
|
682
|
+
contextMenu.point = nextPoint;
|
|
683
|
+
requestOpen(reason);
|
|
684
|
+
},
|
|
685
|
+
close,
|
|
686
|
+
toggle,
|
|
687
|
+
setOpen,
|
|
688
|
+
setOptions,
|
|
689
|
+
update,
|
|
690
|
+
subscribe(listener) {
|
|
691
|
+
listener(snapshot());
|
|
692
|
+
listeners.add(listener);
|
|
693
|
+
return () => {
|
|
694
|
+
listeners.delete(listener);
|
|
695
|
+
};
|
|
696
|
+
},
|
|
697
|
+
destroy() {
|
|
698
|
+
if (destroyed) {
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
closeState(true);
|
|
702
|
+
destroyed = true;
|
|
703
|
+
if (arrowElement && arrowSnapshot) {
|
|
704
|
+
resetArrowStyles(arrowElement, arrowSnapshot);
|
|
705
|
+
}
|
|
706
|
+
arrowElement = null;
|
|
707
|
+
unsubscribePosition();
|
|
708
|
+
positioner.dispose();
|
|
709
|
+
if (triggerClick) {
|
|
710
|
+
options.trigger.removeEventListener("click", triggerClick);
|
|
711
|
+
}
|
|
712
|
+
tooltip.detach();
|
|
713
|
+
contextMenu.detach();
|
|
714
|
+
options.content.removeEventListener("focusin", handleContentFocusIn);
|
|
715
|
+
options.content.removeEventListener("focusout", handleContentFocusOut);
|
|
716
|
+
lease?.release();
|
|
717
|
+
lease = null;
|
|
718
|
+
stack = null;
|
|
719
|
+
listeners.clear();
|
|
720
|
+
options = initial;
|
|
721
|
+
},
|
|
722
|
+
};
|
|
723
|
+
}
|