@bevelo/dom 0.0.0-stage → 0.1.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 +40 -2
- package/dist/index.cjs +4709 -0
- package/dist/index.d.cts +1854 -0
- package/dist/index.d.ts +1854 -0
- package/dist/index.js +4583 -0
- package/package.json +38 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,4583 @@
|
|
|
1
|
+
import { ENDING_STYLE_ATTRIBUTE, STARTING_STYLE_ATTRIBUTE } from "@bevelo/runtime";
|
|
2
|
+
// src/foundation/platform/environment.ts
|
|
3
|
+
/** A hint is Apple-shaped if any of the three places it can appear says so. Used
|
|
4
|
+
* case-insensitively because UA strings are not consistent about it. */
|
|
5
|
+
const APPLE_HINT = /mac|iphone|ipad|ipod/i;
|
|
6
|
+
/** A device reports touch through the point count or through a mobile UA; a
|
|
7
|
+
* touchscreen laptop is honestly both. */
|
|
8
|
+
const TOUCH_HINT = /mobi|android|iphone|ipad|ipod|tablet/i;
|
|
9
|
+
/**
|
|
10
|
+
* The first hint that is actually a string.
|
|
11
|
+
*
|
|
12
|
+
* Taking one field at a time, in the order browsers moved, is deliberate: joining
|
|
13
|
+
* all three and testing the result lets a UA string that mentions a Mac win over a
|
|
14
|
+
* `platform` that says Windows, and the more specific field should answer first.
|
|
15
|
+
* An empty or blank field is skipped rather than counted as an answer, because
|
|
16
|
+
* "`userAgentData.platform` is `""`" is what a privacy browser sends.
|
|
17
|
+
*/
|
|
18
|
+
function firstHint(probe) {
|
|
19
|
+
for (const hint of [
|
|
20
|
+
probe.userAgentDataPlatform,
|
|
21
|
+
probe.platform,
|
|
22
|
+
probe.userAgent
|
|
23
|
+
]) if (typeof hint === "string" && hint.trim() !== "") return hint;
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Whether a Mac, iPhone or iPad is running the page, which decides between ⌘ and
|
|
28
|
+
* Ctrl in a hint and between the two cap orders.
|
|
29
|
+
*
|
|
30
|
+
* "other" is the answer for a server, a worker and every failed probe: a hint that
|
|
31
|
+
* prints Ctrl on a Mac is wrong but readable, while a glyph the platform does not
|
|
32
|
+
* have is not.
|
|
33
|
+
* @experimental
|
|
34
|
+
*/
|
|
35
|
+
function platformOf(probe) {
|
|
36
|
+
return APPLE_HINT.test(firstHint(probe)) ? "mac" : "other";
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether the device has a touchscreen, which is not the same question as whether
|
|
40
|
+
* it is being used by touch: a touchscreen laptop answers true here and "mouse" to
|
|
41
|
+
* the media layer. Both answers are needed — this one decides whether to offer
|
|
42
|
+
* touch affordances at all, and the media snapshot decides which affordance is the
|
|
43
|
+
* primary one.
|
|
44
|
+
*
|
|
45
|
+
* `maxTouchPoints` is preferred over the UA: it is what the browser knows about
|
|
46
|
+
* the hardware rather than what it decided to say about itself, and an iPad in
|
|
47
|
+
* desktop mode reports a Mac UA with 5 points.
|
|
48
|
+
* @experimental
|
|
49
|
+
*/
|
|
50
|
+
function isTouchDevice(probe) {
|
|
51
|
+
const points = probe.maxTouchPoints;
|
|
52
|
+
if (typeof points === "number" && Number.isFinite(points) && points > 0) return true;
|
|
53
|
+
return TOUCH_HINT.test(probe.userAgent ?? "");
|
|
54
|
+
}
|
|
55
|
+
// src/foundation/platform/keycaps.ts
|
|
56
|
+
/**
|
|
57
|
+
* The platform's modifier order, left to right.
|
|
58
|
+
*
|
|
59
|
+
* Apple prints ⌃⌥⇧⌘ — Command last, because it is the modifier every shortcut
|
|
60
|
+
* holds and the eye should land on the distinctive key in the middle. Windows
|
|
61
|
+
* prints the Windows key first ("Win+Shift+S"), and its documentation writes
|
|
62
|
+
* Ctrl+Alt+ before Shift. A single order for both is what puts Ctrl before ⌘ on a
|
|
63
|
+
* Mac and looks like a typo to every user of one.
|
|
64
|
+
* @experimental
|
|
65
|
+
*/
|
|
66
|
+
const MAC_ORDER = Object.freeze([
|
|
67
|
+
"ctrl",
|
|
68
|
+
"alt",
|
|
69
|
+
"shift",
|
|
70
|
+
"meta"
|
|
71
|
+
]);
|
|
72
|
+
const OTHER_ORDER = Object.freeze([
|
|
73
|
+
"meta",
|
|
74
|
+
"ctrl",
|
|
75
|
+
"alt",
|
|
76
|
+
"shift"
|
|
77
|
+
]);
|
|
78
|
+
/**
|
|
79
|
+
* The order the caps are drawn in, left to right.
|
|
80
|
+
* @experimental
|
|
81
|
+
*/
|
|
82
|
+
function keycapOrder(platform) {
|
|
83
|
+
return platform === "mac" ? MAC_ORDER : OTHER_ORDER;
|
|
84
|
+
}
|
|
85
|
+
/** The face of each modifier key on each platform. "Win" rather than "Meta"
|
|
86
|
+
* because no Windows user calls it Meta and the cap is the hardware's own
|
|
87
|
+
* label; Linux hosts pay for it, and `aria-keyshortcuts` stays correct either way. */
|
|
88
|
+
const MODIFIER_LABELS = {
|
|
89
|
+
mac: {
|
|
90
|
+
ctrl: "⌃",
|
|
91
|
+
alt: "⌥",
|
|
92
|
+
shift: "⇧",
|
|
93
|
+
meta: "⌘"
|
|
94
|
+
},
|
|
95
|
+
other: {
|
|
96
|
+
ctrl: "Ctrl",
|
|
97
|
+
alt: "Alt",
|
|
98
|
+
shift: "Shift",
|
|
99
|
+
meta: "Win"
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* The keys whose printed face is worth a table.
|
|
104
|
+
*
|
|
105
|
+
* Arrows are glyphs on both platforms, which is the one place this table differs
|
|
106
|
+
* from the string formatter: "ArrowUp" in a 24px cap is a reflow, and every
|
|
107
|
+
* keyboard, Mac or PC, prints an arrow. A named key that is missing here is
|
|
108
|
+
* title-cased ("f1" → "F1"), so the table only holds what a keyboard actually
|
|
109
|
+
* prints.
|
|
110
|
+
*/
|
|
111
|
+
const KEY_LABELS = {
|
|
112
|
+
" ": {
|
|
113
|
+
mac: "Space",
|
|
114
|
+
other: "Space"
|
|
115
|
+
},
|
|
116
|
+
escape: {
|
|
117
|
+
mac: "Esc",
|
|
118
|
+
other: "Esc"
|
|
119
|
+
},
|
|
120
|
+
enter: {
|
|
121
|
+
mac: "↩",
|
|
122
|
+
other: "Enter"
|
|
123
|
+
},
|
|
124
|
+
backspace: {
|
|
125
|
+
mac: "⌫",
|
|
126
|
+
other: "Backspace"
|
|
127
|
+
},
|
|
128
|
+
delete: {
|
|
129
|
+
mac: "⌦",
|
|
130
|
+
other: "Delete"
|
|
131
|
+
},
|
|
132
|
+
tab: {
|
|
133
|
+
mac: "⇥",
|
|
134
|
+
other: "Tab"
|
|
135
|
+
},
|
|
136
|
+
arrowup: {
|
|
137
|
+
mac: "↑",
|
|
138
|
+
other: "↑"
|
|
139
|
+
},
|
|
140
|
+
arrowdown: {
|
|
141
|
+
mac: "↓",
|
|
142
|
+
other: "↓"
|
|
143
|
+
},
|
|
144
|
+
arrowleft: {
|
|
145
|
+
mac: "←",
|
|
146
|
+
other: "←"
|
|
147
|
+
},
|
|
148
|
+
arrowright: {
|
|
149
|
+
mac: "→",
|
|
150
|
+
other: "→"
|
|
151
|
+
},
|
|
152
|
+
pageup: {
|
|
153
|
+
mac: "PageUp",
|
|
154
|
+
other: "PageUp"
|
|
155
|
+
},
|
|
156
|
+
pagedown: {
|
|
157
|
+
mac: "PageDown",
|
|
158
|
+
other: "PageDown"
|
|
159
|
+
},
|
|
160
|
+
home: {
|
|
161
|
+
mac: "Home",
|
|
162
|
+
other: "Home"
|
|
163
|
+
},
|
|
164
|
+
end: {
|
|
165
|
+
mac: "End",
|
|
166
|
+
other: "End"
|
|
167
|
+
},
|
|
168
|
+
insert: {
|
|
169
|
+
mac: "Insert",
|
|
170
|
+
other: "Insert"
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
/** Uppercases ASCII letters only. A cap shows the key's own face, and
|
|
174
|
+
* `toUpperCase` turns "ß" into "SS" and "é" into "É" — neither is printed
|
|
175
|
+
* anywhere. */
|
|
176
|
+
function asciiUpper(text) {
|
|
177
|
+
return text.replace(/[a-z]/g, (letter) => letter.toUpperCase());
|
|
178
|
+
}
|
|
179
|
+
/** The label for a key that is not in the table. */
|
|
180
|
+
function genericKeyLabel(name) {
|
|
181
|
+
const characters = [...name];
|
|
182
|
+
if (characters.length === 1) return asciiUpper(name);
|
|
183
|
+
const first = characters[0] ?? "";
|
|
184
|
+
return asciiUpper(first) + name.slice(first.length);
|
|
185
|
+
}
|
|
186
|
+
/** The label for the main key of the chord. */
|
|
187
|
+
function keyLabel(name, platform) {
|
|
188
|
+
const lower = name.toLowerCase();
|
|
189
|
+
const named = KEY_LABELS[lower];
|
|
190
|
+
if (named !== void 0) return named[platform];
|
|
191
|
+
return genericKeyLabel(name);
|
|
192
|
+
}
|
|
193
|
+
/** The modifiers of the chord, resolved, de-duplicated and ordered. */
|
|
194
|
+
function modifiersOf(chord, platform) {
|
|
195
|
+
const written = /* @__PURE__ */ new Set();
|
|
196
|
+
if (chord.ctrl === true) written.add("ctrl");
|
|
197
|
+
if (chord.alt === true) written.add("alt");
|
|
198
|
+
if (chord.shift === true) written.add("shift");
|
|
199
|
+
if (chord.meta === true) written.add("meta");
|
|
200
|
+
if (chord.mod === true) written.add(platform === "mac" ? "meta" : "ctrl");
|
|
201
|
+
return keycapOrder(platform).filter((modifier) => written.has(modifier));
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* The caps for a chord: its modifiers in the platform's order, then the key.
|
|
205
|
+
*
|
|
206
|
+
* A chord with no main key answers its modifiers alone — a lone "shift" is what a
|
|
207
|
+
* chord written as a modifier renders as, and returning a cap for an empty key
|
|
208
|
+
* would draw a blank box. A chord that names nothing at all answers no caps, which
|
|
209
|
+
* is what a caller that failed to parse should get rather than a placeholder.
|
|
210
|
+
* @experimental
|
|
211
|
+
*/
|
|
212
|
+
function keycaps(chord, platform) {
|
|
213
|
+
const caps = [];
|
|
214
|
+
for (const modifier of modifiersOf(chord, platform)) caps.push({
|
|
215
|
+
kind: "modifier",
|
|
216
|
+
name: modifier,
|
|
217
|
+
label: MODIFIER_LABELS[platform][modifier]
|
|
218
|
+
});
|
|
219
|
+
if (chord.key !== "") caps.push({
|
|
220
|
+
kind: "key",
|
|
221
|
+
name: chord.key.toLowerCase(),
|
|
222
|
+
label: keyLabel(chord.key, platform)
|
|
223
|
+
});
|
|
224
|
+
return caps;
|
|
225
|
+
}
|
|
226
|
+
// src/foundation/platform/viewport.ts
|
|
227
|
+
/** A length the page can draw in, or 0.
|
|
228
|
+
*
|
|
229
|
+
* Zero is the honest answer for an unusable one: a `display: none` iframe really
|
|
230
|
+
* has no viewport, and clamping to 1px instead would let a solver place a popup in
|
|
231
|
+
* a page that has no room. The alternative — propagating NaN — poisons every
|
|
232
|
+
* comparison downstream, and the alternative — throwing — turns a hidden iframe
|
|
233
|
+
* into a crash. */
|
|
234
|
+
function lengthOf(value) {
|
|
235
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
236
|
+
}
|
|
237
|
+
/** A length that has no meaning at zero, so 0 means "not reported" and the
|
|
238
|
+
* fallback is used. */
|
|
239
|
+
function lengthOr(value, fallback) {
|
|
240
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : fallback;
|
|
241
|
+
}
|
|
242
|
+
/** An offset: any finite number, including a negative one. Safari reports a
|
|
243
|
+
* negative `offsetTop` while its rubber band overshoots, and clamping it to 0
|
|
244
|
+
* would make the content jump by the overshoot when the bounce settles. */
|
|
245
|
+
function offsetOf(value) {
|
|
246
|
+
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
247
|
+
}
|
|
248
|
+
/** A safe-area inset. Negative is not a thing the platform can mean, and a
|
|
249
|
+
* fraction is: a 2x display draws a 47.5px home indicator as 95 device pixels. */
|
|
250
|
+
function insetOf(value) {
|
|
251
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* The metrics a sample describes.
|
|
255
|
+
*
|
|
256
|
+
* A window without a visual viewport still has one in theory — the layout
|
|
257
|
+
* viewport divided by the zoom — so a sample that reports a scale of 2 and no
|
|
258
|
+
* visual size answers a visual viewport half the layout one. That is exactly what
|
|
259
|
+
* a zoomed page looks like, and it is the rule that keeps the two sizes from
|
|
260
|
+
* disagreeing when only one of them was read.
|
|
261
|
+
* @experimental
|
|
262
|
+
*/
|
|
263
|
+
function viewportMetrics(sample) {
|
|
264
|
+
const scale = lengthOr(sample.scale, 1);
|
|
265
|
+
const layout = {
|
|
266
|
+
width: lengthOf(sample.layoutWidth),
|
|
267
|
+
height: lengthOf(sample.layoutHeight)
|
|
268
|
+
};
|
|
269
|
+
const visual = {
|
|
270
|
+
width: lengthOr(sample.visualWidth, layout.width / scale),
|
|
271
|
+
height: lengthOr(sample.visualHeight, layout.height / scale)
|
|
272
|
+
};
|
|
273
|
+
const pixelRatio = lengthOr(sample.pixelRatio, 1);
|
|
274
|
+
const safeArea = sample.safeArea;
|
|
275
|
+
return {
|
|
276
|
+
layout,
|
|
277
|
+
visual,
|
|
278
|
+
offset: {
|
|
279
|
+
x: offsetOf(sample.offsetX),
|
|
280
|
+
y: offsetOf(sample.offsetY)
|
|
281
|
+
},
|
|
282
|
+
scale,
|
|
283
|
+
pixelRatio,
|
|
284
|
+
safeArea: {
|
|
285
|
+
top: insetOf(safeArea?.top),
|
|
286
|
+
right: insetOf(safeArea?.right),
|
|
287
|
+
bottom: insetOf(safeArea?.bottom),
|
|
288
|
+
left: insetOf(safeArea?.left)
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* The part of the layout viewport the user can see, in layout coordinates.
|
|
294
|
+
*
|
|
295
|
+
* This is the rectangle an overlay must fit inside: not the layout viewport, which
|
|
296
|
+
* extends under the on-screen keyboard, and not the window size, which is the same
|
|
297
|
+
* number as the layout viewport only while nothing is zoomed.
|
|
298
|
+
* @experimental
|
|
299
|
+
*/
|
|
300
|
+
function visibleRect(metrics) {
|
|
301
|
+
return {
|
|
302
|
+
x: metrics.offset.x,
|
|
303
|
+
y: metrics.offset.y,
|
|
304
|
+
width: metrics.visual.width,
|
|
305
|
+
height: metrics.visual.height
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* How much of the layout viewport's bottom the user cannot see, in CSS pixels.
|
|
310
|
+
*
|
|
311
|
+
* A keyboard inset is this number capped by a measured keyboard height. The cap
|
|
312
|
+
* is not here because the keyboard's height is not in the metrics:
|
|
313
|
+
* it is a difference between two measurements taken at different times (before
|
|
314
|
+
* and after focus), which is history, and a value cannot hold history honestly.
|
|
315
|
+
* A caller that measured a keyboard height caps this number itself.
|
|
316
|
+
* @experimental
|
|
317
|
+
*/
|
|
318
|
+
function coveredBottom(metrics) {
|
|
319
|
+
return Math.max(0, metrics.layout.height - metrics.offset.y - metrics.visual.height);
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Whether the page is zoomed away from 1, in either direction.
|
|
323
|
+
*
|
|
324
|
+
* A pinch-out (scale below 1) is as much a zoom as a pinch-in, and the reason to
|
|
325
|
+
* ask is the same: a fixed overlay sized from the layout viewport is wrong by the
|
|
326
|
+
* factor. The tolerance absorbs the sub-percent jitter a trackpad pinch leaves
|
|
327
|
+
* behind, which would otherwise make every measurement look new.
|
|
328
|
+
* @experimental
|
|
329
|
+
*/
|
|
330
|
+
function isZoomed(metrics, tolerance = .01) {
|
|
331
|
+
return Math.abs(metrics.scale - 1) > tolerance;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* How much of a layout-space rectangle the user can see.
|
|
335
|
+
*
|
|
336
|
+
* `fraction` is the visible area over the whole area; a rectangle with no area (a
|
|
337
|
+
* caret, a zero-width anchor) counts as fully visible when it is inside, because
|
|
338
|
+
* the alternative — 0/0 — makes the one component that positions a caret
|
|
339
|
+
* impossible to write. A rectangle that ended up with a NaN size is not visible:
|
|
340
|
+
* a measurement that failed must not read as an element on screen.
|
|
341
|
+
* @experimental
|
|
342
|
+
*/
|
|
343
|
+
function onScreen(metrics, rect) {
|
|
344
|
+
const area = rect.width * rect.height;
|
|
345
|
+
if (!Number.isFinite(area) || !Number.isFinite(rect.x) || !Number.isFinite(rect.y)) return {
|
|
346
|
+
visible: false,
|
|
347
|
+
fraction: 0
|
|
348
|
+
};
|
|
349
|
+
const seen = visibleRect(metrics);
|
|
350
|
+
const width = Math.max(0, Math.min(rect.x + rect.width, seen.x + seen.width) - Math.max(rect.x, seen.x));
|
|
351
|
+
const height = Math.max(0, Math.min(rect.y + rect.height, seen.y + seen.height) - Math.max(rect.y, seen.y));
|
|
352
|
+
if (area === 0) {
|
|
353
|
+
const inside = rect.x >= seen.x && rect.x <= seen.x + seen.width && rect.y >= seen.y && rect.y <= seen.y + seen.height;
|
|
354
|
+
return {
|
|
355
|
+
visible: inside,
|
|
356
|
+
fraction: inside ? 1 : 0
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
const fraction = width * height / area;
|
|
360
|
+
return {
|
|
361
|
+
visible: width > 0 && height > 0,
|
|
362
|
+
fraction: Math.min(1, fraction)
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
/** The sub-pixel difference below which two measurements are the same
|
|
366
|
+
* viewport. Half a CSS pixel is the smallest difference a 2x display can show as
|
|
367
|
+
* a device pixel, and a fractional layout viewport moves by that much on every
|
|
368
|
+
* scrollbar that appears. */
|
|
369
|
+
const SAME_PIXELS = .5;
|
|
370
|
+
/** The difference below which two ratios are the same zoom or the same pixel
|
|
371
|
+
* ratio; these are not lengths, so a half-pixel tolerance would call a 1.5x
|
|
372
|
+
* zoom equal to 1x. */
|
|
373
|
+
const SAME_RATIO = .001;
|
|
374
|
+
function sameLength(a, b, tolerance) {
|
|
375
|
+
return Math.abs(a - b) <= tolerance;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Whether two reads describe the same viewport, so an adapter can stay quiet.
|
|
379
|
+
*
|
|
380
|
+
* A resize listener fires for a scrollbar appearing, for a URL bar collapsing and
|
|
381
|
+
* for a sub-pixel layout change nobody can see; notifying on those makes every
|
|
382
|
+
* subscriber re-place itself for nothing. Lengths compare with a half-pixel
|
|
383
|
+
* tolerance, the scale and the pixel ratio with a per-mille one.
|
|
384
|
+
* @experimental
|
|
385
|
+
*/
|
|
386
|
+
function sameViewport(a, b) {
|
|
387
|
+
return sameLength(a.layout.width, b.layout.width, SAME_PIXELS) && sameLength(a.layout.height, b.layout.height, SAME_PIXELS) && sameLength(a.visual.width, b.visual.width, SAME_PIXELS) && sameLength(a.visual.height, b.visual.height, SAME_PIXELS) && sameLength(a.offset.x, b.offset.x, SAME_PIXELS) && sameLength(a.offset.y, b.offset.y, SAME_PIXELS) && sameLength(a.safeArea.top, b.safeArea.top, SAME_PIXELS) && sameLength(a.safeArea.right, b.safeArea.right, SAME_PIXELS) && sameLength(a.safeArea.bottom, b.safeArea.bottom, SAME_PIXELS) && sameLength(a.safeArea.left, b.safeArea.left, SAME_PIXELS) && sameLength(a.scale, b.scale, SAME_RATIO) && sameLength(a.pixelRatio, b.pixelRatio, SAME_RATIO);
|
|
388
|
+
}
|
|
389
|
+
// src/foundation/platform/media.ts
|
|
390
|
+
/**
|
|
391
|
+
* The query strings, in one place, so the adapter and a stylesheet cannot drift.
|
|
392
|
+
*
|
|
393
|
+
* `prefers-color-scheme: light` is deliberately absent: the absence of `dark` is
|
|
394
|
+
* light, and a browser that matches neither is a browser with no preference, which
|
|
395
|
+
* CSS resolves the same way.
|
|
396
|
+
* @experimental
|
|
397
|
+
*/
|
|
398
|
+
const MEDIA_QUERIES = Object.freeze({
|
|
399
|
+
reducedMotion: "(prefers-reduced-motion: reduce)",
|
|
400
|
+
dark: "(prefers-color-scheme: dark)",
|
|
401
|
+
moreContrast: "(prefers-contrast: more)",
|
|
402
|
+
lessContrast: "(prefers-contrast: less)",
|
|
403
|
+
coarsePointer: "(pointer: coarse)",
|
|
404
|
+
finePointer: "(pointer: fine)",
|
|
405
|
+
anyCoarsePointer: "(any-pointer: coarse)",
|
|
406
|
+
anyFinePointer: "(any-pointer: fine)",
|
|
407
|
+
hover: "(hover: hover)",
|
|
408
|
+
anyHover: "(any-hover: hover)",
|
|
409
|
+
portrait: "(orientation: portrait)"
|
|
410
|
+
});
|
|
411
|
+
/** A length that has no meaning at zero: 0 means "not reported". */
|
|
412
|
+
function reported(value) {
|
|
413
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Resolve the pair of pointer queries.
|
|
417
|
+
*
|
|
418
|
+
* The two queries are not complements: a device with no pointer matches neither,
|
|
419
|
+
* and a device with a mouse and a touchscreen matches `fine` while `any-pointer`
|
|
420
|
+
* matches both. So an explicit match wins, a pair of explicit "no" answers means
|
|
421
|
+
* no pointer, a single answer implies the other, and two unknowns go to the
|
|
422
|
+
* fallback. The rejected alternative is a single `coarse` check, which calls every
|
|
423
|
+
* touch device coarse and every no-pointer device fine.
|
|
424
|
+
*/
|
|
425
|
+
function pointerOf(fine, coarse, fallback) {
|
|
426
|
+
if (fine === true) return "fine";
|
|
427
|
+
if (coarse === true) return "coarse";
|
|
428
|
+
if (fine === false && coarse === false) return "none";
|
|
429
|
+
if (fine === void 0 && coarse === void 0) return fallback;
|
|
430
|
+
return fine === false ? "coarse" : "fine";
|
|
431
|
+
}
|
|
432
|
+
/** An answer, or the fallback when the environment could not answer. */
|
|
433
|
+
function answered(value, fallback) {
|
|
434
|
+
return value === void 0 ? fallback : value;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* The snapshot the answers describe.
|
|
438
|
+
*
|
|
439
|
+
* Orientation comes from the width and height rather than from a query, even
|
|
440
|
+
* though `(orientation: portrait)` exists: the two would have to agree in the same
|
|
441
|
+
* frame anyway, a square viewport is portrait by the specification, and a number
|
|
442
|
+
* the adapter already read cannot be a frame behind the query beside it. The
|
|
443
|
+
* query is used only when the sizes are unknown, which is the server's case.
|
|
444
|
+
*
|
|
445
|
+
* The fallbacks for an unanswerable environment are conservative on purpose: no
|
|
446
|
+
* pointer known means no pointer, which makes a component show keyboard
|
|
447
|
+
* affordances — always correct, if occasionally not optimal — instead of a hover
|
|
448
|
+
* state a finger can never trigger.
|
|
449
|
+
* @experimental
|
|
450
|
+
*/
|
|
451
|
+
function mediaSnapshot(facts) {
|
|
452
|
+
const width = reported(facts.width);
|
|
453
|
+
const height = reported(facts.height);
|
|
454
|
+
const pointerFallback = typeof facts.touchPoints === "number" && facts.touchPoints > 0 ? "coarse" : "none";
|
|
455
|
+
const pointer = pointerOf(facts.matches(MEDIA_QUERIES.finePointer), facts.matches(MEDIA_QUERIES.coarsePointer), pointerFallback);
|
|
456
|
+
const anyPointer = pointerOf(facts.matches(MEDIA_QUERIES.anyFinePointer), facts.matches(MEDIA_QUERIES.anyCoarsePointer), pointer === "none" ? pointerFallback : pointer);
|
|
457
|
+
const hoverAnswer = facts.matches(MEDIA_QUERIES.hover);
|
|
458
|
+
const hover = answered(hoverAnswer, anyPointer === "fine");
|
|
459
|
+
const anyHover = answered(facts.matches(MEDIA_QUERIES.anyHover), hoverAnswer === void 0 ? anyPointer === "fine" : hover);
|
|
460
|
+
const orientation = width > 0 && height > 0 ? height >= width ? "portrait" : "landscape" : answered(facts.matches(MEDIA_QUERIES.portrait), false) ? "portrait" : "landscape";
|
|
461
|
+
const contrast = facts.matches(MEDIA_QUERIES.moreContrast) === true ? "more" : facts.matches(MEDIA_QUERIES.lessContrast) === true ? "less" : "no-preference";
|
|
462
|
+
return {
|
|
463
|
+
reducedMotion: answered(facts.matches(MEDIA_QUERIES.reducedMotion), false),
|
|
464
|
+
colorScheme: facts.matches(MEDIA_QUERIES.dark) === true ? "dark" : "light",
|
|
465
|
+
contrast,
|
|
466
|
+
pointer,
|
|
467
|
+
anyPointer,
|
|
468
|
+
hover,
|
|
469
|
+
anyHover,
|
|
470
|
+
orientation,
|
|
471
|
+
width,
|
|
472
|
+
height
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Read the input profile.
|
|
477
|
+
*
|
|
478
|
+
* The table has one row that is not obvious: a fine pointer that cannot hover is a
|
|
479
|
+
* stylus, not a mouse — a mouse always hovers, and a pen hovering over a tablet is
|
|
480
|
+
* a different interaction. A coarse primary pointer on a device that can hover is
|
|
481
|
+
* a tablet with a trackpad, which is the one case where both the touch target and
|
|
482
|
+
* the hover hint are right.
|
|
483
|
+
* @experimental
|
|
484
|
+
*/
|
|
485
|
+
function inputProfile(snapshot) {
|
|
486
|
+
return {
|
|
487
|
+
kind: snapshot.pointer === "none" ? "keyboard" : snapshot.pointer === "fine" ? snapshot.hover ? "mouse" : "stylus" : snapshot.anyHover ? "hybrid" : "touch",
|
|
488
|
+
hovers: snapshot.anyHover,
|
|
489
|
+
coarse: snapshot.pointer === "coarse"
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* The default ladder, mobile-first.
|
|
494
|
+
*
|
|
495
|
+
* These are the widths the kit's own sheets use; they are exported rather than
|
|
496
|
+
* inlined because a component that asks for "lg" and a stylesheet that starts its
|
|
497
|
+
* `lg:` at another width is the drift this layer exists to stop. A host with its
|
|
498
|
+
* own scale passes it to {@link breakpointOf} instead of editing this one.
|
|
499
|
+
* @experimental
|
|
500
|
+
*/
|
|
501
|
+
const BREAKPOINTS = Object.freeze([
|
|
502
|
+
{
|
|
503
|
+
name: "base",
|
|
504
|
+
minWidth: 0
|
|
505
|
+
},
|
|
506
|
+
{
|
|
507
|
+
name: "sm",
|
|
508
|
+
minWidth: 640
|
|
509
|
+
},
|
|
510
|
+
{
|
|
511
|
+
name: "md",
|
|
512
|
+
minWidth: 768
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
name: "lg",
|
|
516
|
+
minWidth: 1024
|
|
517
|
+
},
|
|
518
|
+
{
|
|
519
|
+
name: "xl",
|
|
520
|
+
minWidth: 1280
|
|
521
|
+
},
|
|
522
|
+
{
|
|
523
|
+
name: "2xl",
|
|
524
|
+
minWidth: 1536
|
|
525
|
+
}
|
|
526
|
+
]);
|
|
527
|
+
/** The rung a ladder with nothing usable in it falls back to. */
|
|
528
|
+
const BASE_BREAKPOINT = Object.freeze({
|
|
529
|
+
name: "base",
|
|
530
|
+
minWidth: 0
|
|
531
|
+
});
|
|
532
|
+
/**
|
|
533
|
+
* A ladder with its rungs in the order they answer in.
|
|
534
|
+
*
|
|
535
|
+
* The caller's ladder is sorted rather than trusted, because a host that declared
|
|
536
|
+
* its scale in the order that reads best ("sm, lg, md") would otherwise get the
|
|
537
|
+
* wrong rung for every width between two of them. An entry with an unusable width
|
|
538
|
+
* is dropped; a later entry wins a tie, because a host that lists two names for
|
|
539
|
+
* one width meant the more specific one to answer.
|
|
540
|
+
*/
|
|
541
|
+
function sortedLadder(ladder) {
|
|
542
|
+
return ladder.filter((rung) => Number.isFinite(rung.minWidth) && rung.minWidth >= 0).sort((a, b) => a.minWidth - b.minWidth);
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* The rung a width falls in.
|
|
546
|
+
*
|
|
547
|
+
* A width narrower than the ladder's first rung answers the first rung: a host
|
|
548
|
+
* whose scale starts at 768px still needs a name for a phone, and answering
|
|
549
|
+
* "nothing" would make every component handle a fourth state. A width that is not
|
|
550
|
+
* a number answers the base rung, because that is the layout that assumes nothing.
|
|
551
|
+
* @experimental
|
|
552
|
+
*/
|
|
553
|
+
function breakpointOf(width, ladder = BREAKPOINTS) {
|
|
554
|
+
if (!Number.isFinite(width)) return ladder[0] ?? BASE_BREAKPOINT;
|
|
555
|
+
const rungs = sortedLadder(ladder);
|
|
556
|
+
const first = rungs[0];
|
|
557
|
+
if (first === void 0) return BASE_BREAKPOINT;
|
|
558
|
+
let answer = first;
|
|
559
|
+
for (const rung of rungs) {
|
|
560
|
+
if (rung.minWidth > width) break;
|
|
561
|
+
answer = rung;
|
|
562
|
+
}
|
|
563
|
+
return answer;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Which aspects differ between two snapshots, in a fixed order.
|
|
567
|
+
*
|
|
568
|
+
* A subscriber that re-renders everything on every change is the reason this
|
|
569
|
+
* exists: a phone rotating changes orientation and breakpoint at once but leaves
|
|
570
|
+
* the colour scheme alone, and a component that only draws a hover hint wants to
|
|
571
|
+
* hear about "hover" and nothing else. The breakpoint aspect compares names, not
|
|
572
|
+
* widths, so a one-pixel resize inside a rung is not a change.
|
|
573
|
+
* @experimental
|
|
574
|
+
*/
|
|
575
|
+
function mediaChanges(before, after, ladder = BREAKPOINTS) {
|
|
576
|
+
const changes = [];
|
|
577
|
+
if (before.reducedMotion !== after.reducedMotion) changes.push("motion");
|
|
578
|
+
if (before.colorScheme !== after.colorScheme) changes.push("color-scheme");
|
|
579
|
+
if (before.contrast !== after.contrast) changes.push("contrast");
|
|
580
|
+
if (before.pointer !== after.pointer || before.anyPointer !== after.anyPointer) changes.push("pointer");
|
|
581
|
+
if (before.hover !== after.hover || before.anyHover !== after.anyHover) changes.push("hover");
|
|
582
|
+
if (before.orientation !== after.orientation) changes.push("orientation");
|
|
583
|
+
if (breakpointOf(before.width, ladder).name !== breakpointOf(after.width, ladder).name) changes.push("breakpoint");
|
|
584
|
+
return changes;
|
|
585
|
+
}
|
|
586
|
+
// src/foundation/platform/observation.ts
|
|
587
|
+
/**
|
|
588
|
+
* Turn the boxes a component cares about into the watches it needs.
|
|
589
|
+
*
|
|
590
|
+
* A trigger and its content are both watched for resize and intersection: the
|
|
591
|
+
* content's size can exceed the room the trigger has, and either one leaving the
|
|
592
|
+
* screen means the panel has to move or close. A scroll container is watched for
|
|
593
|
+
* resize and scroll and deliberately not for intersection — an observer on the
|
|
594
|
+
* container measures the container's own visibility, which the panel inside it
|
|
595
|
+
* already learns from its own entry, and a container that scrolls its content out
|
|
596
|
+
* of view is exactly the case IntersectionObserver reports as still intersecting.
|
|
597
|
+
* @experimental
|
|
598
|
+
*/
|
|
599
|
+
function observationPlan(requests) {
|
|
600
|
+
const resize = [];
|
|
601
|
+
const scroll = [];
|
|
602
|
+
const intersection = [];
|
|
603
|
+
const seenResize = /* @__PURE__ */ new Set();
|
|
604
|
+
const seenScroll = /* @__PURE__ */ new Set();
|
|
605
|
+
const seenIntersection = /* @__PURE__ */ new Set();
|
|
606
|
+
for (const request of requests) {
|
|
607
|
+
const { key } = request;
|
|
608
|
+
if (!seenResize.has(key)) {
|
|
609
|
+
seenResize.add(key);
|
|
610
|
+
resize.push(key);
|
|
611
|
+
}
|
|
612
|
+
if (request.role === "container") {
|
|
613
|
+
if (!seenScroll.has(key)) {
|
|
614
|
+
seenScroll.add(key);
|
|
615
|
+
scroll.push(key);
|
|
616
|
+
}
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
if (!seenIntersection.has(key)) {
|
|
620
|
+
seenIntersection.add(key);
|
|
621
|
+
intersection.push(key);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return {
|
|
625
|
+
resize,
|
|
626
|
+
scroll,
|
|
627
|
+
intersection,
|
|
628
|
+
box: "border-box"
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
/** The sub-pixel difference below which two sizes are the same size. Half a CSS
|
|
632
|
+
* pixel: a 1.5 device-pixel ratio rounds a border box to a fraction, and a
|
|
633
|
+
* component that wrote the measured width back to its element would otherwise
|
|
634
|
+
* resize itself again every frame. */
|
|
635
|
+
const RESIZE_TOLERANCE = .5;
|
|
636
|
+
/** A size the page can be laid out at, or undefined when the measurement failed. */
|
|
637
|
+
function measured(size) {
|
|
638
|
+
if (!Number.isFinite(size.width) || !Number.isFinite(size.height)) return;
|
|
639
|
+
if (size.width < 0 || size.height < 0) return;
|
|
640
|
+
return {
|
|
641
|
+
width: size.width,
|
|
642
|
+
height: size.height
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
/** A tolerance that can compare: a non-negative finite number of pixels. */
|
|
646
|
+
function toleranceOf(tolerance) {
|
|
647
|
+
return typeof tolerance === "number" && Number.isFinite(tolerance) && tolerance >= 0 ? tolerance : RESIZE_TOLERANCE;
|
|
648
|
+
}
|
|
649
|
+
function differs(before, after, tolerance) {
|
|
650
|
+
if (before === void 0) return false;
|
|
651
|
+
return Math.abs(before.width - after.width) >= tolerance || Math.abs(before.height - after.height) >= tolerance;
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Read one delivery of measurements against the sizes already known.
|
|
655
|
+
*
|
|
656
|
+
* Four decisions, each a bug a component would otherwise ship:
|
|
657
|
+
*
|
|
658
|
+
* - The first delivery is a baseline. An observer reports what it sees the moment
|
|
659
|
+
* it starts observing; without this, every popover resizes on open and every
|
|
660
|
+
* panel plays an entrance animation nobody asked for.
|
|
661
|
+
* - A difference below the tolerance is not a resize. Fractional device pixels
|
|
662
|
+
* make a border box wobble by a third of a pixel between frames, and a
|
|
663
|
+
* component that acts on that wobbles with it.
|
|
664
|
+
* - A failed measurement never overwrites a good one. A NaN or negative size with
|
|
665
|
+
* no previous value leaves the key absent — absence means "not measured yet",
|
|
666
|
+
* which a component already handles, while a 0×0 anchor is silently placed wrong.
|
|
667
|
+
* - A key that is not in the plan is ignored rather than reported. The observer and
|
|
668
|
+
* the plan disagreeing is the caller's bug, and reporting it as a resize would
|
|
669
|
+
* make the component act on a box it never asked for.
|
|
670
|
+
* @experimental
|
|
671
|
+
*/
|
|
672
|
+
function applyResizeBatch(previous, entries, plan, options = {}) {
|
|
673
|
+
const tolerance = toleranceOf(options.tolerance);
|
|
674
|
+
const watched = new Set(plan.resize);
|
|
675
|
+
const ignored = [];
|
|
676
|
+
const seenIgnored = /* @__PURE__ */ new Set();
|
|
677
|
+
const measuredNow = /* @__PURE__ */ new Map();
|
|
678
|
+
for (const entry of entries) {
|
|
679
|
+
if (!watched.has(entry.key)) {
|
|
680
|
+
if (!seenIgnored.has(entry.key)) {
|
|
681
|
+
seenIgnored.add(entry.key);
|
|
682
|
+
ignored.push(entry.key);
|
|
683
|
+
}
|
|
684
|
+
continue;
|
|
685
|
+
}
|
|
686
|
+
const size = measured(entry.size);
|
|
687
|
+
if (size === void 0) {
|
|
688
|
+
if (!seenIgnored.has(entry.key)) {
|
|
689
|
+
seenIgnored.add(entry.key);
|
|
690
|
+
ignored.push(entry.key);
|
|
691
|
+
}
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
measuredNow.set(entry.key, size);
|
|
695
|
+
}
|
|
696
|
+
const state = new Map(previous ?? []);
|
|
697
|
+
for (const [key, size] of measuredNow) state.set(key, size);
|
|
698
|
+
const resized = [];
|
|
699
|
+
if (previous !== void 0) for (const key of plan.resize) {
|
|
700
|
+
const now = measuredNow.get(key);
|
|
701
|
+
if (now !== void 0 && differs(previous.get(key), now, tolerance)) resized.push(key);
|
|
702
|
+
}
|
|
703
|
+
return {
|
|
704
|
+
state,
|
|
705
|
+
resized,
|
|
706
|
+
baseline: previous === void 0,
|
|
707
|
+
ignored
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
/** The default tolerance in pixels for a scroll edge. One, not zero: fractional
|
|
711
|
+
* device pixels leave `scrollTop + clientHeight` a fraction short of
|
|
712
|
+
* `scrollHeight` at the end, and a "scroll to load more" that never fires is
|
|
713
|
+
* indistinguishable from a broken one. */
|
|
714
|
+
const SCROLL_TOLERANCE = 1;
|
|
715
|
+
function amountOf(value) {
|
|
716
|
+
return Number.isFinite(value) && value > 0 ? value : 0;
|
|
717
|
+
}
|
|
718
|
+
function positionOf(value) {
|
|
719
|
+
return Number.isFinite(value) ? value : 0;
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Read a scroll box's edges.
|
|
723
|
+
*
|
|
724
|
+
* "At the bottom" is true when the content fits, not only when the scroll offset
|
|
725
|
+
* reached the end: a list shorter than its frame is at its bottom with nothing to
|
|
726
|
+
* scroll, and the component that hides its own shadow/border unless the content
|
|
727
|
+
* overflows must say so. A negative `scrollTop` is Safari's rubber band
|
|
728
|
+
* overshooting, which is the top of the list, not a position above it.
|
|
729
|
+
* @experimental
|
|
730
|
+
*/
|
|
731
|
+
function scrollEdges(box, tolerance = 1) {
|
|
732
|
+
const slack = toleranceOf(tolerance);
|
|
733
|
+
const top = positionOf(box.scrollTop);
|
|
734
|
+
const height = amountOf(box.scrollHeight);
|
|
735
|
+
const seenHeight = amountOf(box.clientHeight);
|
|
736
|
+
const left = positionOf(box.scrollLeft);
|
|
737
|
+
const width = amountOf(box.scrollWidth);
|
|
738
|
+
const seenWidth = amountOf(box.clientWidth);
|
|
739
|
+
return {
|
|
740
|
+
atTop: top <= slack,
|
|
741
|
+
atBottom: height - top - seenHeight <= slack,
|
|
742
|
+
atStart: left <= slack,
|
|
743
|
+
atEnd: width - left - seenWidth <= slack,
|
|
744
|
+
scrollsVertically: height - seenHeight > slack,
|
|
745
|
+
scrollsHorizontally: width - seenWidth > slack
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
// src/foundation/platform/visibility.ts
|
|
749
|
+
/**
|
|
750
|
+
* The state the signals describe.
|
|
751
|
+
*
|
|
752
|
+
* Frozen outranks hidden, and both outrank off screen. The order matters because
|
|
753
|
+
* the signals are not exclusive: a frozen page is also often hidden, and a hidden
|
|
754
|
+
* page's elements report `isIntersecting: false` — the one state that explains
|
|
755
|
+
* why nothing can be done is the strongest one, not the last one read.
|
|
756
|
+
* @experimental
|
|
757
|
+
*/
|
|
758
|
+
function visibilityState(signals) {
|
|
759
|
+
if (signals.pageFrozen === true) return "frozen";
|
|
760
|
+
if (signals.documentHidden === true) return "hidden";
|
|
761
|
+
return signals.intersecting === false ? "offscreen" : "visible";
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* The policy for a state.
|
|
765
|
+
*
|
|
766
|
+
* The one option is off-screen polling: a component that must keep a counter
|
|
767
|
+
* moving while it is scrolled past (an unread count, a ticker) asks for it by
|
|
768
|
+
* name, because the default — no work while nobody is looking — is what keeps a
|
|
769
|
+
* long page from running a hundred intervals.
|
|
770
|
+
* @experimental
|
|
771
|
+
*/
|
|
772
|
+
function visibilityPolicy(signals, options = {}) {
|
|
773
|
+
const state = visibilityState(signals);
|
|
774
|
+
const visible = state === "visible";
|
|
775
|
+
const offscreen = state === "offscreen";
|
|
776
|
+
return {
|
|
777
|
+
state,
|
|
778
|
+
animates: visible,
|
|
779
|
+
measures: visible || offscreen,
|
|
780
|
+
polls: visible || offscreen && options.offscreenPolling === true,
|
|
781
|
+
acceptsFocus: visible || offscreen,
|
|
782
|
+
announces: visible || offscreen
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
function ratioOf(value, fallback) {
|
|
786
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 1 ? value : fallback;
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Whether an intersection ratio crossed the line, from the state it was in.
|
|
790
|
+
*
|
|
791
|
+
* The two thresholds exist for one bug: an element sitting exactly on a fold
|
|
792
|
+
* reports ratios around the threshold every frame, and a component that starts
|
|
793
|
+
* work on "entered" and stops on "left" restarts it dozens of times per scroll.
|
|
794
|
+
* A gap between the thresholds makes the state sticky. The gap is normalised —
|
|
795
|
+
* an `exitAt` above `enterAt` would be a band that is both entered and left, so
|
|
796
|
+
* the exit is pulled down to the entry rather than trusted.
|
|
797
|
+
*
|
|
798
|
+
* A ratio that is not a number answers "unchanged": a measurement the page cannot
|
|
799
|
+
* make must not flip a component's state, and a NaN would do exactly that.
|
|
800
|
+
* @experimental
|
|
801
|
+
*/
|
|
802
|
+
function crossedVisibility(visible, ratio, options = {}) {
|
|
803
|
+
if (!Number.isFinite(ratio)) return "unchanged";
|
|
804
|
+
const enterAt = ratioOf(options.enterAt, 0);
|
|
805
|
+
const exitAt = Math.min(ratioOf(options.exitAt, 0), enterAt);
|
|
806
|
+
if (!visible) return ratio > enterAt ? "entered" : "unchanged";
|
|
807
|
+
return ratio <= exitAt ? "left" : "unchanged";
|
|
808
|
+
}
|
|
809
|
+
// src/foundation/platform/pointer.ts
|
|
810
|
+
/**
|
|
811
|
+
* How long a still pointer stays awake before it counts as idle. Three seconds:
|
|
812
|
+
* long enough that reading a panel does not dismiss it, short enough that the
|
|
813
|
+
* chrome is gone before the user's hand reaches the keyboard.
|
|
814
|
+
* @experimental
|
|
815
|
+
*/
|
|
816
|
+
const IDLE_AFTER = 3e3;
|
|
817
|
+
/** The kinds an event may name. Anything else — including the empty string Safari
|
|
818
|
+
* before 13 sends for a mouse — leaves the known kind alone. */
|
|
819
|
+
const KINDS = /* @__PURE__ */ new Set([
|
|
820
|
+
"mouse",
|
|
821
|
+
"touch",
|
|
822
|
+
"pen"
|
|
823
|
+
]);
|
|
824
|
+
/** The event types that are activity. `pointerleave` is deliberately absent: the
|
|
825
|
+
* pointer leaving the window is the moment idling should begin, not activity, and
|
|
826
|
+
* counting it keeps a tooltip open forever after the cursor has gone. */
|
|
827
|
+
const ACTIVITY = /* @__PURE__ */ new Set([
|
|
828
|
+
"pointermove",
|
|
829
|
+
"pointerdown",
|
|
830
|
+
"pointerup",
|
|
831
|
+
"pointercancel",
|
|
832
|
+
"wheel"
|
|
833
|
+
]);
|
|
834
|
+
/** A clock reading the tracker can use. A non-finite one is not a reading: it
|
|
835
|
+
* makes every later subtraction NaN, so it is refused here instead of spreading. */
|
|
836
|
+
function clockOf(now) {
|
|
837
|
+
return Number.isFinite(now) ? now : void 0;
|
|
838
|
+
}
|
|
839
|
+
/** The kind an event names, or the one already known. */
|
|
840
|
+
function kindOf(signal, previous) {
|
|
841
|
+
const named = signal.pointerType;
|
|
842
|
+
if (typeof named === "string" && KINDS.has(named)) return named;
|
|
843
|
+
return previous;
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* The state a tracker starts in.
|
|
847
|
+
*
|
|
848
|
+
* A caller passes the clock it will keep using — `pointerState(performance.now())`
|
|
849
|
+
* — because starting at 0 and comparing against a clock that starts at 500000
|
|
850
|
+
* makes the first idle check see an hour of stillness.
|
|
851
|
+
* @experimental
|
|
852
|
+
*/
|
|
853
|
+
function pointerState(now = 0) {
|
|
854
|
+
return {
|
|
855
|
+
activity: clockOf(now) ?? 0,
|
|
856
|
+
kind: "unknown",
|
|
857
|
+
pressing: false
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* The state after one event.
|
|
862
|
+
*
|
|
863
|
+
* Three rules:
|
|
864
|
+
*
|
|
865
|
+
* - An event type the tracker does not own leaves everything alone. `pointerenter`
|
|
866
|
+
* and `pointerleave` are not activity, and a component that passes every event it
|
|
867
|
+
* receives must not have its hint kept alive by the one that means "gone".
|
|
868
|
+
* - The clock is only written when it can be read. A broken clock still updates the
|
|
869
|
+
* press and the device: losing those makes a drag look abandoned, while losing the
|
|
870
|
+
* timestamp only makes the next idle check answer from the last good reading.
|
|
871
|
+
* - A press keeps the tracker awake through {@link pointerIdle}, not here: this
|
|
872
|
+
* function reports the state, and the decision about idleness belongs to the
|
|
873
|
+
* question.
|
|
874
|
+
* @experimental
|
|
875
|
+
*/
|
|
876
|
+
function pointerActivity(state, signal, now) {
|
|
877
|
+
if (!ACTIVITY.has(signal.type)) return state;
|
|
878
|
+
const kind = kindOf(signal, state.kind);
|
|
879
|
+
const stamp = clockOf(now);
|
|
880
|
+
const pressing = signal.type === "pointerdown" ? true : signal.type === "pointerup" || signal.type === "pointercancel" ? false : state.pressing;
|
|
881
|
+
if (stamp === void 0) return {
|
|
882
|
+
activity: state.activity,
|
|
883
|
+
kind,
|
|
884
|
+
pressing
|
|
885
|
+
};
|
|
886
|
+
return {
|
|
887
|
+
activity: stamp,
|
|
888
|
+
kind,
|
|
889
|
+
pressing
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* The threshold {@link pointerIdle} will use for these options.
|
|
894
|
+
*
|
|
895
|
+
* Exported because an adapter that schedules a timer needs the same number the
|
|
896
|
+
* decision compares against; reading the option again with its own fallback is how
|
|
897
|
+
* a timer ends up waking a millisecond before the decision agrees that the pointer
|
|
898
|
+
* is idle. A threshold that cannot be used — zero, negative, NaN, infinite — is
|
|
899
|
+
* {@link IDLE_AFTER}, not a pointer that is idle on every read.
|
|
900
|
+
* @experimental
|
|
901
|
+
*/
|
|
902
|
+
function idleThreshold(options = {}) {
|
|
903
|
+
return typeof options.idleAfter === "number" && Number.isFinite(options.idleAfter) && options.idleAfter > 0 ? options.idleAfter : IDLE_AFTER;
|
|
904
|
+
}
|
|
905
|
+
/**
|
|
906
|
+
* The idle question, answered from the state and a clock reading.
|
|
907
|
+
*
|
|
908
|
+
* `waited` is clamped at zero: a clock that went backwards (a fake clock in a
|
|
909
|
+
* test, a monotonic clock reset by a sleep) must not produce a negative wait,
|
|
910
|
+
* which would read as "idle for -4 seconds" and, in the caller's comparison, as
|
|
911
|
+
* idle forever. A press suppresses idle while the press lasts — the number a
|
|
912
|
+
* slider shows while the thumb is held is the case, and a value that disappears
|
|
913
|
+
* mid-drag is worse than a hint that stays a moment too long.
|
|
914
|
+
*
|
|
915
|
+
* The threshold is inclusive: "idle after 3000 ms" means 3000 ms is enough. A
|
|
916
|
+
* threshold that cannot be used (zero, negative, NaN, infinite) falls back to
|
|
917
|
+
* {@link IDLE_AFTER} rather than making the pointer idle on every read.
|
|
918
|
+
* @experimental
|
|
919
|
+
*/
|
|
920
|
+
function pointerIdle(state, now, options = {}) {
|
|
921
|
+
const threshold = idleThreshold(options);
|
|
922
|
+
const stamp = clockOf(now);
|
|
923
|
+
if (stamp === void 0) return {
|
|
924
|
+
idle: false,
|
|
925
|
+
waited: 0
|
|
926
|
+
};
|
|
927
|
+
const waited = Math.max(0, stamp - state.activity);
|
|
928
|
+
return {
|
|
929
|
+
idle: !state.pressing && waited >= threshold,
|
|
930
|
+
waited
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
// src/foundation/platform/clipboard.ts
|
|
934
|
+
/** The reasons `DOMException` names map to. Anything not named here — including
|
|
935
|
+
* an error that is not an object, or one with no name — is "unavailable": a
|
|
936
|
+
* failure that identifies nothing must not be reported as a refusal, because
|
|
937
|
+
* "denied" sends the user to a permission setting that may not exist. */
|
|
938
|
+
const REASONS = {
|
|
939
|
+
NotAllowedError: "denied",
|
|
940
|
+
SecurityError: "denied",
|
|
941
|
+
NotSupportedError: "unsupported",
|
|
942
|
+
NotFoundError: "unavailable",
|
|
943
|
+
AbortError: "unavailable",
|
|
944
|
+
DataError: "invalid"
|
|
945
|
+
};
|
|
946
|
+
/**
|
|
947
|
+
* The reason an error means.
|
|
948
|
+
*
|
|
949
|
+
* `undefined` is "unsupported", not "unavailable": a missing API answers by not
|
|
950
|
+
* being there, so an adapter that found nothing to call has no error to classify
|
|
951
|
+
* and passes `undefined`. The rejected alternative is an `instanceof DOMException`
|
|
952
|
+
* check, which is false for the error an iframe's clipboard rejects with — a
|
|
953
|
+
* cross-realm object — and true for a DOMException a page threw for another reason,
|
|
954
|
+
* which is why the name is read structurally.
|
|
955
|
+
* @experimental
|
|
956
|
+
*/
|
|
957
|
+
function clipboardFailure(error) {
|
|
958
|
+
if (error === void 0 || error === null) return "unsupported";
|
|
959
|
+
if (typeof error !== "object") return "unavailable";
|
|
960
|
+
const name = error.name;
|
|
961
|
+
if (typeof name !== "string" || name === "") return "unavailable";
|
|
962
|
+
return REASONS[name] ?? "unavailable";
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* How a write should be attempted.
|
|
966
|
+
*
|
|
967
|
+
* The async API is preferred everywhere it can be used. The synchronous
|
|
968
|
+
* `execCommand` route is not a "todo": it is the correct answer for an insecure
|
|
969
|
+
* context and for an unfocused document, both of which refuse the async API
|
|
970
|
+
* outright, and it costs a visible selection change, which is why it is the
|
|
971
|
+
* fallback and not the default. A read has no fallback at all — see
|
|
972
|
+
* {@link canReadClipboard} — so a caller must not assume the two directions share a
|
|
973
|
+
* strategy.
|
|
974
|
+
* @experimental
|
|
975
|
+
*/
|
|
976
|
+
function clipboardStrategy(environment) {
|
|
977
|
+
if (environment.api && environment.secure && environment.focused) return "clipboard-api";
|
|
978
|
+
return environment.fallback ? "exec-command" : "none";
|
|
979
|
+
}
|
|
980
|
+
/**
|
|
981
|
+
* Whether a read can be attempted at all.
|
|
982
|
+
*
|
|
983
|
+
* A read is refused unless the document is focused and the context is secure, and
|
|
984
|
+
* there is no synchronous route to fall back to — the old `execCommand("paste")`
|
|
985
|
+
* was disabled by every engine years ago. So a component that offers "paste" must
|
|
986
|
+
* hide it when this answers false, instead of showing a button that can only fail.
|
|
987
|
+
* @experimental
|
|
988
|
+
*/
|
|
989
|
+
function canReadClipboard(environment) {
|
|
990
|
+
return environment.api && environment.secure && environment.focused;
|
|
991
|
+
}
|
|
992
|
+
// src/foundation/platform/document-policy.ts
|
|
993
|
+
/** A padding the page can add to: a finite, non-negative length. A computed style
|
|
994
|
+
* that could not be parsed is no padding, which is what the browser used. */
|
|
995
|
+
function paddingOf(value) {
|
|
996
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* The edits that keep the page from jumping when its scrollbar goes away.
|
|
1000
|
+
*
|
|
1001
|
+
* Two refusals are deliberate. A negative or non-finite scrollbar width is a
|
|
1002
|
+
* measurement bug — `innerWidth` read before layout in a hidden tab, a client width
|
|
1003
|
+
* of 0 — and compensating for it would shift the page by a made-up amount, so it
|
|
1004
|
+
* is treated as 0. And a width is not capped: a 60px gutter means the measurement
|
|
1005
|
+
* is wrong, and a silently clamped one is a page that jumps a little instead of a
|
|
1006
|
+
* page that reports a bug.
|
|
1007
|
+
*
|
|
1008
|
+
* The padding is added to the existing one rather than replacing it: a body with
|
|
1009
|
+
* 16px already has 16px of its layout to lose, and `padding-right: 15px` would
|
|
1010
|
+
* shrink the page's text to the scrollbar's width.
|
|
1011
|
+
* @experimental
|
|
1012
|
+
*/
|
|
1013
|
+
function scrollLockEdits(input) {
|
|
1014
|
+
const gutter = Number.isFinite(input.scrollbarWidth) && input.scrollbarWidth > 0 ? input.scrollbarWidth : 0;
|
|
1015
|
+
const side = input.direction === "rtl" ? "left" : "right";
|
|
1016
|
+
const base = side === "left" ? paddingOf(input.paddingLeft) : paddingOf(input.paddingRight);
|
|
1017
|
+
return {
|
|
1018
|
+
gutter,
|
|
1019
|
+
overflow: "hidden",
|
|
1020
|
+
padding: gutter > 0 ? {
|
|
1021
|
+
side,
|
|
1022
|
+
value: base + gutter
|
|
1023
|
+
} : void 0
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* The regions that must become inert while a modal is open.
|
|
1028
|
+
*
|
|
1029
|
+
* A region this lock did not inert is never restored by it: an element that was
|
|
1030
|
+
* already inert belongs to whatever inerted it, and removing the attribute on
|
|
1031
|
+
* release would make a page interactive that another overlay deliberately locked.
|
|
1032
|
+
* Duplicate keys answer once — a host that reports the same region twice (a
|
|
1033
|
+
* portalled node's owner and its parent) must not get two answers for one element.
|
|
1034
|
+
* @experimental
|
|
1035
|
+
*/
|
|
1036
|
+
function inertKeys(regions) {
|
|
1037
|
+
const keys = [];
|
|
1038
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1039
|
+
for (const region of regions) {
|
|
1040
|
+
if (region.role !== "content" || region.alreadyInert === true) continue;
|
|
1041
|
+
if (seen.has(region.key)) continue;
|
|
1042
|
+
seen.add(region.key);
|
|
1043
|
+
keys.push(region.key);
|
|
1044
|
+
}
|
|
1045
|
+
return keys;
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* The decision, in the order the checks matter.
|
|
1049
|
+
*
|
|
1050
|
+
* `move` first, because the focus event caused by the guard's own move arrives
|
|
1051
|
+
* before the call returns and answering it schedules a move from inside a move —
|
|
1052
|
+
* the two-guard bounce. Then disconnection, because a guard whose root left the
|
|
1053
|
+
* document has lost its own focus and must release rather than fight for it. Then
|
|
1054
|
+
* visibility, then ownership, and only then the pull.
|
|
1055
|
+
*
|
|
1056
|
+
* The default for every field is the quiet answer: an unstated `innermost` is
|
|
1057
|
+
* false, so a caller that has not worked out the ownership does not pull.
|
|
1058
|
+
* @experimental
|
|
1059
|
+
*/
|
|
1060
|
+
function focusPull(state) {
|
|
1061
|
+
if (state.moving === true) return "hold";
|
|
1062
|
+
if (state.connected === false) return "decline";
|
|
1063
|
+
if (state.visible === false) return "decline";
|
|
1064
|
+
if (state.innermost !== true) return "decline";
|
|
1065
|
+
if (state.focusInside === true) return "decline";
|
|
1066
|
+
return "pull";
|
|
1067
|
+
}
|
|
1068
|
+
// src/foundation/platform/dom.ts
|
|
1069
|
+
/**
|
|
1070
|
+
* Which platform this window is on, for a shortcut display.
|
|
1071
|
+
*
|
|
1072
|
+
* The fields are read here and classified in `environment.ts`; a caller that
|
|
1073
|
+
* renders a hint for another platform passes `platformOf` a probe of its own
|
|
1074
|
+
* instead of calling this.
|
|
1075
|
+
* @experimental
|
|
1076
|
+
*/
|
|
1077
|
+
function readPlatform(view) {
|
|
1078
|
+
const navigator_ = view.navigator;
|
|
1079
|
+
const userAgentDataPlatform = navigator_.userAgentData?.platform;
|
|
1080
|
+
return platformOf({
|
|
1081
|
+
userAgent: navigator_.userAgent,
|
|
1082
|
+
platform: navigator_.platform,
|
|
1083
|
+
...userAgentDataPlatform === void 0 ? {} : { userAgentDataPlatform },
|
|
1084
|
+
maxTouchPoints: navigator_.maxTouchPoints
|
|
1085
|
+
});
|
|
1086
|
+
}
|
|
1087
|
+
/** Whether an environment-provided object exists. The library types promise these
|
|
1088
|
+
* fields because every current browser has them; the engines old enough to be
|
|
1089
|
+
* without one report `undefined` or `null` at runtime, and both mean "no". */
|
|
1090
|
+
function present(value) {
|
|
1091
|
+
return value !== null && value !== void 0;
|
|
1092
|
+
}
|
|
1093
|
+
/** The safe-area probe, one per document. A fixed zero-size element that reads the
|
|
1094
|
+
* `env()` insets through its padding: `env()` cannot be read from script, and a
|
|
1095
|
+
* stylesheet that declared the insets would be a second copy of the numbers. */
|
|
1096
|
+
const probes = /* @__PURE__ */ new WeakMap();
|
|
1097
|
+
const SAFE_AREA_SIDES = [
|
|
1098
|
+
"top",
|
|
1099
|
+
"right",
|
|
1100
|
+
"bottom",
|
|
1101
|
+
"left"
|
|
1102
|
+
];
|
|
1103
|
+
function probeFor(doc) {
|
|
1104
|
+
const existing = probes.get(doc);
|
|
1105
|
+
if (existing !== void 0 && existing.isConnected) return existing;
|
|
1106
|
+
const probe = existing ?? doc.createElement("div");
|
|
1107
|
+
probe.setAttribute("aria-hidden", "true");
|
|
1108
|
+
probe.setAttribute("data-bevelo-safe-area", "");
|
|
1109
|
+
probe.style.cssText = "position:fixed;top:0;left:0;width:0;height:0;visibility:hidden;pointer-events:none;" + SAFE_AREA_SIDES.map((side) => "padding-" + side + ":env(safe-area-inset-" + side + ",0px)").join(";");
|
|
1110
|
+
(doc.body ?? doc.documentElement).append(probe);
|
|
1111
|
+
probes.set(doc, probe);
|
|
1112
|
+
return probe;
|
|
1113
|
+
}
|
|
1114
|
+
/** A computed length, or 0 when the environment reports nothing usable (jsdom
|
|
1115
|
+
* reports an empty string for a property nothing set). */
|
|
1116
|
+
function computedLength(view, element, property) {
|
|
1117
|
+
const value = Number.parseFloat(view.getComputedStyle(element).getPropertyValue(property));
|
|
1118
|
+
return Number.isFinite(value) ? value : 0;
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* The safe-area insets around a notch, a rounded corner or a home indicator.
|
|
1122
|
+
*
|
|
1123
|
+
* All zero unless the page opts into `viewport-fit=cover`, which is why a component
|
|
1124
|
+
* must not add them unconditionally: a page that has not opted in gets 0 here and
|
|
1125
|
+
* no extra padding, which is the browser's own behaviour.
|
|
1126
|
+
*/
|
|
1127
|
+
function readSafeArea(view) {
|
|
1128
|
+
const probe = probeFor(view.document);
|
|
1129
|
+
return {
|
|
1130
|
+
top: computedLength(view, probe, "padding-top"),
|
|
1131
|
+
right: computedLength(view, probe, "padding-right"),
|
|
1132
|
+
bottom: computedLength(view, probe, "padding-bottom"),
|
|
1133
|
+
left: computedLength(view, probe, "padding-left")
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
/**
|
|
1137
|
+
* Read the metrics the page is drawn in, as the pure value describes them.
|
|
1138
|
+
*
|
|
1139
|
+
* A window without a visual viewport reports only the layout one and
|
|
1140
|
+
* {@link viewportMetrics} derives the rest: that is the correct split, because the
|
|
1141
|
+
* browser that lacks the API is the browser that cannot zoom.
|
|
1142
|
+
* @experimental
|
|
1143
|
+
*/
|
|
1144
|
+
function readViewportMetrics(view) {
|
|
1145
|
+
const visual = view.visualViewport;
|
|
1146
|
+
return viewportMetrics({
|
|
1147
|
+
layoutWidth: view.innerWidth,
|
|
1148
|
+
layoutHeight: view.innerHeight,
|
|
1149
|
+
...!present(visual) ? {} : {
|
|
1150
|
+
visualWidth: visual.width,
|
|
1151
|
+
visualHeight: visual.height,
|
|
1152
|
+
offsetX: visual.offsetLeft,
|
|
1153
|
+
offsetY: visual.offsetTop,
|
|
1154
|
+
scale: visual.scale
|
|
1155
|
+
},
|
|
1156
|
+
pixelRatio: view.devicePixelRatio,
|
|
1157
|
+
safeArea: readSafeArea(view)
|
|
1158
|
+
});
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
* Follow the viewport: window resizes, and the visual viewport's own resize and
|
|
1162
|
+
* scroll (the on-screen keyboard and the pinch-zoom are not window resizes).
|
|
1163
|
+
*
|
|
1164
|
+
* No call on subscribe, which is the rule the runtime's own environment follows:
|
|
1165
|
+
* a synchronous notification from a subscribe call is how a React effect ends up
|
|
1166
|
+
* updating state during render. The caller reads {@link readViewportMetrics} once
|
|
1167
|
+
* for the value it renders. A notification is skipped when the new metrics are the
|
|
1168
|
+
* same viewport at the half-pixel tolerance — a scrollbar appearing is a resize
|
|
1169
|
+
* event and not a resize anybody can see.
|
|
1170
|
+
* @experimental
|
|
1171
|
+
*/
|
|
1172
|
+
function bindViewportMetrics(view, onChange) {
|
|
1173
|
+
const visual = view.visualViewport;
|
|
1174
|
+
let last = readViewportMetrics(view);
|
|
1175
|
+
const emit = () => {
|
|
1176
|
+
const next = readViewportMetrics(view);
|
|
1177
|
+
if (sameViewport(last, next)) return;
|
|
1178
|
+
last = next;
|
|
1179
|
+
onChange(next);
|
|
1180
|
+
};
|
|
1181
|
+
view.addEventListener("resize", emit);
|
|
1182
|
+
visual?.addEventListener("resize", emit);
|
|
1183
|
+
visual?.addEventListener("scroll", emit);
|
|
1184
|
+
return () => {
|
|
1185
|
+
view.removeEventListener("resize", emit);
|
|
1186
|
+
visual?.removeEventListener("resize", emit);
|
|
1187
|
+
visual?.removeEventListener("scroll", emit);
|
|
1188
|
+
};
|
|
1189
|
+
}
|
|
1190
|
+
/**
|
|
1191
|
+
* The on-screen keyboard's height on one element, as a CSS custom property.
|
|
1192
|
+
*
|
|
1193
|
+
* `coveredBottom` is the decision — how much of the layout viewport the visual
|
|
1194
|
+
* viewport no longer covers, which is the keyboard and not the URL bar (that
|
|
1195
|
+
* collapse resizes the layout viewport too). This writes it to `property`
|
|
1196
|
+
* (`--keyboard-inset` by default) in CSS pixels on bind and on every metrics
|
|
1197
|
+
* change, so a fixed bottom bar can ride above the keyboard with
|
|
1198
|
+
* `bottom: var(--keyboard-inset)`. The element's own value is restored on
|
|
1199
|
+
* release.
|
|
1200
|
+
*
|
|
1201
|
+
* Same contract as {@link bindViewportMetrics}: the write on bind is a style,
|
|
1202
|
+
* not a state update, so it cannot land during render; a document with no
|
|
1203
|
+
* window gets a no-op.
|
|
1204
|
+
* @experimental
|
|
1205
|
+
*/
|
|
1206
|
+
function bindKeyboardInset(element, options) {
|
|
1207
|
+
const view = element.ownerDocument.defaultView;
|
|
1208
|
+
if (view === null) return () => {};
|
|
1209
|
+
const property = options?.property ?? "--keyboard-inset";
|
|
1210
|
+
const previous = element.style.getPropertyValue(property);
|
|
1211
|
+
const priority = element.style.getPropertyPriority(property);
|
|
1212
|
+
const write = (pixels) => {
|
|
1213
|
+
element.style.setProperty(property, pixels + "px");
|
|
1214
|
+
options?.onInset?.(pixels);
|
|
1215
|
+
};
|
|
1216
|
+
write(coveredBottom(readViewportMetrics(view)));
|
|
1217
|
+
const stop = bindViewportMetrics(view, (metrics) => write(coveredBottom(metrics)));
|
|
1218
|
+
return () => {
|
|
1219
|
+
stop();
|
|
1220
|
+
element.style.setProperty(property, previous, priority);
|
|
1221
|
+
};
|
|
1222
|
+
}
|
|
1223
|
+
/**
|
|
1224
|
+
* Read the media snapshot for a window.
|
|
1225
|
+
*
|
|
1226
|
+
* A window without `matchMedia` answers `undefined` for every query rather than
|
|
1227
|
+
* `false`: a browser that cannot be asked is not a browser that says "no", and the
|
|
1228
|
+
* pure builder's fallbacks are chosen for exactly that case. The width and height
|
|
1229
|
+
* are read here because they are the breakpoint's input and the orientation's
|
|
1230
|
+
* source of truth.
|
|
1231
|
+
* @experimental
|
|
1232
|
+
*/
|
|
1233
|
+
function readMediaSnapshot(view) {
|
|
1234
|
+
const query = view.matchMedia;
|
|
1235
|
+
const matches = typeof query === "function" ? (text) => {
|
|
1236
|
+
try {
|
|
1237
|
+
return query.call(view, text).matches;
|
|
1238
|
+
} catch {
|
|
1239
|
+
return;
|
|
1240
|
+
}
|
|
1241
|
+
} : () => void 0;
|
|
1242
|
+
const navigator_ = view.navigator;
|
|
1243
|
+
return mediaSnapshot({
|
|
1244
|
+
width: view.innerWidth,
|
|
1245
|
+
height: view.innerHeight,
|
|
1246
|
+
touchPoints: navigator_.maxTouchPoints,
|
|
1247
|
+
matches
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Subscribe to the media preferences with one listener set for the window.
|
|
1252
|
+
*
|
|
1253
|
+
* Every query is opened once and every listener fans out to the same callback, so
|
|
1254
|
+
* a component that reads the snapshot does not open a `MediaQueryList` of its own
|
|
1255
|
+
* — the disagreement this module exists to remove. The deprecated `addListener` is
|
|
1256
|
+
* used where `addEventListener` is missing (Safari before 14), detected once per
|
|
1257
|
+
* list so add and remove always go through the same API. A resize is listened for
|
|
1258
|
+
* as well, because the breakpoint changes on width and no query reports that.
|
|
1259
|
+
*
|
|
1260
|
+
* The callback receives the aspects that changed, not the whole snapshot alone: a
|
|
1261
|
+
* phone rotating changes orientation and breakpoint and leaves the colour scheme
|
|
1262
|
+
* alone, and a subscriber that only draws a hover hint wants to hear about "hover"
|
|
1263
|
+
* and nothing else. No notification on subscribe, for the same reason as the
|
|
1264
|
+
* viewport.
|
|
1265
|
+
* @experimental
|
|
1266
|
+
*/
|
|
1267
|
+
function bindMediaQueries(view, onChange) {
|
|
1268
|
+
const query = view.matchMedia;
|
|
1269
|
+
const lists = [];
|
|
1270
|
+
if (typeof query === "function") for (const text of Object.values(MEDIA_QUERIES)) lists.push(query.call(view, text));
|
|
1271
|
+
let last = readMediaSnapshot(view);
|
|
1272
|
+
const emit = () => {
|
|
1273
|
+
const next = readMediaSnapshot(view);
|
|
1274
|
+
const changes = mediaChanges(last, next);
|
|
1275
|
+
if (changes.length === 0) return;
|
|
1276
|
+
last = next;
|
|
1277
|
+
onChange(next, changes);
|
|
1278
|
+
};
|
|
1279
|
+
const bindings = [];
|
|
1280
|
+
for (const list of lists) if (typeof list.addEventListener === "function") {
|
|
1281
|
+
list.addEventListener("change", emit);
|
|
1282
|
+
bindings.push({
|
|
1283
|
+
list,
|
|
1284
|
+
addListener: false
|
|
1285
|
+
});
|
|
1286
|
+
} else if (typeof list.addListener === "function") {
|
|
1287
|
+
list.addListener(emit);
|
|
1288
|
+
bindings.push({
|
|
1289
|
+
list,
|
|
1290
|
+
addListener: true
|
|
1291
|
+
});
|
|
1292
|
+
}
|
|
1293
|
+
view.addEventListener("resize", emit);
|
|
1294
|
+
return () => {
|
|
1295
|
+
for (const { list, addListener } of bindings) if (addListener) list.removeListener(emit);
|
|
1296
|
+
else list.removeEventListener("change", emit);
|
|
1297
|
+
view.removeEventListener("resize", emit);
|
|
1298
|
+
};
|
|
1299
|
+
}
|
|
1300
|
+
/** The size of a resize entry, preferring the observed border box and falling back
|
|
1301
|
+
* to the rect for an environment that does not report box sizes. */
|
|
1302
|
+
function sizeOfEntry(entry) {
|
|
1303
|
+
const sizes = entry.borderBoxSize;
|
|
1304
|
+
const first = Array.isArray(sizes) ? sizes[0] : sizes;
|
|
1305
|
+
if (first !== void 0 && typeof first.inlineSize === "number" && typeof first.blockSize === "number") return {
|
|
1306
|
+
width: first.inlineSize,
|
|
1307
|
+
height: first.blockSize
|
|
1308
|
+
};
|
|
1309
|
+
const rect = entry.target.getBoundingClientRect();
|
|
1310
|
+
return {
|
|
1311
|
+
width: rect.width,
|
|
1312
|
+
height: rect.height
|
|
1313
|
+
};
|
|
1314
|
+
}
|
|
1315
|
+
/**
|
|
1316
|
+
* Start watching a plan: one resize observer, one intersection observer and one
|
|
1317
|
+
* capture-phase scroll listener for the whole plan.
|
|
1318
|
+
*
|
|
1319
|
+
* One observer per kind rather than one per element, because the browser delivers a
|
|
1320
|
+
* batch to a single callback: a page that observes twelve boxes gets one callback
|
|
1321
|
+
* per frame instead of twelve, and the pure layer turns that batch into one answer.
|
|
1322
|
+
* The scroll listener is on the document and in the capture phase because a scroll
|
|
1323
|
+
* does not bubble, and a capture listener on the document is the only way to hear a
|
|
1324
|
+
* container's scroll without adding one listener per container.
|
|
1325
|
+
*
|
|
1326
|
+
* `resolve` maps a plan key to the element it names. A key that resolves to nothing
|
|
1327
|
+
* is skipped rather than throwing: a box that has not rendered yet is the ordinary
|
|
1328
|
+
* first frame, and the caller binds again when it does. An environment without the
|
|
1329
|
+
* observer constructors keeps the initial measurement and gets no updates — the
|
|
1330
|
+
* adapter does not fake a resize loop.
|
|
1331
|
+
* @experimental
|
|
1332
|
+
*/
|
|
1333
|
+
function bindObservation(view, plan, resolve, onChange, options = {}) {
|
|
1334
|
+
const doc = view.document;
|
|
1335
|
+
const byElement = /* @__PURE__ */ new Map();
|
|
1336
|
+
const intersectionKeys = /* @__PURE__ */ new Map();
|
|
1337
|
+
const scrollTargets = /* @__PURE__ */ new Map();
|
|
1338
|
+
const resizeElements = [];
|
|
1339
|
+
const intersectionElements = [];
|
|
1340
|
+
for (const key of plan.resize) {
|
|
1341
|
+
const element = resolve(key);
|
|
1342
|
+
if (element === void 0) continue;
|
|
1343
|
+
byElement.set(element, key);
|
|
1344
|
+
resizeElements.push(element);
|
|
1345
|
+
}
|
|
1346
|
+
for (const key of plan.scroll) {
|
|
1347
|
+
const element = resolve(key);
|
|
1348
|
+
if (element !== void 0) scrollTargets.set(element, key);
|
|
1349
|
+
}
|
|
1350
|
+
for (const key of plan.intersection) {
|
|
1351
|
+
const element = resolve(key);
|
|
1352
|
+
if (element !== void 0) {
|
|
1353
|
+
intersectionKeys.set(element, key);
|
|
1354
|
+
intersectionElements.push(element);
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
const cleanups = [];
|
|
1358
|
+
const resizeCtor = view.ResizeObserver;
|
|
1359
|
+
if (resizeCtor !== void 0 && resizeElements.length > 0) {
|
|
1360
|
+
let state;
|
|
1361
|
+
const observer = new resizeCtor((entries) => {
|
|
1362
|
+
const mapped = entries.map((entry) => ({
|
|
1363
|
+
key: byElement.get(entry.target) ?? "",
|
|
1364
|
+
size: sizeOfEntry(entry)
|
|
1365
|
+
}));
|
|
1366
|
+
const batch = applyResizeBatch(state, mapped.filter((entry) => entry.key !== ""), plan, options.tolerance === void 0 ? {} : { tolerance: options.tolerance });
|
|
1367
|
+
state = batch.state;
|
|
1368
|
+
if (batch.resized.length > 0) onChange({
|
|
1369
|
+
kind: "resize",
|
|
1370
|
+
keys: batch.resized
|
|
1371
|
+
});
|
|
1372
|
+
});
|
|
1373
|
+
for (const element of resizeElements) observer.observe(element, { box: plan.box });
|
|
1374
|
+
cleanups.push(() => observer.disconnect());
|
|
1375
|
+
}
|
|
1376
|
+
const intersectionCtor = view.IntersectionObserver;
|
|
1377
|
+
if (intersectionCtor !== void 0 && intersectionElements.length > 0) {
|
|
1378
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1379
|
+
const observer = new intersectionCtor((entries) => {
|
|
1380
|
+
const entered = [];
|
|
1381
|
+
const left = [];
|
|
1382
|
+
for (const entry of entries) {
|
|
1383
|
+
const key = intersectionKeys.get(entry.target);
|
|
1384
|
+
if (key === void 0) continue;
|
|
1385
|
+
const crossing = crossedVisibility(seen.get(entry.target) ?? false, entry.intersectionRatio);
|
|
1386
|
+
seen.set(entry.target, entry.intersectionRatio > 0);
|
|
1387
|
+
if (crossing === "entered") entered.push(key);
|
|
1388
|
+
else if (crossing === "left") left.push(key);
|
|
1389
|
+
}
|
|
1390
|
+
if (entered.length > 0 || left.length > 0) onChange({
|
|
1391
|
+
kind: "intersection",
|
|
1392
|
+
entered,
|
|
1393
|
+
left
|
|
1394
|
+
});
|
|
1395
|
+
}, { threshold: options.thresholds ?? [0] });
|
|
1396
|
+
for (const element of intersectionElements) observer.observe(element);
|
|
1397
|
+
cleanups.push(() => observer.disconnect());
|
|
1398
|
+
}
|
|
1399
|
+
if (scrollTargets.size > 0) {
|
|
1400
|
+
const onScroll = (event) => {
|
|
1401
|
+
const target = event.target;
|
|
1402
|
+
if (!(target instanceof Element)) return;
|
|
1403
|
+
const key = scrollTargets.get(target);
|
|
1404
|
+
if (key === void 0) return;
|
|
1405
|
+
const element = target;
|
|
1406
|
+
onChange({
|
|
1407
|
+
kind: "scroll",
|
|
1408
|
+
key,
|
|
1409
|
+
edges: scrollEdges({
|
|
1410
|
+
scrollTop: element.scrollTop,
|
|
1411
|
+
scrollHeight: element.scrollHeight,
|
|
1412
|
+
clientHeight: element.clientHeight,
|
|
1413
|
+
scrollLeft: element.scrollLeft,
|
|
1414
|
+
scrollWidth: element.scrollWidth,
|
|
1415
|
+
clientWidth: element.clientWidth
|
|
1416
|
+
})
|
|
1417
|
+
});
|
|
1418
|
+
};
|
|
1419
|
+
doc.addEventListener("scroll", onScroll, true);
|
|
1420
|
+
cleanups.push(() => doc.removeEventListener("scroll", onScroll, true));
|
|
1421
|
+
}
|
|
1422
|
+
return () => {
|
|
1423
|
+
for (const cleanup of cleanups) cleanup();
|
|
1424
|
+
};
|
|
1425
|
+
}
|
|
1426
|
+
/** The events the tracker listens for. `pointerleave` is not one of them: it is the
|
|
1427
|
+
* event that means "the pointer has gone", and counting it as activity would keep
|
|
1428
|
+
* a hint alive forever. */
|
|
1429
|
+
const POINTER_EVENTS = [
|
|
1430
|
+
"pointermove",
|
|
1431
|
+
"pointerdown",
|
|
1432
|
+
"pointerup",
|
|
1433
|
+
"pointercancel",
|
|
1434
|
+
"wheel"
|
|
1435
|
+
];
|
|
1436
|
+
/**
|
|
1437
|
+
* Track the pointer for a window.
|
|
1438
|
+
*
|
|
1439
|
+
* The timer is scheduled from the clock the caller gave, not from the event: the
|
|
1440
|
+
* next idle flip is "threshold minus the time already waited", so a pointer that
|
|
1441
|
+
* moved 2900 ms into a 3000 ms window flips 100 ms later. The answer itself is
|
|
1442
|
+
* always {@link pointerIdle}'s, so the timer is an optimisation and never the
|
|
1443
|
+
* source of truth — an early or late timer changes when the callback runs, never
|
|
1444
|
+
* what it says.
|
|
1445
|
+
* @experimental
|
|
1446
|
+
*/
|
|
1447
|
+
function trackPointer(view, options = {}) {
|
|
1448
|
+
const clock = options.now ?? (() => view.performance.now());
|
|
1449
|
+
const threshold = idleThreshold(options.idleAfter === void 0 ? {} : { idleAfter: options.idleAfter });
|
|
1450
|
+
let state = pointerState(clock());
|
|
1451
|
+
let timer;
|
|
1452
|
+
let idle = false;
|
|
1453
|
+
let kind = state.kind;
|
|
1454
|
+
let pressing = state.pressing;
|
|
1455
|
+
const emit = () => {
|
|
1456
|
+
const now = pointerIdle(state, clock(), { idleAfter: threshold });
|
|
1457
|
+
if (now.idle === idle && state.kind === kind && state.pressing === pressing) return;
|
|
1458
|
+
idle = now.idle;
|
|
1459
|
+
kind = state.kind;
|
|
1460
|
+
pressing = state.pressing;
|
|
1461
|
+
options.onChange?.(state, idle);
|
|
1462
|
+
};
|
|
1463
|
+
const schedule = () => {
|
|
1464
|
+
if (timer !== void 0) {
|
|
1465
|
+
clearTimeout(timer);
|
|
1466
|
+
timer = void 0;
|
|
1467
|
+
}
|
|
1468
|
+
if (state.pressing) return;
|
|
1469
|
+
const waited = pointerIdle(state, clock(), { idleAfter: threshold }).waited;
|
|
1470
|
+
timer = setTimeout(() => {
|
|
1471
|
+
timer = void 0;
|
|
1472
|
+
check();
|
|
1473
|
+
}, Math.max(0, threshold - waited));
|
|
1474
|
+
};
|
|
1475
|
+
const check = () => {
|
|
1476
|
+
emit();
|
|
1477
|
+
if (timer === void 0 && !idle && !state.pressing) schedule();
|
|
1478
|
+
};
|
|
1479
|
+
const onEvent = (event) => {
|
|
1480
|
+
const pointerEvent = event;
|
|
1481
|
+
const signal = {
|
|
1482
|
+
type: event.type,
|
|
1483
|
+
...typeof pointerEvent.pointerType === "string" && pointerEvent.pointerType !== "" ? { pointerType: pointerEvent.pointerType } : {}
|
|
1484
|
+
};
|
|
1485
|
+
state = pointerActivity(state, signal, clock());
|
|
1486
|
+
emit();
|
|
1487
|
+
schedule();
|
|
1488
|
+
};
|
|
1489
|
+
for (const type of POINTER_EVENTS) view.addEventListener(type, onEvent, { passive: true });
|
|
1490
|
+
schedule();
|
|
1491
|
+
return {
|
|
1492
|
+
state: () => state,
|
|
1493
|
+
check,
|
|
1494
|
+
stop() {
|
|
1495
|
+
for (const type of POINTER_EVENTS) view.removeEventListener(type, onEvent);
|
|
1496
|
+
if (timer !== void 0) {
|
|
1497
|
+
clearTimeout(timer);
|
|
1498
|
+
timer = void 0;
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
};
|
|
1502
|
+
}
|
|
1503
|
+
/** The environment flags, read from the window. */
|
|
1504
|
+
function clipboardEnvironment(view) {
|
|
1505
|
+
const navigator_ = view.navigator;
|
|
1506
|
+
const doc = view.document;
|
|
1507
|
+
return {
|
|
1508
|
+
api: typeof navigator_.clipboard?.writeText === "function" || typeof navigator_.clipboard?.readText === "function",
|
|
1509
|
+
secure: view.isSecureContext === true,
|
|
1510
|
+
focused: typeof doc.hasFocus === "function" ? doc.hasFocus() : false,
|
|
1511
|
+
fallback: typeof doc.execCommand === "function" && doc.body !== null
|
|
1512
|
+
};
|
|
1513
|
+
}
|
|
1514
|
+
/**
|
|
1515
|
+
* The synchronous copy route: a temporary textarea, selected, copied, removed.
|
|
1516
|
+
*
|
|
1517
|
+
* It is the only route an insecure context has, and the price is that the page's
|
|
1518
|
+
* selection changes and is not restored — a selection inside a text input cannot be
|
|
1519
|
+
* put back from here. The element is removed and focus returned whether the copy
|
|
1520
|
+
* worked or not, so a failure leaves no textarea behind.
|
|
1521
|
+
*/
|
|
1522
|
+
function copyWithCommand(doc, text) {
|
|
1523
|
+
if (typeof doc.execCommand !== "function" || doc.body === null) return false;
|
|
1524
|
+
const active = doc.activeElement;
|
|
1525
|
+
const area = doc.createElement("textarea");
|
|
1526
|
+
area.value = text;
|
|
1527
|
+
area.setAttribute("readonly", "");
|
|
1528
|
+
area.setAttribute("aria-hidden", "true");
|
|
1529
|
+
area.style.cssText = "position:fixed;top:0;left:-9999px;opacity:0";
|
|
1530
|
+
doc.body.append(area);
|
|
1531
|
+
let copied = false;
|
|
1532
|
+
try {
|
|
1533
|
+
area.select();
|
|
1534
|
+
copied = doc.execCommand("copy");
|
|
1535
|
+
} catch {
|
|
1536
|
+
copied = false;
|
|
1537
|
+
}
|
|
1538
|
+
area.remove();
|
|
1539
|
+
if (active instanceof HTMLElement) active.focus();
|
|
1540
|
+
return copied;
|
|
1541
|
+
}
|
|
1542
|
+
/**
|
|
1543
|
+
* The clipboard port for a window.
|
|
1544
|
+
*
|
|
1545
|
+
* Writes follow {@link clipboardStrategy}: the async API where it can be used, the
|
|
1546
|
+
* synchronous route where it cannot, and `unsupported` where neither exists. A
|
|
1547
|
+
* refused async write falls back to the synchronous route when the environment has
|
|
1548
|
+
* one — being refused for an unfocused document is exactly the case the fallback is
|
|
1549
|
+
* for — and the original reason is kept when the fallback fails too, because that
|
|
1550
|
+
* reason is the one that tells the user what to change.
|
|
1551
|
+
* @experimental
|
|
1552
|
+
*/
|
|
1553
|
+
function createClipboardPort(view) {
|
|
1554
|
+
const writeText = async (text) => {
|
|
1555
|
+
const environment = clipboardEnvironment(view);
|
|
1556
|
+
const strategy = clipboardStrategy(environment);
|
|
1557
|
+
const navigator_ = view.navigator;
|
|
1558
|
+
if (strategy === "clipboard-api") try {
|
|
1559
|
+
await navigator_.clipboard?.writeText(text);
|
|
1560
|
+
return { ok: true };
|
|
1561
|
+
} catch (error) {
|
|
1562
|
+
const reason = clipboardFailure(error);
|
|
1563
|
+
if (environment.fallback && copyWithCommand(view.document, text)) return { ok: true };
|
|
1564
|
+
return {
|
|
1565
|
+
ok: false,
|
|
1566
|
+
reason
|
|
1567
|
+
};
|
|
1568
|
+
}
|
|
1569
|
+
if (strategy === "exec-command" && copyWithCommand(view.document, text)) return { ok: true };
|
|
1570
|
+
return {
|
|
1571
|
+
ok: false,
|
|
1572
|
+
reason: "unsupported"
|
|
1573
|
+
};
|
|
1574
|
+
};
|
|
1575
|
+
const readText = async () => {
|
|
1576
|
+
const environment = clipboardEnvironment(view);
|
|
1577
|
+
const navigator_ = view.navigator;
|
|
1578
|
+
if (!canReadClipboard(environment) || typeof navigator_.clipboard?.readText !== "function") return {
|
|
1579
|
+
ok: false,
|
|
1580
|
+
reason: environment.api && environment.secure ? "denied" : "unsupported"
|
|
1581
|
+
};
|
|
1582
|
+
try {
|
|
1583
|
+
const text = await navigator_.clipboard.readText();
|
|
1584
|
+
return {
|
|
1585
|
+
ok: true,
|
|
1586
|
+
text: typeof text === "string" ? text : ""
|
|
1587
|
+
};
|
|
1588
|
+
} catch (error) {
|
|
1589
|
+
return {
|
|
1590
|
+
ok: false,
|
|
1591
|
+
reason: clipboardFailure(error)
|
|
1592
|
+
};
|
|
1593
|
+
}
|
|
1594
|
+
};
|
|
1595
|
+
return {
|
|
1596
|
+
writeText,
|
|
1597
|
+
readText
|
|
1598
|
+
};
|
|
1599
|
+
}
|
|
1600
|
+
const scrollLocks = /* @__PURE__ */ new WeakMap();
|
|
1601
|
+
/**
|
|
1602
|
+
* Lock the page's scroll without moving it sideways.
|
|
1603
|
+
*
|
|
1604
|
+
* The scrollbar's width is measured from the document's client width; a document
|
|
1605
|
+
* that reports none (a hidden tab, jsdom, a display:none iframe) is treated as an
|
|
1606
|
+
* overlay scrollbar and gets no padding — a page whose client width cannot be read
|
|
1607
|
+
* must not be shifted by a made-up fifteen pixels.
|
|
1608
|
+
*
|
|
1609
|
+
* Counted per document, like the platform's own lock: nested modals share one lock
|
|
1610
|
+
* and only the last release restores the styles.
|
|
1611
|
+
* @experimental
|
|
1612
|
+
*/
|
|
1613
|
+
function lockPageScroll(doc) {
|
|
1614
|
+
const existing = scrollLocks.get(doc);
|
|
1615
|
+
if (existing !== void 0) {
|
|
1616
|
+
existing.count += 1;
|
|
1617
|
+
return releaseScrollLock(doc, existing);
|
|
1618
|
+
}
|
|
1619
|
+
const view = doc.defaultView;
|
|
1620
|
+
const html = doc.documentElement;
|
|
1621
|
+
const body = doc.body;
|
|
1622
|
+
const documentWidth = html.clientWidth;
|
|
1623
|
+
const scroll = view !== null && documentWidth > 0 ? view.innerWidth - documentWidth : 0;
|
|
1624
|
+
const style = view === null || body === null ? void 0 : view.getComputedStyle(body);
|
|
1625
|
+
const edits = scrollLockEdits({
|
|
1626
|
+
scrollbarWidth: scroll,
|
|
1627
|
+
direction: style?.direction === "rtl" ? "rtl" : "ltr",
|
|
1628
|
+
paddingLeft: style === void 0 ? 0 : Number.parseFloat(style.paddingLeft),
|
|
1629
|
+
paddingRight: style === void 0 ? 0 : Number.parseFloat(style.paddingRight)
|
|
1630
|
+
});
|
|
1631
|
+
const previousOverflow = html.style.overflow;
|
|
1632
|
+
const previousPaddingLeft = body?.style.paddingLeft ?? "";
|
|
1633
|
+
const previousPaddingRight = body?.style.paddingRight ?? "";
|
|
1634
|
+
html.style.overflow = edits.overflow;
|
|
1635
|
+
if (body !== null && edits.padding !== void 0) {
|
|
1636
|
+
const value = edits.padding.value + "px";
|
|
1637
|
+
if (edits.padding.side === "left") body.style.paddingLeft = value;
|
|
1638
|
+
else body.style.paddingRight = value;
|
|
1639
|
+
}
|
|
1640
|
+
const state = {
|
|
1641
|
+
count: 1,
|
|
1642
|
+
restore: () => {
|
|
1643
|
+
html.style.overflow = previousOverflow;
|
|
1644
|
+
if (body !== null) {
|
|
1645
|
+
body.style.paddingLeft = previousPaddingLeft;
|
|
1646
|
+
body.style.paddingRight = previousPaddingRight;
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
};
|
|
1650
|
+
scrollLocks.set(doc, state);
|
|
1651
|
+
return releaseScrollLock(doc, state);
|
|
1652
|
+
}
|
|
1653
|
+
function releaseScrollLock(doc, state) {
|
|
1654
|
+
let released = false;
|
|
1655
|
+
return () => {
|
|
1656
|
+
if (released) return;
|
|
1657
|
+
released = true;
|
|
1658
|
+
state.count -= 1;
|
|
1659
|
+
if (state.count > 0) return;
|
|
1660
|
+
scrollLocks.delete(doc);
|
|
1661
|
+
state.restore();
|
|
1662
|
+
};
|
|
1663
|
+
}
|
|
1664
|
+
/** Live regions: an announcement inside an inert subtree is never read. The
|
|
1665
|
+
* kit's own announcer carries `aria-live`, so it is one of them. */
|
|
1666
|
+
const LIVE_REGIONS = "[aria-live]:not([aria-live=\"off\"]),[role=\"status\"],[role=\"alert\"],[role=\"log\"],output";
|
|
1667
|
+
function parentAcrossShadow(node) {
|
|
1668
|
+
const parent = node.parentNode;
|
|
1669
|
+
return typeof ShadowRoot !== "undefined" && parent instanceof ShadowRoot ? parent.host : parent;
|
|
1670
|
+
}
|
|
1671
|
+
/**
|
|
1672
|
+
* Make the page unreachable behind a modal.
|
|
1673
|
+
*
|
|
1674
|
+
* Everything becomes inert except the kept elements, their ancestors and the
|
|
1675
|
+
* page's live regions: along each kept element's path to the body, every
|
|
1676
|
+
* sibling goes inert, across shadow roots. Inerting siblings rather than whole
|
|
1677
|
+
* top-level regions is what keeps a live region inside the app announcing, and
|
|
1678
|
+
* what leaves each sibling's `inert` a fact a page can read on the element
|
|
1679
|
+
* itself. An element that already carries `inert` belongs to whoever set it and
|
|
1680
|
+
* is left alone, so the release removes only what this call added. Nesting is
|
|
1681
|
+
* the caller's: a second call adds nothing and its release would remove the
|
|
1682
|
+
* first call's attributes, so a modal stack calls this once per top layer
|
|
1683
|
+
* rather than once per layer.
|
|
1684
|
+
* @experimental
|
|
1685
|
+
*/
|
|
1686
|
+
function applyInertRegions(doc, options = {}) {
|
|
1687
|
+
const body = doc.body;
|
|
1688
|
+
if (body === null) return () => {};
|
|
1689
|
+
const all = [...options.except ?? [], ...Array.from(body.querySelectorAll(LIVE_REGIONS))];
|
|
1690
|
+
const targets = all.filter((element) => !all.some((other) => other !== element && other.contains(element)));
|
|
1691
|
+
const kept = /* @__PURE__ */ new Set();
|
|
1692
|
+
for (const target of targets) for (let node = target; node !== null; node = parentAcrossShadow(node)) kept.add(node);
|
|
1693
|
+
const applied = [];
|
|
1694
|
+
const paths = targets.length > 0 ? targets : Array.from(body.children).slice(0, 1);
|
|
1695
|
+
for (const target of paths) for (let node = target; node !== null && node !== body; node = parentAcrossShadow(node)) {
|
|
1696
|
+
const parent = node.parentNode;
|
|
1697
|
+
for (const sibling of Array.from(parent?.children ?? [])) {
|
|
1698
|
+
if (kept.has(sibling) || sibling.hasAttribute("inert") || options.keeps?.(sibling) === true) continue;
|
|
1699
|
+
sibling.setAttribute("inert", "");
|
|
1700
|
+
applied.push(sibling);
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
return () => {
|
|
1704
|
+
for (const element of applied) element.removeAttribute("inert");
|
|
1705
|
+
};
|
|
1706
|
+
}
|
|
1707
|
+
// src/foundation/popups/focus-restore.ts
|
|
1708
|
+
/**
|
|
1709
|
+
* Where focus goes when a popup closes.
|
|
1710
|
+
*
|
|
1711
|
+
* `"opener"` means focus it, `"body"` means let focus fall to the document
|
|
1712
|
+
* (blur the layer and let the browser choose), and `"none"` means leave focus
|
|
1713
|
+
* exactly where it is — the popup did not take it, or the user has already moved
|
|
1714
|
+
* it.
|
|
1715
|
+
*/
|
|
1716
|
+
function decideFocusRestore(input) {
|
|
1717
|
+
if (!input.tookFocus) return {
|
|
1718
|
+
to: "none",
|
|
1719
|
+
reason: "popup-held-no-focus"
|
|
1720
|
+
};
|
|
1721
|
+
if (!input.focusInside) return {
|
|
1722
|
+
to: "none",
|
|
1723
|
+
reason: "focus-moved-away"
|
|
1724
|
+
};
|
|
1725
|
+
const opener = input.opener;
|
|
1726
|
+
if (opener === void 0) return {
|
|
1727
|
+
to: "body",
|
|
1728
|
+
reason: "no-opener"
|
|
1729
|
+
};
|
|
1730
|
+
if (!opener.exists) return {
|
|
1731
|
+
to: "body",
|
|
1732
|
+
reason: "opener-gone"
|
|
1733
|
+
};
|
|
1734
|
+
if (!opener.focusable) return {
|
|
1735
|
+
to: "body",
|
|
1736
|
+
reason: "opener-unfocusable"
|
|
1737
|
+
};
|
|
1738
|
+
return { to: "opener" };
|
|
1739
|
+
}
|
|
1740
|
+
// src/focus-order.ts
|
|
1741
|
+
/** The elements the APG treats as tabbable, before filtering. */
|
|
1742
|
+
const FOCUSABLE_SELECTOR = [
|
|
1743
|
+
"a[href]",
|
|
1744
|
+
"area[href]",
|
|
1745
|
+
"button",
|
|
1746
|
+
"input:not([type='hidden'])",
|
|
1747
|
+
"select",
|
|
1748
|
+
"textarea",
|
|
1749
|
+
"iframe",
|
|
1750
|
+
"object",
|
|
1751
|
+
"embed",
|
|
1752
|
+
"summary",
|
|
1753
|
+
"audio[controls]",
|
|
1754
|
+
"video[controls]",
|
|
1755
|
+
"[contenteditable]:not([contenteditable='false'])",
|
|
1756
|
+
"[tabindex]"
|
|
1757
|
+
].join(",");
|
|
1758
|
+
function parentOrShadowHost(element) {
|
|
1759
|
+
const parent = element.parentElement;
|
|
1760
|
+
if (parent) return parent;
|
|
1761
|
+
const root = element.getRootNode();
|
|
1762
|
+
if (typeof ShadowRoot !== "undefined" && root instanceof ShadowRoot) return root.host;
|
|
1763
|
+
return null;
|
|
1764
|
+
}
|
|
1765
|
+
function deepActiveElement(doc) {
|
|
1766
|
+
let current = doc.activeElement;
|
|
1767
|
+
while (current !== null && "shadowRoot" in current && current.shadowRoot?.activeElement) current = current.shadowRoot.activeElement;
|
|
1768
|
+
return current;
|
|
1769
|
+
}
|
|
1770
|
+
/** Containment across shadow boundaries: a node in a shadow tree is inside its host's ancestors. */
|
|
1771
|
+
function composedContains(root, target) {
|
|
1772
|
+
let current = target;
|
|
1773
|
+
while (current !== null) {
|
|
1774
|
+
if (current === root) return true;
|
|
1775
|
+
if (current instanceof Element) current = parentOrShadowHost(current);
|
|
1776
|
+
else {
|
|
1777
|
+
const parent = current.parentNode;
|
|
1778
|
+
if (parent !== null) current = parent;
|
|
1779
|
+
else {
|
|
1780
|
+
const rootNode = current.getRootNode();
|
|
1781
|
+
current = typeof ShadowRoot !== "undefined" && rootNode instanceof ShadowRoot ? rootNode.host : null;
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
return false;
|
|
1786
|
+
}
|
|
1787
|
+
function isDisabledForFocus(element) {
|
|
1788
|
+
if (element.getAttribute("aria-disabled") === "true") return true;
|
|
1789
|
+
try {
|
|
1790
|
+
return element.matches(":disabled");
|
|
1791
|
+
} catch {
|
|
1792
|
+
return false;
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
function isNodeRendered(node, view) {
|
|
1796
|
+
if (node.hasAttribute("hidden") || node.hasAttribute("inert") || node.getAttribute("aria-hidden") === "true") return false;
|
|
1797
|
+
const style = view?.getComputedStyle(node);
|
|
1798
|
+
if (style !== void 0) {
|
|
1799
|
+
if (style.display === "none") return false;
|
|
1800
|
+
if (style.visibility === "hidden" || style.visibility === "collapse") return false;
|
|
1801
|
+
}
|
|
1802
|
+
return true;
|
|
1803
|
+
}
|
|
1804
|
+
function areAncestorsRendered(root, view) {
|
|
1805
|
+
if (!root.isConnected) return false;
|
|
1806
|
+
let node = root;
|
|
1807
|
+
while (node !== null) {
|
|
1808
|
+
if (!isNodeRendered(node, view)) return false;
|
|
1809
|
+
node = parentOrShadowHost(node);
|
|
1810
|
+
}
|
|
1811
|
+
return true;
|
|
1812
|
+
}
|
|
1813
|
+
function isRendered(element) {
|
|
1814
|
+
return areAncestorsRendered(element, element.ownerDocument.defaultView);
|
|
1815
|
+
}
|
|
1816
|
+
/**
|
|
1817
|
+
* The radios Tab treats as one stop with this one: same name, same form, and,
|
|
1818
|
+
* outside a form, the same document or shadow root.
|
|
1819
|
+
*/
|
|
1820
|
+
function radioGroup(element) {
|
|
1821
|
+
if (!(element instanceof HTMLInputElement) || element.type !== "radio" || element.name === "") return [];
|
|
1822
|
+
return (element.form ? Array.from(element.form.elements) : Array.from(element.getRootNode().querySelectorAll("input[type='radio']"))).filter((radio) => radio instanceof HTMLInputElement && radio.type === "radio" && radio.name === element.name && radio.form === element.form);
|
|
1823
|
+
}
|
|
1824
|
+
function takesTab(element) {
|
|
1825
|
+
return element.tabIndex >= 0 && !isDisabledForFocus(element) && isRendered(element);
|
|
1826
|
+
}
|
|
1827
|
+
function isTabbableSelf(element) {
|
|
1828
|
+
if (!(element instanceof HTMLElement) || !element.matches(FOCUSABLE_SELECTOR) || element.tabIndex < 0 || isDisabledForFocus(element)) return false;
|
|
1829
|
+
if (element instanceof HTMLInputElement && element.type === "radio" && element.name !== "" && !element.checked) return !radioGroup(element).some((radio) => radio.checked && takesTab(radio));
|
|
1830
|
+
return true;
|
|
1831
|
+
}
|
|
1832
|
+
/**
|
|
1833
|
+
* Whether Tab stops on the element. A radio group is one stop: its checked
|
|
1834
|
+
* radio, or every radio while the checked one cannot take focus (it is behind
|
|
1835
|
+
* a modal, say), which is what the browser does.
|
|
1836
|
+
*/
|
|
1837
|
+
function isTabbable(element) {
|
|
1838
|
+
return isTabbableSelf(element) && isRendered(element);
|
|
1839
|
+
}
|
|
1840
|
+
function childrenOf(container) {
|
|
1841
|
+
const raw = container.children;
|
|
1842
|
+
if (raw !== void 0 && raw !== null && typeof raw[Symbol.iterator] === "function") return Array.from(raw);
|
|
1843
|
+
return Array.from(container.childNodes).filter((node) => node.nodeType === 1);
|
|
1844
|
+
}
|
|
1845
|
+
/** A shadow tree under a host with a negative tabindex is out of Tab order. */
|
|
1846
|
+
function hostSkipsShadow(element) {
|
|
1847
|
+
return element.shadowRoot !== null && element.hasAttribute("tabindex") && element.tabIndex < 0;
|
|
1848
|
+
}
|
|
1849
|
+
function collectInDocumentOrder(container, view, out) {
|
|
1850
|
+
const children = childrenOf(container);
|
|
1851
|
+
for (let i = 0; i < children.length; i += 1) {
|
|
1852
|
+
const child = children[i];
|
|
1853
|
+
if (!isNodeRendered(child, view)) continue;
|
|
1854
|
+
if (isTabbableSelf(child)) out.push(child);
|
|
1855
|
+
if (child.shadowRoot && !hostSkipsShadow(child)) collectInDocumentOrder(child.shadowRoot, view, out);
|
|
1856
|
+
collectInDocumentOrder(child, view, out);
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
/**
|
|
1860
|
+
* The tabbable elements inside a root, in document order, shadow trees included.
|
|
1861
|
+
*
|
|
1862
|
+
* Tabbable, not merely focusable: tabindex -1 is skipped (it is focusable
|
|
1863
|
+
* programmatically, which is what an overlay root and its items use), hidden,
|
|
1864
|
+
* inert, aria-hidden and disabled elements are skipped, a radio group is one
|
|
1865
|
+
* stop, and a native control's browser-imposed tabindex is respected.
|
|
1866
|
+
*/
|
|
1867
|
+
function focusables(root) {
|
|
1868
|
+
const view = root.ownerDocument.defaultView;
|
|
1869
|
+
if (!areAncestorsRendered(root, view)) return [];
|
|
1870
|
+
const found = [];
|
|
1871
|
+
if (root.shadowRoot && !hostSkipsShadow(root)) collectInDocumentOrder(root.shadowRoot, view, found);
|
|
1872
|
+
collectInDocumentOrder(root, view, found);
|
|
1873
|
+
return found;
|
|
1874
|
+
}
|
|
1875
|
+
function orderScope(scope) {
|
|
1876
|
+
const positive = [];
|
|
1877
|
+
const out = [];
|
|
1878
|
+
for (let i = 0; i < scope.length; i += 1) {
|
|
1879
|
+
const entry = scope[i];
|
|
1880
|
+
if (entry.index > 0) positive.push(entry);
|
|
1881
|
+
}
|
|
1882
|
+
if (positive.length > 1) positive.sort((a, b) => a.index - b.index);
|
|
1883
|
+
for (let i = 0; i < positive.length; i += 1) {
|
|
1884
|
+
const stops = positive[i].stops;
|
|
1885
|
+
for (let j = 0; j < stops.length; j += 1) out.push(stops[j]);
|
|
1886
|
+
}
|
|
1887
|
+
for (let i = 0; i < scope.length; i += 1) {
|
|
1888
|
+
const entry = scope[i];
|
|
1889
|
+
if (entry.index <= 0) {
|
|
1890
|
+
const stops = entry.stops;
|
|
1891
|
+
for (let j = 0; j < stops.length; j += 1) out.push(stops[j]);
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
return out;
|
|
1895
|
+
}
|
|
1896
|
+
function walkTabScope(nodes, view, scope) {
|
|
1897
|
+
for (const element of nodes) {
|
|
1898
|
+
if (!isNodeRendered(element, view)) continue;
|
|
1899
|
+
const shadow = element.shadowRoot;
|
|
1900
|
+
const hasTabIndex = element.hasAttribute("tabindex");
|
|
1901
|
+
const index = hasTabIndex ? element.tabIndex : 0;
|
|
1902
|
+
if (shadow !== null && hasTabIndex && index < 0) continue;
|
|
1903
|
+
const selfStop = !shadow?.delegatesFocus && isTabbableSelf(element) ? element : void 0;
|
|
1904
|
+
const slotted = typeof HTMLSlotElement !== "undefined" && element instanceof HTMLSlotElement ? element.assignedElements() : [];
|
|
1905
|
+
if (shadow || slotted.length > 0) {
|
|
1906
|
+
const inner = [];
|
|
1907
|
+
walkTabScope(shadow ? childrenOf(shadow) : slotted, view, inner);
|
|
1908
|
+
const stops = selfStop !== void 0 ? [selfStop, ...orderScope(inner)] : orderScope(inner);
|
|
1909
|
+
if (stops.length > 0) scope.push({
|
|
1910
|
+
index,
|
|
1911
|
+
stops
|
|
1912
|
+
});
|
|
1913
|
+
} else {
|
|
1914
|
+
if (selfStop !== void 0) scope.push({
|
|
1915
|
+
index,
|
|
1916
|
+
stops: [selfStop]
|
|
1917
|
+
});
|
|
1918
|
+
walkTabScope(childrenOf(element), view, scope);
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
/** The tabbable elements inside a root in the order Tab visits them. */
|
|
1923
|
+
function tabOrder(root) {
|
|
1924
|
+
const view = root.ownerDocument.defaultView;
|
|
1925
|
+
if (!areAncestorsRendered(root, view)) return [];
|
|
1926
|
+
const top = [];
|
|
1927
|
+
walkTabScope(root.shadowRoot && !hostSkipsShadow(root) ? childrenOf(root.shadowRoot) : childrenOf(root), view, top);
|
|
1928
|
+
return orderScope(top);
|
|
1929
|
+
}
|
|
1930
|
+
/**
|
|
1931
|
+
* The stops in the root's own tree that Tab visits before the rest: positive
|
|
1932
|
+
* tabindex values, lowest first. Ones inside a shadow root or slotted into a
|
|
1933
|
+
* custom element order only within it, so they keep their host's place.
|
|
1934
|
+
*/
|
|
1935
|
+
function positiveTabStops(stops, root) {
|
|
1936
|
+
const tree = root.getRootNode();
|
|
1937
|
+
const slotted = (element) => {
|
|
1938
|
+
for (let node = element; node && node !== root; node = node.parentElement) if (node.assignedSlot) return true;
|
|
1939
|
+
return false;
|
|
1940
|
+
};
|
|
1941
|
+
return stops.filter((element) => element.tabIndex > 0 && element.getRootNode() === tree && !slotted(element)).sort((a, b) => a.tabIndex - b.tabIndex);
|
|
1942
|
+
}
|
|
1943
|
+
/** Whether `candidate` comes after `node` in document order and is not inside it. */
|
|
1944
|
+
function follows(node, candidate) {
|
|
1945
|
+
return (node.compareDocumentPosition(candidate) & Node.DOCUMENT_POSITION_FOLLOWING) !== 0 && !node.contains(candidate);
|
|
1946
|
+
}
|
|
1947
|
+
/**
|
|
1948
|
+
* The first tabbable element after `node` in document order, skipping anything
|
|
1949
|
+
* inside `skip`, like the panel that is handing focus on.
|
|
1950
|
+
*
|
|
1951
|
+
* Document order, not Tab order: a positive tabindex further down the page is
|
|
1952
|
+
* not "the element after" anything, even though the browser would visit it first.
|
|
1953
|
+
*/
|
|
1954
|
+
function nextTabbable(node, skip) {
|
|
1955
|
+
const body = (node.ownerDocument ?? document).body;
|
|
1956
|
+
if (body === null) return;
|
|
1957
|
+
return focusables(body).find((element) => !element.contains(node) && !(skip?.contains(element) ?? false) && follows(node, element));
|
|
1958
|
+
}
|
|
1959
|
+
// src/platform.ts
|
|
1960
|
+
const ESCAPE_KEYS = /* @__PURE__ */ new Set(["Escape", "Esc"]);
|
|
1961
|
+
/** The root the overlays live in, and the kit's live regions. */
|
|
1962
|
+
const PORTAL_ROOT_ATTRIBUTE = "data-ui-layer-root";
|
|
1963
|
+
const ANNOUNCER_ATTRIBUTE = "data-bevelo-announcer";
|
|
1964
|
+
/** Hidden from sight, not from assistive technology: the opposite of what a
|
|
1965
|
+
* screen reader needs, which is why it is clip, never display:none. */
|
|
1966
|
+
const ANNOUNCER_HIDDEN = "position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap;border:0";
|
|
1967
|
+
const createdRoots = /* @__PURE__ */ new WeakMap();
|
|
1968
|
+
const overriddenRoots = /* @__PURE__ */ new WeakMap();
|
|
1969
|
+
/**
|
|
1970
|
+
* The element portalled parts are appended to: one lazily created
|
|
1971
|
+
* `<div data-ui-layer-root>` per document, as the last child of body.
|
|
1972
|
+
*
|
|
1973
|
+
* A shared root rather than a per-overlay parent, because stacking order is
|
|
1974
|
+
* document order in the layer root and a per-overlay parent would need z-index
|
|
1975
|
+
* bookkeeping to reproduce it. The root is re-appended when the host emptied
|
|
1976
|
+
* body: that is a host choice we should follow, not a detached page.
|
|
1977
|
+
*/
|
|
1978
|
+
function layerRoot(doc = document) {
|
|
1979
|
+
const override = overriddenRoots.get(doc);
|
|
1980
|
+
if (override) return override;
|
|
1981
|
+
const existing = createdRoots.get(doc);
|
|
1982
|
+
if (existing?.isConnected) return existing;
|
|
1983
|
+
const root = existing ?? doc.createElement("div");
|
|
1984
|
+
if (!existing) {
|
|
1985
|
+
root.setAttribute(PORTAL_ROOT_ATTRIBUTE, "");
|
|
1986
|
+
createdRoots.set(doc, root);
|
|
1987
|
+
}
|
|
1988
|
+
(doc.body ?? doc.documentElement).append(root);
|
|
1989
|
+
return root;
|
|
1990
|
+
}
|
|
1991
|
+
/**
|
|
1992
|
+
* The layer root when one already exists, without creating it.
|
|
1993
|
+
*
|
|
1994
|
+
* Read paths — part resolution, dismissal scans — must not materialize the
|
|
1995
|
+
* portal container just to look inside it, so they ask this instead of
|
|
1996
|
+
* `layerRoot`, which appends the element on first call.
|
|
1997
|
+
*/
|
|
1998
|
+
function existingLayerRoot(doc) {
|
|
1999
|
+
const override = overriddenRoots.get(doc);
|
|
2000
|
+
if (override) return override;
|
|
2001
|
+
const existing = createdRoots.get(doc);
|
|
2002
|
+
return existing?.isConnected ? existing : void 0;
|
|
2003
|
+
}
|
|
2004
|
+
/**
|
|
2005
|
+
* Points the portal root at a host-supplied element (a shadow root's container,
|
|
2006
|
+
* a test container). `undefined` restores the default body-level root.
|
|
2007
|
+
*/
|
|
2008
|
+
function setLayerRoot(root) {
|
|
2009
|
+
const doc = root ? root.ownerDocument : document;
|
|
2010
|
+
if (root) overriddenRoots.set(doc, root);
|
|
2011
|
+
else overriddenRoots.delete(doc);
|
|
2012
|
+
}
|
|
2013
|
+
const portalOwners = /* @__PURE__ */ new WeakMap();
|
|
2014
|
+
/**
|
|
2015
|
+
* Records the natural parent of a portalled node. The renderer calls this; a
|
|
2016
|
+
* component never should.
|
|
2017
|
+
*
|
|
2018
|
+
* Once a description is appended to the layer root the DOM cannot answer "which
|
|
2019
|
+
* component owns this", so dismissal keeps the answer here. `null` forgets the
|
|
2020
|
+
* node, which is what removal does.
|
|
2021
|
+
*/
|
|
2022
|
+
function registerPortal(node, owner) {
|
|
2023
|
+
if (owner) portalOwners.set(node, owner);
|
|
2024
|
+
else portalOwners.delete(node);
|
|
2025
|
+
}
|
|
2026
|
+
/**
|
|
2027
|
+
* The element a portalled node was rendered from, if it is one.
|
|
2028
|
+
*
|
|
2029
|
+
* Part resolution and dismissal both need "which component does this node
|
|
2030
|
+
* belong to" for elements that no longer sit in their owner's subtree; the
|
|
2031
|
+
* DOM cannot answer it once the node lives under the layer root, so the
|
|
2032
|
+
* answer lives here.
|
|
2033
|
+
*/
|
|
2034
|
+
function portalOwnerOf(node) {
|
|
2035
|
+
return portalOwners.get(node);
|
|
2036
|
+
}
|
|
2037
|
+
/** The last press in each document, for telling which layer a new one opened from. */
|
|
2038
|
+
const lastPresses = /* @__PURE__ */ new WeakMap();
|
|
2039
|
+
/** How long after a press a layer that opens counts as opened by it. */
|
|
2040
|
+
const PRESS_OPENS_WITHIN = 1e3;
|
|
2041
|
+
const stack = [];
|
|
2042
|
+
let sequence = 0;
|
|
2043
|
+
const documentListeners = /* @__PURE__ */ new WeakMap();
|
|
2044
|
+
function topRecord(doc) {
|
|
2045
|
+
for (let index = stack.length - 1; index >= 0; index -= 1) {
|
|
2046
|
+
const record = stack[index];
|
|
2047
|
+
if (record && !record.released && record.doc === doc) return record;
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
/** The real target of an event. `event.target` is retargeted to the shadow host
|
|
2051
|
+
* for a press inside a shadow tree; the composed path still names the node the
|
|
2052
|
+
* user actually hit. */
|
|
2053
|
+
function composedTarget(event) {
|
|
2054
|
+
if (typeof event.composedPath === "function") {
|
|
2055
|
+
const first = event.composedPath()[0];
|
|
2056
|
+
if (first instanceof Node) return first;
|
|
2057
|
+
}
|
|
2058
|
+
return event.target instanceof Node ? event.target : null;
|
|
2059
|
+
}
|
|
2060
|
+
/** The logical parent: a portalled node's owner, else its parent or shadow host. */
|
|
2061
|
+
function logicalParent(element) {
|
|
2062
|
+
return portalOwners.get(element) ?? parentOrShadowHost(element);
|
|
2063
|
+
}
|
|
2064
|
+
/** Whether `node` sits inside `root`, following portalled nodes back to their owners. */
|
|
2065
|
+
function logicallyContains(root, node) {
|
|
2066
|
+
let current = node instanceof Element ? node : node?.parentElement ?? null;
|
|
2067
|
+
while (current) {
|
|
2068
|
+
if (current === root) return true;
|
|
2069
|
+
current = logicalParent(current);
|
|
2070
|
+
}
|
|
2071
|
+
return false;
|
|
2072
|
+
}
|
|
2073
|
+
/** Whether a node counts as inside a layer: its element, anything portalled
|
|
2074
|
+
* from it, and whatever its dismissal binding names as inside (its trigger). */
|
|
2075
|
+
function insideRecord(record, node, event) {
|
|
2076
|
+
if (logicallyContains(record.element, node)) return true;
|
|
2077
|
+
for (const extra of record.press?.inside() ?? []) if (extra && logicallyContains(extra, node)) return true;
|
|
2078
|
+
for (const other of stack) if (!other.released && other.parent === record && insideRecord(other, node, event)) return true;
|
|
2079
|
+
return event !== void 0 && typeof event.composedPath === "function" && event.composedPath().includes(record.element);
|
|
2080
|
+
}
|
|
2081
|
+
/** The topmost open layer that holds one of the nodes, other than `self`. */
|
|
2082
|
+
function layerHolding(doc, nodes, self) {
|
|
2083
|
+
for (let index = stack.length - 1; index >= 0; index -= 1) {
|
|
2084
|
+
const record = stack[index];
|
|
2085
|
+
if (!record || record === self || record.released || record.doc !== doc) continue;
|
|
2086
|
+
if (nodes.some((node) => node !== null && node !== void 0 && insideRecord(record, node))) return record;
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
/**
|
|
2090
|
+
* The layer a new one opened from: the one holding the press that just opened
|
|
2091
|
+
* it, else the one holding focus, which is where a keyboard opened it from.
|
|
2092
|
+
*/
|
|
2093
|
+
function openerLayer(record) {
|
|
2094
|
+
const doc = record.doc;
|
|
2095
|
+
const press = lastPresses.get(doc);
|
|
2096
|
+
return layerHolding(doc, [press !== void 0 && performance.now() - press.at < PRESS_OPENS_WITHIN ? press.target : null], record) ?? layerHolding(doc, [deepActiveElement(doc)], record);
|
|
2097
|
+
}
|
|
2098
|
+
/** Whether `node` is inside the layer held by `element`, or any layer opened
|
|
2099
|
+
* from it; an element that is not a layer is asked about its subtree. */
|
|
2100
|
+
function layerTreeContains(element, node) {
|
|
2101
|
+
const record = stack.find((entry) => !entry.released && entry.element === element);
|
|
2102
|
+
return record === void 0 ? logicallyContains(element, node) : insideRecord(record, node);
|
|
2103
|
+
}
|
|
2104
|
+
/**
|
|
2105
|
+
* The layers an outside press dismisses, top down: every layer the press is
|
|
2106
|
+
* outside of, until one that contains it or one that shields the layers
|
|
2107
|
+
* beneath it. A press inside a child layer is inside its parent too, because
|
|
2108
|
+
* the child is portalled from it.
|
|
2109
|
+
*/
|
|
2110
|
+
function pressPlan(doc, target, event) {
|
|
2111
|
+
const dismissed = [];
|
|
2112
|
+
if (inPersistentLayer(target)) return dismissed;
|
|
2113
|
+
for (let index = stack.length - 1; index >= 0; index -= 1) {
|
|
2114
|
+
const record = stack[index];
|
|
2115
|
+
if (!record || record.released || record.doc !== doc || record.press === void 0) continue;
|
|
2116
|
+
if (insideRecord(record, target, event)) break;
|
|
2117
|
+
dismissed.push(record);
|
|
2118
|
+
if (record.press.shield) break;
|
|
2119
|
+
}
|
|
2120
|
+
return dismissed;
|
|
2121
|
+
}
|
|
2122
|
+
const CONSUMPTION_RANK = {
|
|
2123
|
+
none: 0,
|
|
2124
|
+
pointerdown: 1,
|
|
2125
|
+
all: 2
|
|
2126
|
+
};
|
|
2127
|
+
/** Delivers an outside press to each planned layer, and answers how much of
|
|
2128
|
+
* the press the layers that accepted it consume. */
|
|
2129
|
+
function dismissPlanned(records, event) {
|
|
2130
|
+
let consume = "none";
|
|
2131
|
+
for (const record of records) {
|
|
2132
|
+
const policy = record.press;
|
|
2133
|
+
if (policy !== void 0 && dismissRecord(record, "outside-press", event) && CONSUMPTION_RANK[policy.consume] > CONSUMPTION_RANK[consume]) consume = policy.consume;
|
|
2134
|
+
}
|
|
2135
|
+
return consume;
|
|
2136
|
+
}
|
|
2137
|
+
function swallow(event) {
|
|
2138
|
+
event.preventDefault();
|
|
2139
|
+
event.stopPropagation();
|
|
2140
|
+
}
|
|
2141
|
+
/**
|
|
2142
|
+
* The document's listeners, shared by every layer in it and installed while
|
|
2143
|
+
* one is open: Escape for whatever no layer's own element heard, and the
|
|
2144
|
+
* outside presses.
|
|
2145
|
+
*
|
|
2146
|
+
* A mouse or pen press dismisses on pointerdown, or, for a layer that asks for
|
|
2147
|
+
* "click", once it has also been released outside, so a text selection dragged
|
|
2148
|
+
* out of a field never dismisses. Only the primary button arms a "click"
|
|
2149
|
+
* dismissal. A touch always waits for its click, so a scroll that starts on the
|
|
2150
|
+
* page is not a press.
|
|
2151
|
+
*/
|
|
2152
|
+
function listenIn(doc) {
|
|
2153
|
+
let swallowClick = false;
|
|
2154
|
+
let touch;
|
|
2155
|
+
let pending;
|
|
2156
|
+
const onKeyDown = (event) => dispatchEscape(doc, event);
|
|
2157
|
+
const onPointerDown = (event) => {
|
|
2158
|
+
lastPresses.set(doc, {
|
|
2159
|
+
target: composedTarget(event),
|
|
2160
|
+
at: performance.now()
|
|
2161
|
+
});
|
|
2162
|
+
swallowClick = false;
|
|
2163
|
+
pending = void 0;
|
|
2164
|
+
touch = void 0;
|
|
2165
|
+
if (event.defaultPrevented) return;
|
|
2166
|
+
const target = composedTarget(event);
|
|
2167
|
+
if (event.pointerType === "touch") {
|
|
2168
|
+
touch = {
|
|
2169
|
+
x: event.clientX,
|
|
2170
|
+
y: event.clientY,
|
|
2171
|
+
target
|
|
2172
|
+
};
|
|
2173
|
+
return;
|
|
2174
|
+
}
|
|
2175
|
+
const dismissed = pressPlan(doc, target, event);
|
|
2176
|
+
const first = dismissed[0];
|
|
2177
|
+
if (first === void 0) return;
|
|
2178
|
+
if (first.press?.outsidePress === "click") {
|
|
2179
|
+
if (event.button !== 0) return;
|
|
2180
|
+
pending = {
|
|
2181
|
+
first,
|
|
2182
|
+
target
|
|
2183
|
+
};
|
|
2184
|
+
if (first.press.consume !== "none") {
|
|
2185
|
+
event.preventDefault();
|
|
2186
|
+
swallowClick = first.press.consume === "all";
|
|
2187
|
+
}
|
|
2188
|
+
return;
|
|
2189
|
+
}
|
|
2190
|
+
const consume = dismissPlanned(dismissed, event);
|
|
2191
|
+
if (consume !== "none") {
|
|
2192
|
+
swallow(event);
|
|
2193
|
+
swallowClick = consume === "all";
|
|
2194
|
+
}
|
|
2195
|
+
};
|
|
2196
|
+
const onPointerUp = (event) => {
|
|
2197
|
+
const press = pending;
|
|
2198
|
+
pending = void 0;
|
|
2199
|
+
if (press === void 0 || press.first.released) return;
|
|
2200
|
+
if (insideRecord(press.first, composedTarget(event), event)) return;
|
|
2201
|
+
dismissPlanned(pressPlan(doc, press.target, event), event);
|
|
2202
|
+
};
|
|
2203
|
+
const onClick = (event) => {
|
|
2204
|
+
if (swallowClick && event.detail > 0) {
|
|
2205
|
+
swallowClick = false;
|
|
2206
|
+
swallow(event);
|
|
2207
|
+
return;
|
|
2208
|
+
}
|
|
2209
|
+
swallowClick = false;
|
|
2210
|
+
const press = touch;
|
|
2211
|
+
touch = void 0;
|
|
2212
|
+
if (press === void 0 || Math.hypot(event.clientX - press.x, event.clientY - press.y) > 10) return;
|
|
2213
|
+
if (dismissPlanned(pressPlan(doc, press.target, event), event) !== "none") swallow(event);
|
|
2214
|
+
};
|
|
2215
|
+
doc.addEventListener("keydown", onKeyDown);
|
|
2216
|
+
doc.addEventListener("pointerdown", onPointerDown, true);
|
|
2217
|
+
doc.addEventListener("pointerup", onPointerUp, true);
|
|
2218
|
+
doc.addEventListener("click", onClick, true);
|
|
2219
|
+
return {
|
|
2220
|
+
count: 0,
|
|
2221
|
+
stop() {
|
|
2222
|
+
doc.removeEventListener("keydown", onKeyDown);
|
|
2223
|
+
doc.removeEventListener("pointerdown", onPointerDown, true);
|
|
2224
|
+
doc.removeEventListener("pointerup", onPointerUp, true);
|
|
2225
|
+
if (!swallowClick) {
|
|
2226
|
+
doc.removeEventListener("click", onClick, true);
|
|
2227
|
+
return;
|
|
2228
|
+
}
|
|
2229
|
+
const off = () => {
|
|
2230
|
+
doc.removeEventListener("click", onClick, true);
|
|
2231
|
+
doc.removeEventListener("click", off, true);
|
|
2232
|
+
doc.removeEventListener("pointerdown", off, true);
|
|
2233
|
+
};
|
|
2234
|
+
doc.addEventListener("click", off, true);
|
|
2235
|
+
doc.addEventListener("pointerdown", off, true);
|
|
2236
|
+
}
|
|
2237
|
+
};
|
|
2238
|
+
}
|
|
2239
|
+
function acquireDocumentListeners(doc) {
|
|
2240
|
+
let entry = documentListeners.get(doc);
|
|
2241
|
+
if (entry === void 0) {
|
|
2242
|
+
entry = listenIn(doc);
|
|
2243
|
+
documentListeners.set(doc, entry);
|
|
2244
|
+
}
|
|
2245
|
+
entry.count += 1;
|
|
2246
|
+
}
|
|
2247
|
+
function releaseDocumentListeners(doc) {
|
|
2248
|
+
const entry = documentListeners.get(doc);
|
|
2249
|
+
if (entry === void 0) return;
|
|
2250
|
+
entry.count -= 1;
|
|
2251
|
+
if (entry.count > 0) return;
|
|
2252
|
+
documentListeners.delete(doc);
|
|
2253
|
+
entry.stop();
|
|
2254
|
+
}
|
|
2255
|
+
/**
|
|
2256
|
+
* Escape goes to the top layer of the document. The layer's own element hears
|
|
2257
|
+
* it first, in the bubble phase, so a control inside that handled the key
|
|
2258
|
+
* (preventDefault) keeps it, and an ancestor of the layer hears it already
|
|
2259
|
+
* handled. The document hears what no layer's element saw, like Escape with
|
|
2260
|
+
* focus on the body.
|
|
2261
|
+
*/
|
|
2262
|
+
function dispatchEscape(doc, event) {
|
|
2263
|
+
if (event.defaultPrevented || event.isComposing || event.altKey || event.ctrlKey || event.metaKey) return;
|
|
2264
|
+
if (!ESCAPE_KEYS.has(event.key)) return;
|
|
2265
|
+
const top = topRecord(doc);
|
|
2266
|
+
if (!top || top.takesEscape?.() === false) return;
|
|
2267
|
+
if (dismissRecord(top, "escape-key", event)) event.preventDefault();
|
|
2268
|
+
}
|
|
2269
|
+
function dismissRecord(record, reason, event) {
|
|
2270
|
+
if (record.released) return false;
|
|
2271
|
+
for (const handler of [...record.handlers]) if (handler(reason, event) !== false) return true;
|
|
2272
|
+
const callback = record.options.onDismiss;
|
|
2273
|
+
if (callback) return callback(reason) !== false;
|
|
2274
|
+
return false;
|
|
2275
|
+
}
|
|
2276
|
+
function releaseRecord(record) {
|
|
2277
|
+
if (record.released) return;
|
|
2278
|
+
record.released = true;
|
|
2279
|
+
const index = stack.indexOf(record);
|
|
2280
|
+
if (index !== -1) stack.splice(index, 1);
|
|
2281
|
+
record.handlers.clear();
|
|
2282
|
+
const restores = [...record.restores];
|
|
2283
|
+
record.restores.length = 0;
|
|
2284
|
+
for (let cursor = restores.length - 1; cursor >= 0; cursor -= 1) {
|
|
2285
|
+
const restore = restores[cursor];
|
|
2286
|
+
if (restore) restore();
|
|
2287
|
+
}
|
|
2288
|
+
releaseDocumentListeners(record.doc);
|
|
2289
|
+
}
|
|
2290
|
+
/**
|
|
2291
|
+
* Puts an element on the layer stack, bottom to top.
|
|
2292
|
+
*
|
|
2293
|
+
* This registers the document's listeners (one set per document, shared by
|
|
2294
|
+
* every layer, installed on demand) and the element's own Escape listener and,
|
|
2295
|
+
* for a modal layer, makes the rest of the page inert and locks page scroll
|
|
2296
|
+
* until the layer is released. The alternative — each overlay installing its
|
|
2297
|
+
* own listeners and asking "am I topmost?" — makes every overlay aware of every other.
|
|
2298
|
+
*/
|
|
2299
|
+
function pushRecord(element, options) {
|
|
2300
|
+
const doc = element.ownerDocument;
|
|
2301
|
+
const record = {
|
|
2302
|
+
id: sequence += 1,
|
|
2303
|
+
element,
|
|
2304
|
+
doc,
|
|
2305
|
+
options,
|
|
2306
|
+
handlers: /* @__PURE__ */ new Set(),
|
|
2307
|
+
restores: [],
|
|
2308
|
+
released: false,
|
|
2309
|
+
press: void 0,
|
|
2310
|
+
takesEscape: void 0,
|
|
2311
|
+
parent: void 0
|
|
2312
|
+
};
|
|
2313
|
+
record.parent = openerLayer(record);
|
|
2314
|
+
stack.push(record);
|
|
2315
|
+
refreshInert(doc);
|
|
2316
|
+
acquireDocumentListeners(doc);
|
|
2317
|
+
const onKeyDown = (event) => {
|
|
2318
|
+
if (topRecord(doc) === record) dispatchEscape(doc, event);
|
|
2319
|
+
};
|
|
2320
|
+
element.addEventListener("keydown", onKeyDown);
|
|
2321
|
+
record.restores.push(() => element.removeEventListener("keydown", onKeyDown));
|
|
2322
|
+
if (options.modal === true) {
|
|
2323
|
+
record.restores.push(setInertOuter(true, [element], doc));
|
|
2324
|
+
record.restores.push(lockScroll(doc));
|
|
2325
|
+
}
|
|
2326
|
+
return record;
|
|
2327
|
+
}
|
|
2328
|
+
function pushLayer(element, options = {}) {
|
|
2329
|
+
const record = pushRecord(element, options);
|
|
2330
|
+
return {
|
|
2331
|
+
id: record.id,
|
|
2332
|
+
dismiss(reason) {
|
|
2333
|
+
dismissRecord(record, reason, new Event("dismiss"));
|
|
2334
|
+
},
|
|
2335
|
+
release() {
|
|
2336
|
+
releaseRecord(record);
|
|
2337
|
+
}
|
|
2338
|
+
};
|
|
2339
|
+
}
|
|
2340
|
+
/** The stacked elements, bottom to top (optionally scoped to `doc`). */
|
|
2341
|
+
function layers(doc) {
|
|
2342
|
+
return (doc === void 0 ? stack : stack.filter((record) => record.doc === doc)).map((record) => record.element);
|
|
2343
|
+
}
|
|
2344
|
+
/** Whether this element is the top layer of its document. */
|
|
2345
|
+
function isTopLayer(element) {
|
|
2346
|
+
return topRecord(element.ownerDocument)?.element === element;
|
|
2347
|
+
}
|
|
2348
|
+
/** Whether this runtime supports the native HTML Popover Top Layer (`popover="manual"`). */
|
|
2349
|
+
function supportsTopLayer() {
|
|
2350
|
+
return typeof HTMLElement !== "undefined" && typeof HTMLElement.prototype.showPopover === "function";
|
|
2351
|
+
}
|
|
2352
|
+
const POPOVER_RESET_ID = "bevelo-popover-reset";
|
|
2353
|
+
const POPOVER_RESET_CSS = "@layer bevelo { :where([popover][data-bevelo-top-layer]) { inset: auto; width: auto; height: auto; max-width: none; max-height: none; margin: 0; padding: 0; border: 0; overflow: visible; color: inherit; background: none; } }";
|
|
2354
|
+
function ensurePopoverReset(doc) {
|
|
2355
|
+
if (doc.getElementById(POPOVER_RESET_ID) !== null) return;
|
|
2356
|
+
const container = doc.head ?? doc.documentElement;
|
|
2357
|
+
if (container === null) return;
|
|
2358
|
+
const style = doc.createElement("style");
|
|
2359
|
+
style.id = POPOVER_RESET_ID;
|
|
2360
|
+
style.textContent = POPOVER_RESET_CSS;
|
|
2361
|
+
container.prepend(style);
|
|
2362
|
+
}
|
|
2363
|
+
/**
|
|
2364
|
+
* Promotes a connected overlay element into the browser's Top Layer via
|
|
2365
|
+
* `popover="manual"` + `showPopover()` when supported, returning a cleanup that
|
|
2366
|
+
* hides and restores the element's prior `popover` attribute.
|
|
2367
|
+
*/
|
|
2368
|
+
function promoteTopLayer(element) {
|
|
2369
|
+
if (!supportsTopLayer() || !element.isConnected) return () => {};
|
|
2370
|
+
ensurePopoverReset(element.ownerDocument);
|
|
2371
|
+
const el = element;
|
|
2372
|
+
const hadPopover = element.hasAttribute("popover");
|
|
2373
|
+
const previousPopover = element.getAttribute("popover");
|
|
2374
|
+
element.setAttribute("data-bevelo-top-layer", "");
|
|
2375
|
+
if (previousPopover !== "manual") element.setAttribute("popover", "manual");
|
|
2376
|
+
try {
|
|
2377
|
+
if (!element.matches(":popover-open")) el.showPopover();
|
|
2378
|
+
} catch {}
|
|
2379
|
+
return () => {
|
|
2380
|
+
try {
|
|
2381
|
+
if (element.matches(":popover-open")) el.hidePopover();
|
|
2382
|
+
} catch {}
|
|
2383
|
+
element.removeAttribute("data-bevelo-top-layer");
|
|
2384
|
+
if (!hadPopover) element.removeAttribute("popover");
|
|
2385
|
+
else if (previousPopover !== null && previousPopover !== "manual") element.setAttribute("popover", previousPopover);
|
|
2386
|
+
};
|
|
2387
|
+
}
|
|
2388
|
+
const persistentLayers = /* @__PURE__ */ new Set();
|
|
2389
|
+
const raising = /* @__PURE__ */ new WeakMap();
|
|
2390
|
+
/** Whether a node sits inside a persistent layer. */
|
|
2391
|
+
function inPersistentLayer(node) {
|
|
2392
|
+
if (node === null || node === void 0) return false;
|
|
2393
|
+
for (const element of persistentLayers) if (element.isConnected && composedContains(element, node)) return true;
|
|
2394
|
+
return false;
|
|
2395
|
+
}
|
|
2396
|
+
/** Whether a popover that opened belongs to a layer on the stack. A popover no
|
|
2397
|
+
* layer owns, like an extension's that shows itself again to stay on top, is
|
|
2398
|
+
* left alone, or the two would raise each other forever. */
|
|
2399
|
+
function ownedByLayer(opened) {
|
|
2400
|
+
return stack.some((record) => !record.released && (composedContains(record.element, opened) || composedContains(opened, record.element)));
|
|
2401
|
+
}
|
|
2402
|
+
/** Shows a top-layer element again, which moves it to the top, keeping focus where it was. */
|
|
2403
|
+
function showAgain(element) {
|
|
2404
|
+
if (!element.isConnected || !element.matches(":popover-open")) return;
|
|
2405
|
+
const doc = element.ownerDocument;
|
|
2406
|
+
const focused = doc.activeElement;
|
|
2407
|
+
const el = element;
|
|
2408
|
+
try {
|
|
2409
|
+
el.hidePopover();
|
|
2410
|
+
el.showPopover();
|
|
2411
|
+
} catch {}
|
|
2412
|
+
if (focused instanceof HTMLElement && focused !== doc.activeElement) focused.focus({ preventScroll: true });
|
|
2413
|
+
}
|
|
2414
|
+
function raiseAbovePopups(doc) {
|
|
2415
|
+
let entry = raising.get(doc);
|
|
2416
|
+
if (entry === void 0) {
|
|
2417
|
+
const onToggle = (event) => {
|
|
2418
|
+
const opened = event.target;
|
|
2419
|
+
if (event.newState !== "open" || !(opened instanceof Element)) return;
|
|
2420
|
+
if (inPersistentLayer(opened) || !ownedByLayer(opened)) return;
|
|
2421
|
+
for (const element of persistentLayers) if (element instanceof HTMLElement && element.ownerDocument === doc) showAgain(element);
|
|
2422
|
+
};
|
|
2423
|
+
doc.addEventListener("toggle", onToggle, true);
|
|
2424
|
+
entry = {
|
|
2425
|
+
count: 0,
|
|
2426
|
+
off: () => doc.removeEventListener("toggle", onToggle, true)
|
|
2427
|
+
};
|
|
2428
|
+
raising.set(doc, entry);
|
|
2429
|
+
}
|
|
2430
|
+
const current = entry;
|
|
2431
|
+
current.count += 1;
|
|
2432
|
+
let done = false;
|
|
2433
|
+
return () => {
|
|
2434
|
+
if (done) return;
|
|
2435
|
+
done = true;
|
|
2436
|
+
current.count -= 1;
|
|
2437
|
+
if (current.count === 0) {
|
|
2438
|
+
current.off();
|
|
2439
|
+
raising.delete(doc);
|
|
2440
|
+
}
|
|
2441
|
+
};
|
|
2442
|
+
}
|
|
2443
|
+
/** Marks an element as a persistent layer until the returned cleanup runs. */
|
|
2444
|
+
function bindPersistentLayer(element) {
|
|
2445
|
+
persistentLayers.add(element);
|
|
2446
|
+
const doc = element.ownerDocument;
|
|
2447
|
+
const stopRaising = raiseAbovePopups(doc);
|
|
2448
|
+
refreshInert(doc);
|
|
2449
|
+
let bound = true;
|
|
2450
|
+
return () => {
|
|
2451
|
+
if (!bound) return;
|
|
2452
|
+
bound = false;
|
|
2453
|
+
persistentLayers.delete(element);
|
|
2454
|
+
stopRaising();
|
|
2455
|
+
};
|
|
2456
|
+
}
|
|
2457
|
+
/**
|
|
2458
|
+
* How long a close may be deferred when the environment cannot report its
|
|
2459
|
+
* animations (jsdom) or an animation never finishes. 200 ms is a short close, so a
|
|
2460
|
+
* leaking attribute cannot outlive a frame the user can see by more than that.
|
|
2461
|
+
*/
|
|
2462
|
+
const PRESENCE_TIMEOUT = 200;
|
|
2463
|
+
/**
|
|
2464
|
+
* The reduced-motion answer for a document.
|
|
2465
|
+
*
|
|
2466
|
+
* The answer is the platform foundation's media snapshot, read through its own
|
|
2467
|
+
* adapter: this module does not open a media query itself, because a component
|
|
2468
|
+
* that asks `matchMedia` is a component that can disagree with the one next to
|
|
2469
|
+
* it about the same frame. A document with no window, or a window that cannot be
|
|
2470
|
+
* asked, is not a document that says "no motion" — it is a document that cannot
|
|
2471
|
+
* answer, and the snapshot's fallback (no preference expressed) is what it
|
|
2472
|
+
* reports.
|
|
2473
|
+
*/
|
|
2474
|
+
function prefersReducedMotion(doc = document) {
|
|
2475
|
+
const view = doc.defaultView;
|
|
2476
|
+
return view === null ? false : readMediaSnapshot(view).reducedMotion;
|
|
2477
|
+
}
|
|
2478
|
+
/**
|
|
2479
|
+
* Whether the current device offers a hover-capable pointer.
|
|
2480
|
+
*
|
|
2481
|
+
* This is a DOM fact, not primitive policy: adapters read it here and pass the
|
|
2482
|
+
* answer into overlays as input. A missing or unanswerable window reports false,
|
|
2483
|
+
* which keeps a hover-only affordance closed when nobody can prove hover exists.
|
|
2484
|
+
*/
|
|
2485
|
+
function hoverCapability(view = typeof window === "undefined" ? void 0 : window) {
|
|
2486
|
+
return view !== void 0 && inputProfile(readMediaSnapshot(view)).hovers;
|
|
2487
|
+
}
|
|
2488
|
+
/** Runs `run` one frame later. A missing `requestAnimationFrame` (jsdom
|
|
2489
|
+
* without `pretendToBeVisual`, a worker) falls back to a task. */
|
|
2490
|
+
function nextFrame(doc, run) {
|
|
2491
|
+
const view = doc.defaultView;
|
|
2492
|
+
if (view && typeof view.requestAnimationFrame === "function") {
|
|
2493
|
+
const frame = view.requestAnimationFrame(() => run());
|
|
2494
|
+
return () => view.cancelAnimationFrame(frame);
|
|
2495
|
+
}
|
|
2496
|
+
const timer = setTimeout(run, 0);
|
|
2497
|
+
return () => clearTimeout(timer);
|
|
2498
|
+
}
|
|
2499
|
+
/**
|
|
2500
|
+
* Runs `run` after the next painted frame.
|
|
2501
|
+
*
|
|
2502
|
+
* A `requestAnimationFrame` callback still runs before paint, so a style change
|
|
2503
|
+
* cleared there is never seen — and a transition whose start style was never
|
|
2504
|
+
* painted does not run. The task after the frame is what guarantees one paint:
|
|
2505
|
+
* a frame, then a task.
|
|
2506
|
+
*/
|
|
2507
|
+
function afterPaint(doc, run) {
|
|
2508
|
+
let timer;
|
|
2509
|
+
const cancelFrame = nextFrame(doc, () => {
|
|
2510
|
+
timer = setTimeout(run, 0);
|
|
2511
|
+
});
|
|
2512
|
+
return () => {
|
|
2513
|
+
cancelFrame();
|
|
2514
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
2515
|
+
};
|
|
2516
|
+
}
|
|
2517
|
+
/** Parses a comma-separated time list ("0s, 150ms") into seconds; NaN when the
|
|
2518
|
+
* environment reports no computed style (jsdom). */
|
|
2519
|
+
function times(value) {
|
|
2520
|
+
return value.split(",").map((part) => {
|
|
2521
|
+
const text = part.trim();
|
|
2522
|
+
const number = Number.parseFloat(text);
|
|
2523
|
+
if (!Number.isFinite(number)) return NaN;
|
|
2524
|
+
return text.endsWith("ms") ? number / 1e3 : number;
|
|
2525
|
+
});
|
|
2526
|
+
}
|
|
2527
|
+
/** The longest duration+delay pair in a CSS time list, in milliseconds. */
|
|
2528
|
+
function longestOf(durations, delays) {
|
|
2529
|
+
const values = times(durations);
|
|
2530
|
+
const waits = times(delays);
|
|
2531
|
+
let longest = 0;
|
|
2532
|
+
for (let index = 0; index < values.length; index += 1) {
|
|
2533
|
+
const duration = values[index];
|
|
2534
|
+
const delay = waits[index % Math.max(1, waits.length)];
|
|
2535
|
+
if (duration === void 0 || !Number.isFinite(duration)) continue;
|
|
2536
|
+
longest = Math.max(longest, duration + (delay === void 0 || !Number.isFinite(delay) ? 0 : delay));
|
|
2537
|
+
}
|
|
2538
|
+
return Math.round(longest * 1e3);
|
|
2539
|
+
}
|
|
2540
|
+
/**
|
|
2541
|
+
* The motion the element's computed style declares, in milliseconds, or
|
|
2542
|
+
* `undefined` when the environment cannot say.
|
|
2543
|
+
*
|
|
2544
|
+
* `undefined` is not zero: jsdom reports empty computed styles, and an unknown
|
|
2545
|
+
* duration must still be given a chance to animate, which is why a close there
|
|
2546
|
+
* waits out {@link PRESENCE_TIMEOUT}. A browser always resolves the properties,
|
|
2547
|
+
* so "0" genuinely means "no stylesheet animates this element".
|
|
2548
|
+
*/
|
|
2549
|
+
function declaredMotion(element) {
|
|
2550
|
+
const style = element.ownerDocument.defaultView?.getComputedStyle(element);
|
|
2551
|
+
if (!style || style.transitionDuration === "" && style.animationDuration === "") return;
|
|
2552
|
+
return Math.max(longestOf(style.transitionDuration, style.transitionDelay), longestOf(style.animationDuration, style.animationDelay));
|
|
2553
|
+
}
|
|
2554
|
+
/** How long the timeouts may last: a declared motion raises the floor, never
|
|
2555
|
+
* lowers it, so a 400 ms transition gets 400 ms and a leaked 5 ms one still
|
|
2556
|
+
* cannot hide a layer before it finished. */
|
|
2557
|
+
function waitFor(element, timeout) {
|
|
2558
|
+
const motion = declaredMotion(element);
|
|
2559
|
+
return motion === void 0 ? timeout : Math.max(timeout, motion + 50);
|
|
2560
|
+
}
|
|
2561
|
+
/**
|
|
2562
|
+
* Runs `done` once the element's running animations have finished, after the
|
|
2563
|
+
* exit has been painted.
|
|
2564
|
+
*
|
|
2565
|
+
* The wait is armed after a paint on purpose: read inside the same task as the
|
|
2566
|
+
* attribute change, `getAnimations()` does not see the transition that change
|
|
2567
|
+
* started, and a settle scheduled from it would cut the animation short.
|
|
2568
|
+
*
|
|
2569
|
+
* The fallbacks, in order: reduced motion settles on the next frame; an
|
|
2570
|
+
* environment without `getAnimations` (jsdom) settles on the timeout, so a test
|
|
2571
|
+
* can advance it rather than hang; an element with no animations at all settles
|
|
2572
|
+
* on the next frame rather than waiting out a timer nobody needs. The returned
|
|
2573
|
+
* cleanup cancels all of them, which is what an interrupted exit calls.
|
|
2574
|
+
*/
|
|
2575
|
+
function whenSettled(element, done, options = {}) {
|
|
2576
|
+
const doc = element.ownerDocument;
|
|
2577
|
+
const timeout = Number.isFinite(options.timeout) && (options.timeout ?? -1) >= 0 ? options.timeout : 200;
|
|
2578
|
+
let finished = false;
|
|
2579
|
+
let cancelWait;
|
|
2580
|
+
const finish = () => {
|
|
2581
|
+
if (finished) return;
|
|
2582
|
+
finished = true;
|
|
2583
|
+
cancelWait?.();
|
|
2584
|
+
cancelWait = void 0;
|
|
2585
|
+
done();
|
|
2586
|
+
};
|
|
2587
|
+
const cleanup = () => {
|
|
2588
|
+
finished = true;
|
|
2589
|
+
cancelWait?.();
|
|
2590
|
+
cancelWait = void 0;
|
|
2591
|
+
};
|
|
2592
|
+
if (options.reducedMotion ?? prefersReducedMotion(doc)) {
|
|
2593
|
+
cancelWait = nextFrame(doc, finish);
|
|
2594
|
+
return cleanup;
|
|
2595
|
+
}
|
|
2596
|
+
if (typeof element.getAnimations !== "function") {
|
|
2597
|
+
const timer = setTimeout(finish, timeout);
|
|
2598
|
+
cancelWait = () => clearTimeout(timer);
|
|
2599
|
+
return cleanup;
|
|
2600
|
+
}
|
|
2601
|
+
cancelWait = afterPaint(doc, () => {
|
|
2602
|
+
cancelWait = void 0;
|
|
2603
|
+
if (finished) return;
|
|
2604
|
+
const animations = element.getAnimations({ subtree: true });
|
|
2605
|
+
if (animations.length === 0) {
|
|
2606
|
+
cancelWait = nextFrame(doc, finish);
|
|
2607
|
+
return;
|
|
2608
|
+
}
|
|
2609
|
+
const timer = setTimeout(finish, waitFor(element, timeout));
|
|
2610
|
+
cancelWait = () => clearTimeout(timer);
|
|
2611
|
+
Promise.allSettled(animations.map((animation) => animation.finished)).then(finish, finish);
|
|
2612
|
+
});
|
|
2613
|
+
return cleanup;
|
|
2614
|
+
}
|
|
2615
|
+
/**
|
|
2616
|
+
* Starts an element's entry: `data-starting-style` for one painted frame.
|
|
2617
|
+
*
|
|
2618
|
+
* The attribute has to be *seen* once — it is the style a transition starts
|
|
2619
|
+
* from — so it is cleared after the frame that painted it. Returns a cleanup
|
|
2620
|
+
* that clears it early, for an element removed mid-entry.
|
|
2621
|
+
*/
|
|
2622
|
+
function beginEnter(element) {
|
|
2623
|
+
element.removeAttribute(ENDING_STYLE_ATTRIBUTE);
|
|
2624
|
+
element.removeAttribute("inert");
|
|
2625
|
+
element.setAttribute(STARTING_STYLE_ATTRIBUTE, "");
|
|
2626
|
+
return afterPaint(element.ownerDocument, () => {
|
|
2627
|
+
element.removeAttribute(STARTING_STYLE_ATTRIBUTE);
|
|
2628
|
+
});
|
|
2629
|
+
}
|
|
2630
|
+
/**
|
|
2631
|
+
* Binds a layer element's open/closed state to its entry and exit phases.
|
|
2632
|
+
*
|
|
2633
|
+
* The element is not written to here: the phase the component stores is what its
|
|
2634
|
+
* view renders, so `data-starting-style`, `data-ending-style`, `inert` and the
|
|
2635
|
+
* deferred `hidden` all survive a patch during the exit (a host re-render would
|
|
2636
|
+
* otherwise hide the layer halfway through its own animation).
|
|
2637
|
+
*
|
|
2638
|
+
* A stylesheet that declares no motion for the closed layer gets a one-frame
|
|
2639
|
+
* exit: the motion is read on the frame after the close, since only the closed
|
|
2640
|
+
* element shows what the closed state animates. An environment that cannot say
|
|
2641
|
+
* what its stylesheets declare (jsdom) gets the timeout, so the lifecycle stays
|
|
2642
|
+
* testable.
|
|
2643
|
+
*/
|
|
2644
|
+
function bindLayerPresence(element, options) {
|
|
2645
|
+
const doc = element.ownerDocument;
|
|
2646
|
+
const timeout = options.timeout;
|
|
2647
|
+
let open = options.isOpen();
|
|
2648
|
+
let cancel;
|
|
2649
|
+
const clear = () => {
|
|
2650
|
+
cancel?.();
|
|
2651
|
+
cancel = void 0;
|
|
2652
|
+
};
|
|
2653
|
+
const enter = () => {
|
|
2654
|
+
clear();
|
|
2655
|
+
options.phase("entering");
|
|
2656
|
+
cancel = afterPaint(doc, () => {
|
|
2657
|
+
cancel = void 0;
|
|
2658
|
+
options.phase("idle");
|
|
2659
|
+
});
|
|
2660
|
+
};
|
|
2661
|
+
const exit = () => {
|
|
2662
|
+
clear();
|
|
2663
|
+
const reduced = options.reducedMotion?.() ?? prefersReducedMotion(doc);
|
|
2664
|
+
options.phase("exiting");
|
|
2665
|
+
const settle = () => {
|
|
2666
|
+
cancel = whenSettled(element, () => {
|
|
2667
|
+
cancel = void 0;
|
|
2668
|
+
options.phase("idle");
|
|
2669
|
+
}, {
|
|
2670
|
+
...timeout === void 0 ? {} : { timeout },
|
|
2671
|
+
reducedMotion: reduced
|
|
2672
|
+
});
|
|
2673
|
+
};
|
|
2674
|
+
if (declaredMotion(element) === void 0) {
|
|
2675
|
+
settle();
|
|
2676
|
+
return;
|
|
2677
|
+
}
|
|
2678
|
+
cancel = nextFrame(doc, () => {
|
|
2679
|
+
cancel = void 0;
|
|
2680
|
+
if (declaredMotion(element) === 0) {
|
|
2681
|
+
options.phase("idle");
|
|
2682
|
+
return;
|
|
2683
|
+
}
|
|
2684
|
+
settle();
|
|
2685
|
+
});
|
|
2686
|
+
};
|
|
2687
|
+
return {
|
|
2688
|
+
update() {
|
|
2689
|
+
const next = options.isOpen();
|
|
2690
|
+
if (next === open) return;
|
|
2691
|
+
open = next;
|
|
2692
|
+
if (next) enter();
|
|
2693
|
+
else exit();
|
|
2694
|
+
},
|
|
2695
|
+
stop() {
|
|
2696
|
+
clear();
|
|
2697
|
+
element.removeAttribute(STARTING_STYLE_ATTRIBUTE);
|
|
2698
|
+
element.removeAttribute(ENDING_STYLE_ATTRIBUTE);
|
|
2699
|
+
element.removeAttribute("inert");
|
|
2700
|
+
}
|
|
2701
|
+
};
|
|
2702
|
+
}
|
|
2703
|
+
/**
|
|
2704
|
+
* Listens for focus leaving a layer, for {@link DismissalOptions.onFocusOut}.
|
|
2705
|
+
*
|
|
2706
|
+
* Focus moving to an element outside is a focusin elsewhere. Tab out of the
|
|
2707
|
+
* page lands on the body, which fires no focusin, so a focusout from the layer
|
|
2708
|
+
* with nowhere to go counts too, but only when Tab caused it: a focused control
|
|
2709
|
+
* that goes away or turns disabled also blurs to nothing, and that is not the
|
|
2710
|
+
* user leaving.
|
|
2711
|
+
*/
|
|
2712
|
+
function watchFocusOut(record, options, onFocusOut) {
|
|
2713
|
+
const doc = record.doc;
|
|
2714
|
+
const view = doc.defaultView;
|
|
2715
|
+
let tabbing = false;
|
|
2716
|
+
const offs = [];
|
|
2717
|
+
const listen = (target, type, run, capture = false) => {
|
|
2718
|
+
target.addEventListener(type, run, capture);
|
|
2719
|
+
offs.push(() => target.removeEventListener(type, run, capture));
|
|
2720
|
+
};
|
|
2721
|
+
const leaves = (event) => {
|
|
2722
|
+
if (!record.released) onFocusOut(event);
|
|
2723
|
+
};
|
|
2724
|
+
listen(doc, "keydown", (event) => {
|
|
2725
|
+
tabbing = event.key === "Tab";
|
|
2726
|
+
}, true);
|
|
2727
|
+
listen(doc, "pointerdown", () => {
|
|
2728
|
+
tabbing = false;
|
|
2729
|
+
}, true);
|
|
2730
|
+
listen(doc, "focusin", (event) => {
|
|
2731
|
+
tabbing = false;
|
|
2732
|
+
if (!insideRecord(record, composedTarget(event))) leaves(event);
|
|
2733
|
+
});
|
|
2734
|
+
listen(record.element, "focusout", (event) => {
|
|
2735
|
+
if (!tabbing || event.relatedTarget !== null) return;
|
|
2736
|
+
setTimeout(() => {
|
|
2737
|
+
const now = deepActiveElement(doc);
|
|
2738
|
+
if (now === null || now === doc.body || !insideRecord(record, now)) leaves(event);
|
|
2739
|
+
});
|
|
2740
|
+
});
|
|
2741
|
+
const blur = options.windowBlur ?? "never";
|
|
2742
|
+
if (view !== null && blur !== "never") {
|
|
2743
|
+
const onBlur = (event) => {
|
|
2744
|
+
if (blur === "always") {
|
|
2745
|
+
leaves(event);
|
|
2746
|
+
return;
|
|
2747
|
+
}
|
|
2748
|
+
setTimeout(() => {
|
|
2749
|
+
const active = doc.activeElement;
|
|
2750
|
+
if (active instanceof HTMLIFrameElement && !insideRecord(record, active)) leaves(event);
|
|
2751
|
+
});
|
|
2752
|
+
};
|
|
2753
|
+
view.addEventListener("blur", onBlur);
|
|
2754
|
+
offs.push(() => view.removeEventListener("blur", onBlur));
|
|
2755
|
+
}
|
|
2756
|
+
return () => {
|
|
2757
|
+
for (const off of offs) off();
|
|
2758
|
+
};
|
|
2759
|
+
}
|
|
2760
|
+
/**
|
|
2761
|
+
* Binds Escape and outside-press dismissal to one element.
|
|
2762
|
+
*
|
|
2763
|
+
* The element is the boundary: a press inside it (or inside a node portalled
|
|
2764
|
+
* from it, or an `inside` element) is not an outside press. When the element
|
|
2765
|
+
* is already a layer this adds a dismissal handler to it instead of stacking a
|
|
2766
|
+
* second entry, so an overlay that used `pushLayer` for modal behaviour keeps
|
|
2767
|
+
* one place in the stack.
|
|
2768
|
+
*
|
|
2769
|
+
* Escape is delivered only when this element is the top layer. An outside press
|
|
2770
|
+
* goes to every layer it is outside of, top down, until a layer that contains
|
|
2771
|
+
* it or one that shields the layers beneath, so a press outside a popover and
|
|
2772
|
+
* the menu open from it closes both.
|
|
2773
|
+
*/
|
|
2774
|
+
function bindDismissals(element, onDismiss, options = {}) {
|
|
2775
|
+
const existing = stack.find((record) => !record.released && record.element === element);
|
|
2776
|
+
const record = existing ?? pushRecord(element, {});
|
|
2777
|
+
const handler = (reason, event) => onDismiss(reason, event);
|
|
2778
|
+
record.handlers.add(handler);
|
|
2779
|
+
const press = {
|
|
2780
|
+
outsidePress: options.outsidePress ?? "pointerdown",
|
|
2781
|
+
shield: options.shield ?? record.options.modal === true,
|
|
2782
|
+
consume: options.consume ?? "pointerdown",
|
|
2783
|
+
inside: options.inside ?? (() => [])
|
|
2784
|
+
};
|
|
2785
|
+
record.press = press;
|
|
2786
|
+
const fromTrigger = options.inside === void 0 ? void 0 : layerHolding(record.doc, options.inside(), record);
|
|
2787
|
+
if (fromTrigger !== void 0 && fromTrigger !== record.parent) {
|
|
2788
|
+
record.parent = fromTrigger;
|
|
2789
|
+
refreshInert(record.doc);
|
|
2790
|
+
}
|
|
2791
|
+
const takesEscape = options.takesEscape;
|
|
2792
|
+
record.takesEscape = takesEscape;
|
|
2793
|
+
const stopFocusOut = options.onFocusOut === void 0 ? void 0 : watchFocusOut(record, options, options.onFocusOut);
|
|
2794
|
+
return () => {
|
|
2795
|
+
stopFocusOut?.();
|
|
2796
|
+
record.handlers.delete(handler);
|
|
2797
|
+
if (record.press === press) record.press = void 0;
|
|
2798
|
+
if (record.takesEscape === takesEscape) record.takesEscape = void 0;
|
|
2799
|
+
if (!existing) releaseRecord(record);
|
|
2800
|
+
};
|
|
2801
|
+
}
|
|
2802
|
+
/**
|
|
2803
|
+
* The element the document has focus in, or null.
|
|
2804
|
+
*
|
|
2805
|
+
* Components ask this instead of reading `document.activeElement` themselves:
|
|
2806
|
+
* this module is the one place that knows which document a tree belongs to, and
|
|
2807
|
+
* a component reading the global would be wrong inside a shadow root or an
|
|
2808
|
+
* iframe the platform was pointed at.
|
|
2809
|
+
*/
|
|
2810
|
+
function activeElement(doc = document) {
|
|
2811
|
+
return doc.activeElement;
|
|
2812
|
+
}
|
|
2813
|
+
/**
|
|
2814
|
+
* Whether an element is the focused one, or carries focus itself.
|
|
2815
|
+
*
|
|
2816
|
+
* `matches(":focus")` covers the case `activeElement` cannot: inside a shadow
|
|
2817
|
+
* tree `activeElement` is the shadow host, so the node that actually has focus
|
|
2818
|
+
* is only visible to the selector.
|
|
2819
|
+
*/
|
|
2820
|
+
function isFocused(element) {
|
|
2821
|
+
return element.ownerDocument.activeElement === element || element.matches(":focus");
|
|
2822
|
+
}
|
|
2823
|
+
function canReceiveFocus(element) {
|
|
2824
|
+
if (!(element instanceof HTMLElement)) return false;
|
|
2825
|
+
if (element.hasAttribute("tabindex")) return true;
|
|
2826
|
+
const editable = element.getAttribute("contenteditable");
|
|
2827
|
+
return editable !== null && editable !== "false";
|
|
2828
|
+
}
|
|
2829
|
+
/**
|
|
2830
|
+
* Focuses the first tabbable descendant, else the fallback, else the root when
|
|
2831
|
+
* the root itself opted into focus with a tabindex. Returns what it focused, so
|
|
2832
|
+
* a caller can report "focus could not enter" without reading activeElement.
|
|
2833
|
+
*
|
|
2834
|
+
* The root is a last resort rather than the first choice: a dialog root with
|
|
2835
|
+
* tabindex -1 is where focus goes only when there is nothing inside it.
|
|
2836
|
+
*/
|
|
2837
|
+
function focusFirst(root, options = {}) {
|
|
2838
|
+
const target = focusables(root)[0] ?? options.fallback ?? (canReceiveFocus(root) ? root : void 0);
|
|
2839
|
+
if (!target) return;
|
|
2840
|
+
target.focus();
|
|
2841
|
+
return target;
|
|
2842
|
+
}
|
|
2843
|
+
/**
|
|
2844
|
+
* The active traps, outermost first: the order their layers opened in.
|
|
2845
|
+
*
|
|
2846
|
+
* A trap cannot decide alone whether a focus event is its business. Two modal
|
|
2847
|
+
* layers open at once means two document-level traps and one focus event, and a
|
|
2848
|
+
* trap that pulled focus back on every event it did not recognise would answer
|
|
2849
|
+
* the other trap's move with a move of its own — the two would take turns until
|
|
2850
|
+
* the stack overflowed. So only one trap per document may move focus, and every
|
|
2851
|
+
* other trap watches and declines.
|
|
2852
|
+
*/
|
|
2853
|
+
const traps = [];
|
|
2854
|
+
/** The trap allowed to move focus for a document: the last one still connected,
|
|
2855
|
+
* which is the layer that opened last, so the innermost one wins. */
|
|
2856
|
+
function innermostTrap(doc) {
|
|
2857
|
+
for (let index = traps.length - 1; index >= 0; index -= 1) {
|
|
2858
|
+
const record = traps[index];
|
|
2859
|
+
if (record && record.doc === doc && record.root.isConnected) return record;
|
|
2860
|
+
}
|
|
2861
|
+
}
|
|
2862
|
+
/**
|
|
2863
|
+
* True while the innermost trap is moving focus.
|
|
2864
|
+
*
|
|
2865
|
+
* `focus()` dispatches `focusin` synchronously, before the call returns, so
|
|
2866
|
+
* every trap sees a second event while the first is still being answered.
|
|
2867
|
+
* Reacting to it would schedule a move from inside a move, the other half of the
|
|
2868
|
+
* bounce. The guard spans the whole turn rather than one layer, because the
|
|
2869
|
+
* event that arrives mid-move was caused by us and is not the user's to answer.
|
|
2870
|
+
*/
|
|
2871
|
+
let movingFocus = false;
|
|
2872
|
+
function moveFocusInto(root) {
|
|
2873
|
+
movingFocus = true;
|
|
2874
|
+
try {
|
|
2875
|
+
focusFirst(root);
|
|
2876
|
+
} finally {
|
|
2877
|
+
movingFocus = false;
|
|
2878
|
+
}
|
|
2879
|
+
}
|
|
2880
|
+
const FRAME_GUARD_STYLE = "position:fixed;width:1px;height:1px;opacity:0;pointer-events:none;outline:none";
|
|
2881
|
+
/**
|
|
2882
|
+
* Takes Tab back from an iframe inside a root.
|
|
2883
|
+
*
|
|
2884
|
+
* Tab inside an iframe moves focus in the iframe's document, where a trap on
|
|
2885
|
+
* this one cannot hear it, and out of its ends to the page or the browser.
|
|
2886
|
+
* While an iframe in `root` has focus, a hidden stop on each side of it
|
|
2887
|
+
* catches that Tab and sends focus on to the root's neighbouring stop,
|
|
2888
|
+
* wrapping at the ends.
|
|
2889
|
+
*/
|
|
2890
|
+
function guardFrames(root) {
|
|
2891
|
+
const doc = root.ownerDocument;
|
|
2892
|
+
const view = doc.defaultView;
|
|
2893
|
+
let frame;
|
|
2894
|
+
let guards = [];
|
|
2895
|
+
const remove = () => {
|
|
2896
|
+
for (const guard of guards) guard.remove();
|
|
2897
|
+
guards = [];
|
|
2898
|
+
frame = void 0;
|
|
2899
|
+
};
|
|
2900
|
+
const guard = () => {
|
|
2901
|
+
const element = doc.createElement("span");
|
|
2902
|
+
element.tabIndex = 0;
|
|
2903
|
+
element.setAttribute("aria-hidden", "true");
|
|
2904
|
+
element.setAttribute("style", FRAME_GUARD_STYLE);
|
|
2905
|
+
return element;
|
|
2906
|
+
};
|
|
2907
|
+
const watch = () => {
|
|
2908
|
+
const active = doc.activeElement;
|
|
2909
|
+
if (guards.length > 0 || !(active instanceof HTMLIFrameElement) || !root.contains(active)) return;
|
|
2910
|
+
frame = active;
|
|
2911
|
+
const before = guard();
|
|
2912
|
+
const after = guard();
|
|
2913
|
+
guards = [before, after];
|
|
2914
|
+
active.before(before);
|
|
2915
|
+
active.after(after);
|
|
2916
|
+
};
|
|
2917
|
+
const later = () => {
|
|
2918
|
+
watch();
|
|
2919
|
+
setTimeout(watch);
|
|
2920
|
+
};
|
|
2921
|
+
const onFocusIn = (event) => {
|
|
2922
|
+
const side = guards.indexOf(event.target);
|
|
2923
|
+
const from = frame;
|
|
2924
|
+
remove();
|
|
2925
|
+
if (side < 0 || from === void 0) return;
|
|
2926
|
+
const stops = tabOrder(root);
|
|
2927
|
+
const index = stops.indexOf(from);
|
|
2928
|
+
((side === 0 ? stops.at(index - 1) : stops.at((index + 1) % stops.length)) ?? root).focus({ preventScroll: true });
|
|
2929
|
+
};
|
|
2930
|
+
view?.addEventListener("blur", later);
|
|
2931
|
+
doc.addEventListener("focusout", later);
|
|
2932
|
+
doc.addEventListener("focusin", onFocusIn, true);
|
|
2933
|
+
return () => {
|
|
2934
|
+
view?.removeEventListener("blur", later);
|
|
2935
|
+
doc.removeEventListener("focusout", later);
|
|
2936
|
+
doc.removeEventListener("focusin", onFocusIn, true);
|
|
2937
|
+
remove();
|
|
2938
|
+
};
|
|
2939
|
+
}
|
|
2940
|
+
/**
|
|
2941
|
+
* Keeps Tab inside a root and pulls back focus that escaped it.
|
|
2942
|
+
*
|
|
2943
|
+
* The listeners are on the document, capture phase, because focus can leave the
|
|
2944
|
+
* root without a keydown inside it (a programmatic focus, or a pointer landing
|
|
2945
|
+
* on a page area that is not inert). Tab wraps at the ends of Tab order, not
|
|
2946
|
+
* document order: a positive tabindex comes first, a shadow root orders its own
|
|
2947
|
+
* stops, and a radio group is one stop. Between the ends the browser moves
|
|
2948
|
+
* focus, except across a positive tabindex, which the trap steps onto and off
|
|
2949
|
+
* itself because the browser would leave for the page's own positive stops.
|
|
2950
|
+
*
|
|
2951
|
+
* Focus in a layer portalled from the root (a menu opened inside it) is inside
|
|
2952
|
+
* the root: the trap neither pulls it back nor takes that layer's Tab.
|
|
2953
|
+
*
|
|
2954
|
+
* Each layer owns its trap, and the traps of a document share one rule: the
|
|
2955
|
+
* innermost connected trap is the only one that may move focus or claim Tab.
|
|
2956
|
+
* That is what keeps two modal overlays from calling each other's focus handler
|
|
2957
|
+
* in a loop; a single modal behaves exactly as before, because it is the only
|
|
2958
|
+
* trap and therefore the innermost one.
|
|
2959
|
+
*/
|
|
2960
|
+
function trapFocus(root) {
|
|
2961
|
+
const doc = root.ownerDocument;
|
|
2962
|
+
let record;
|
|
2963
|
+
const onKeyDown = (event) => {
|
|
2964
|
+
if (event.key !== "Tab" || event.defaultPrevented || event.isComposing) return;
|
|
2965
|
+
if (!record || !root.isConnected || innermostTrap(doc) !== record) return;
|
|
2966
|
+
const stops = tabOrder(root);
|
|
2967
|
+
const first = stops[0];
|
|
2968
|
+
const last = stops[stops.length - 1];
|
|
2969
|
+
if (!first || !last) {
|
|
2970
|
+
if (canReceiveFocus(root)) {
|
|
2971
|
+
event.preventDefault();
|
|
2972
|
+
root.focus();
|
|
2973
|
+
}
|
|
2974
|
+
return;
|
|
2975
|
+
}
|
|
2976
|
+
const shift = event.shiftKey;
|
|
2977
|
+
const active = deepActiveElement(doc);
|
|
2978
|
+
if (!(active instanceof HTMLElement) || !composedContains(root, active)) {
|
|
2979
|
+
if (active instanceof Node && (layerTreeContains(root, active) || inPersistentLayer(active))) return;
|
|
2980
|
+
event.preventDefault();
|
|
2981
|
+
(shift ? last : first).focus();
|
|
2982
|
+
return;
|
|
2983
|
+
}
|
|
2984
|
+
const list = focusables(root);
|
|
2985
|
+
const tree = root.getRootNode();
|
|
2986
|
+
const ahead = positiveTabStops(list, root);
|
|
2987
|
+
if (ahead.length > 0) {
|
|
2988
|
+
const light = list.filter((element) => element.getRootNode() === tree);
|
|
2989
|
+
const order = [...ahead, ...light.filter((element) => !ahead.includes(element))];
|
|
2990
|
+
const index = order.indexOf(active);
|
|
2991
|
+
const next = active === root ? order.at(shift ? -1 : 0) : order.at((index + (shift ? -1 : 1)) % order.length);
|
|
2992
|
+
const crossing = ahead.includes(active) || next !== void 0 && ahead.includes(next);
|
|
2993
|
+
const entersShadow = !shift && active.shadowRoot !== null && focusables(active).length > 0;
|
|
2994
|
+
if (active !== root && (index < 0 || !crossing || entersShadow)) return;
|
|
2995
|
+
event.preventDefault();
|
|
2996
|
+
next?.focus();
|
|
2997
|
+
return;
|
|
2998
|
+
}
|
|
2999
|
+
if (shift ? active === first || active === root : active === last) {
|
|
3000
|
+
event.preventDefault();
|
|
3001
|
+
(shift ? last : first).focus();
|
|
3002
|
+
return;
|
|
3003
|
+
}
|
|
3004
|
+
const index = list.indexOf(active);
|
|
3005
|
+
const next = index < 0 ? void 0 : list[index + (shift ? -1 : 1)];
|
|
3006
|
+
const group = next instanceof HTMLInputElement && !next.checked ? radioGroup(next) : [];
|
|
3007
|
+
if (group.some((radio) => radio.checked) && !group.includes(active)) {
|
|
3008
|
+
event.preventDefault();
|
|
3009
|
+
next?.focus();
|
|
3010
|
+
}
|
|
3011
|
+
};
|
|
3012
|
+
const onFocusIn = (event) => {
|
|
3013
|
+
if (!record) return;
|
|
3014
|
+
const target = composedTarget(event);
|
|
3015
|
+
if (focusPull({
|
|
3016
|
+
moving: movingFocus,
|
|
3017
|
+
connected: root.isConnected,
|
|
3018
|
+
visible: doc.visibilityState !== "hidden",
|
|
3019
|
+
innermost: innermostTrap(doc) === record,
|
|
3020
|
+
focusInside: target instanceof Node && (layerTreeContains(root, target) || inPersistentLayer(target))
|
|
3021
|
+
}) === "pull") moveFocusInto(root);
|
|
3022
|
+
};
|
|
3023
|
+
record = {
|
|
3024
|
+
root,
|
|
3025
|
+
doc,
|
|
3026
|
+
onKeyDown,
|
|
3027
|
+
onFocusIn
|
|
3028
|
+
};
|
|
3029
|
+
traps.push(record);
|
|
3030
|
+
doc.addEventListener("keydown", onKeyDown, true);
|
|
3031
|
+
doc.addEventListener("focusin", onFocusIn, true);
|
|
3032
|
+
const stopGuards = guardFrames(root);
|
|
3033
|
+
return () => {
|
|
3034
|
+
if (!record) return;
|
|
3035
|
+
const index = traps.indexOf(record);
|
|
3036
|
+
if (index !== -1) traps.splice(index, 1);
|
|
3037
|
+
doc.removeEventListener("keydown", onKeyDown, true);
|
|
3038
|
+
doc.removeEventListener("focusin", onFocusIn, true);
|
|
3039
|
+
stopGuards();
|
|
3040
|
+
record = void 0;
|
|
3041
|
+
};
|
|
3042
|
+
}
|
|
3043
|
+
/**
|
|
3044
|
+
* Moves Tab between a trigger and a panel portalled away from it, so the panel
|
|
3045
|
+
* sits in the page's Tab order right after its trigger, wherever it renders.
|
|
3046
|
+
*
|
|
3047
|
+
* While bound: Tab from the trigger enters the panel's first stop, Shift+Tab
|
|
3048
|
+
* from the panel's first stop (or the panel itself) goes back to the trigger,
|
|
3049
|
+
* and Tab from its last stop goes to the first tabbable element after the
|
|
3050
|
+
* trigger in document order. With nothing after the trigger, Tab is left to the
|
|
3051
|
+
* browser, which moves focus out of the page. Call it while the panel is open
|
|
3052
|
+
* and non-modal; the returned cleanup removes the listeners.
|
|
3053
|
+
*/
|
|
3054
|
+
function bindFocusFlow(panel, trigger) {
|
|
3055
|
+
const doc = panel.ownerDocument;
|
|
3056
|
+
const onTriggerKeyDown = (event) => {
|
|
3057
|
+
const from = trigger();
|
|
3058
|
+
if (event.key !== "Tab" || event.shiftKey || event.defaultPrevented || from === null || from === void 0) return;
|
|
3059
|
+
const target = composedTarget(event);
|
|
3060
|
+
if (!(target instanceof Node) || !composedContains(from, target)) return;
|
|
3061
|
+
event.preventDefault();
|
|
3062
|
+
(tabOrder(panel)[0] ?? panel).focus();
|
|
3063
|
+
};
|
|
3064
|
+
const onPanelKeyDown = (event) => {
|
|
3065
|
+
const from = trigger();
|
|
3066
|
+
if (event.key !== "Tab" || event.defaultPrevented || event.isComposing || from === null || from === void 0 || !from.isConnected) return;
|
|
3067
|
+
const stops = tabOrder(panel);
|
|
3068
|
+
const active = deepActiveElement(doc);
|
|
3069
|
+
if (event.shiftKey) {
|
|
3070
|
+
if (stops.length === 0 || active === stops[0] || active === panel) {
|
|
3071
|
+
event.preventDefault();
|
|
3072
|
+
from.focus();
|
|
3073
|
+
}
|
|
3074
|
+
return;
|
|
3075
|
+
}
|
|
3076
|
+
if (stops.length === 0 || active === stops[stops.length - 1]) {
|
|
3077
|
+
const next = nextTabbable(from, panel);
|
|
3078
|
+
if (next !== void 0) {
|
|
3079
|
+
event.preventDefault();
|
|
3080
|
+
next.focus();
|
|
3081
|
+
}
|
|
3082
|
+
}
|
|
3083
|
+
};
|
|
3084
|
+
doc.addEventListener("keydown", onTriggerKeyDown, true);
|
|
3085
|
+
panel.addEventListener("keydown", onPanelKeyDown);
|
|
3086
|
+
return () => {
|
|
3087
|
+
doc.removeEventListener("keydown", onTriggerKeyDown, true);
|
|
3088
|
+
panel.removeEventListener("keydown", onPanelKeyDown);
|
|
3089
|
+
};
|
|
3090
|
+
}
|
|
3091
|
+
const lastRememberedByDoc = /* @__PURE__ */ new WeakMap();
|
|
3092
|
+
function restoreElement(element) {
|
|
3093
|
+
if (element?.isConnected) element.focus();
|
|
3094
|
+
}
|
|
3095
|
+
function readPlace(element) {
|
|
3096
|
+
const body = element.ownerDocument.body;
|
|
3097
|
+
const path = [];
|
|
3098
|
+
for (let node = element; node !== null && node !== body; node = node.parentElement) path.push({
|
|
3099
|
+
node,
|
|
3100
|
+
next: node.nextElementSibling,
|
|
3101
|
+
previous: node.previousElementSibling
|
|
3102
|
+
});
|
|
3103
|
+
return path;
|
|
3104
|
+
}
|
|
3105
|
+
/**
|
|
3106
|
+
* The element that took the opener's place in Tab order: the first tabbable
|
|
3107
|
+
* after where it was, in document order (a positive tabindex later in the page
|
|
3108
|
+
* is not "after" it), else the last one before. An opener still in the page
|
|
3109
|
+
* that cannot take focus hands focus to what follows it, as one that left does.
|
|
3110
|
+
*/
|
|
3111
|
+
function neighbourOf(opener, place, panel) {
|
|
3112
|
+
const body = opener.ownerDocument.body;
|
|
3113
|
+
if (body === null) return;
|
|
3114
|
+
const candidates = focusables(body).filter((element) => !logicallyContains(panel, element));
|
|
3115
|
+
const position = (node, candidate) => node.compareDocumentPosition(candidate);
|
|
3116
|
+
const after = (node) => (candidate) => node.contains(candidate) || (position(node, candidate) & Node.DOCUMENT_POSITION_FOLLOWING) !== 0;
|
|
3117
|
+
const before = (node) => (candidate) => !node.contains(candidate) && (position(node, candidate) & Node.DOCUMENT_POSITION_PRECEDING) !== 0;
|
|
3118
|
+
if (opener.isConnected) return candidates.find((candidate) => after(opener)(candidate) && !opener.contains(candidate)) ?? candidates.filter(before(opener)).pop();
|
|
3119
|
+
const index = place.findIndex((step, at) => !step.node.isConnected && (place[at + 1]?.node ?? body).isConnected);
|
|
3120
|
+
const step = place[index];
|
|
3121
|
+
if (step === void 0) return;
|
|
3122
|
+
const parent = place[index + 1]?.node ?? body;
|
|
3123
|
+
const inPlace = (sibling) => sibling !== null && sibling.parentNode === parent;
|
|
3124
|
+
if (inPlace(step.next)) return candidates.find(after(step.next)) ?? candidates.filter(before(step.next)).pop();
|
|
3125
|
+
if (inPlace(step.previous)) {
|
|
3126
|
+
const previous = step.previous;
|
|
3127
|
+
return candidates.find((candidate) => !previous.contains(candidate) && after(previous)(candidate)) ?? candidates.filter((candidate) => previous.contains(candidate) || before(previous)(candidate)).pop();
|
|
3128
|
+
}
|
|
3129
|
+
return candidates.find(after(parent)) ?? candidates.filter(before(parent)).pop();
|
|
3130
|
+
}
|
|
3131
|
+
/**
|
|
3132
|
+
* Whether an element can take focus right now.
|
|
3133
|
+
*
|
|
3134
|
+
* The two facts `decideFocusRestore` asks about an opener are "is it still in
|
|
3135
|
+
* the document" and "can it take focus", and the second is a DOM read: a
|
|
3136
|
+
* disabled trigger, or one a modal above it has made inert, is visible and
|
|
3137
|
+
* unable to hold focus. Calling `focus()` there fails silently, which is the
|
|
3138
|
+
* failure a reader notices as the keyboard having stopped working.
|
|
3139
|
+
*/
|
|
3140
|
+
function canHoldFocus(element) {
|
|
3141
|
+
if (!(element instanceof HTMLElement) || !element.isConnected) return false;
|
|
3142
|
+
if (isDisabledForFocus(element)) return false;
|
|
3143
|
+
return isRendered(element);
|
|
3144
|
+
}
|
|
3145
|
+
/**
|
|
3146
|
+
* Gives focus back after a popup closes.
|
|
3147
|
+
*
|
|
3148
|
+
* The decision is the popups foundation's `decideFocusRestore`, over the facts
|
|
3149
|
+
* this module is the one place able to read: whether the popup ever held focus,
|
|
3150
|
+
* whether focus is still inside it, and whether the remembered opener still
|
|
3151
|
+
* exists and can take focus. Two of its rules replace the blind restore: a popup
|
|
3152
|
+
* that never took focus gives nothing back, and focus the user has already moved
|
|
3153
|
+
* is not taken back — restoring unconditionally could undo a deliberate move to
|
|
3154
|
+
* another field in the same click.
|
|
3155
|
+
*
|
|
3156
|
+
* `"opener"` focuses the trigger. `"body"`, the answer when the opener is gone
|
|
3157
|
+
* or cannot take focus, focuses the caller's fallback, else the element that
|
|
3158
|
+
* took the opener's place in Tab order, else blurs what is left so focus falls
|
|
3159
|
+
* to the document. `"none"` leaves focus exactly where it is.
|
|
3160
|
+
*/
|
|
3161
|
+
function handBackFocus(opener, place, handBack) {
|
|
3162
|
+
const doc = handBack.panel.ownerDocument;
|
|
3163
|
+
const active = deepActiveElement(doc);
|
|
3164
|
+
const focusInside = handBack.focusInside ?? (active instanceof Node && composedContains(handBack.panel, active));
|
|
3165
|
+
const decision = decideFocusRestore({
|
|
3166
|
+
opener: opener === void 0 ? void 0 : {
|
|
3167
|
+
exists: opener.isConnected,
|
|
3168
|
+
focusable: canHoldFocus(opener)
|
|
3169
|
+
},
|
|
3170
|
+
tookFocus: handBack.tookFocus || focusInside,
|
|
3171
|
+
focusInside
|
|
3172
|
+
});
|
|
3173
|
+
if (decision.to === "opener" && opener instanceof HTMLElement) {
|
|
3174
|
+
opener.focus();
|
|
3175
|
+
return;
|
|
3176
|
+
}
|
|
3177
|
+
if (decision.to !== "body") return;
|
|
3178
|
+
const explicit = handBack.fallback?.();
|
|
3179
|
+
const target = explicit !== null && explicit !== void 0 && canHoldFocus(explicit) ? explicit : opener === void 0 ? void 0 : neighbourOf(opener, place, handBack.panel);
|
|
3180
|
+
if (target !== void 0) target.focus();
|
|
3181
|
+
else if (active instanceof HTMLElement) active.blur();
|
|
3182
|
+
}
|
|
3183
|
+
/**
|
|
3184
|
+
* Remembers the element that had focus and returns the release that gives it back.
|
|
3185
|
+
*
|
|
3186
|
+
* Two shapes of release, because there are two kinds of caller. Called with no
|
|
3187
|
+
* argument it is the blind restore it has always been: focus the remembered
|
|
3188
|
+
* element if it is still in the document, which is what `restoreFocus` and a
|
|
3189
|
+
* menu command want. Called with the closing popup it is the hand-back, and the
|
|
3190
|
+
* destination becomes `decideFocusRestore`'s decision — see `handBackFocus`.
|
|
3191
|
+
*
|
|
3192
|
+
* The blind restore is a no-op when the remembered element left the document:
|
|
3193
|
+
* focus must not be forced onto a detached node (browsers silently ignore it,
|
|
3194
|
+
* which would make the failure invisible).
|
|
3195
|
+
*/
|
|
3196
|
+
function rememberFocus(doc = document) {
|
|
3197
|
+
const active = deepActiveElement(doc);
|
|
3198
|
+
const target = active instanceof HTMLElement && active !== doc.body ? active : void 0;
|
|
3199
|
+
if (target === void 0) lastRememberedByDoc.delete(doc);
|
|
3200
|
+
else lastRememberedByDoc.set(doc, target);
|
|
3201
|
+
const place = target === void 0 ? [] : readPlace(target);
|
|
3202
|
+
let released = false;
|
|
3203
|
+
return (handBack) => {
|
|
3204
|
+
if (released) return;
|
|
3205
|
+
released = true;
|
|
3206
|
+
if (handBack === void 0) {
|
|
3207
|
+
restoreElement(target);
|
|
3208
|
+
return;
|
|
3209
|
+
}
|
|
3210
|
+
handBackFocus(target, place, handBack);
|
|
3211
|
+
};
|
|
3212
|
+
}
|
|
3213
|
+
/**
|
|
3214
|
+
* Tracks whether focus is inside one panel, kept from focus events rather than
|
|
3215
|
+
* read on demand: a framework may remove the panel before the close asks, and
|
|
3216
|
+
* the document cannot answer where focus was then.
|
|
3217
|
+
*
|
|
3218
|
+
* A node that loses focus with no related target may be the panel's own close
|
|
3219
|
+
* removing, hiding or making it inert, or the user leaving it for nothing.
|
|
3220
|
+
* Which one is known only once the close has run (a browser can fire the blur
|
|
3221
|
+
* of a removal while the node is still connected), so `held` decides then: the
|
|
3222
|
+
* close took focus when that node can no longer hold it and focus has gone
|
|
3223
|
+
* nowhere since.
|
|
3224
|
+
*/
|
|
3225
|
+
function trackFocusHeld(panel, initiallyHeld = false) {
|
|
3226
|
+
const doc = panel.ownerDocument;
|
|
3227
|
+
let held = initiallyHeld;
|
|
3228
|
+
let dropped;
|
|
3229
|
+
const onFocusIn = () => {
|
|
3230
|
+
held = true;
|
|
3231
|
+
dropped = void 0;
|
|
3232
|
+
};
|
|
3233
|
+
const onFocusOut = (event) => {
|
|
3234
|
+
const target = event.target;
|
|
3235
|
+
const next = event.relatedTarget;
|
|
3236
|
+
dropped = next === null && target instanceof HTMLElement ? target : void 0;
|
|
3237
|
+
held = next instanceof Node && panel.contains(next);
|
|
3238
|
+
};
|
|
3239
|
+
panel.addEventListener("focusin", onFocusIn);
|
|
3240
|
+
panel.addEventListener("focusout", onFocusOut);
|
|
3241
|
+
return {
|
|
3242
|
+
held: () => {
|
|
3243
|
+
if (held || dropped === void 0 || isRendered(dropped)) return held;
|
|
3244
|
+
const active = deepActiveElement(doc);
|
|
3245
|
+
return active === null || active === doc.body;
|
|
3246
|
+
},
|
|
3247
|
+
release() {
|
|
3248
|
+
panel.removeEventListener("focusin", onFocusIn);
|
|
3249
|
+
panel.removeEventListener("focusout", onFocusOut);
|
|
3250
|
+
}
|
|
3251
|
+
};
|
|
3252
|
+
}
|
|
3253
|
+
/** Focuses whatever the last `rememberFocus` recorded for `doc`, if it is still connected. */
|
|
3254
|
+
function restoreFocus(doc = document) {
|
|
3255
|
+
restoreElement(lastRememberedByDoc.get(doc));
|
|
3256
|
+
}
|
|
3257
|
+
const inertStates = /* @__PURE__ */ new WeakMap();
|
|
3258
|
+
function getInertState(doc) {
|
|
3259
|
+
let state = inertStates.get(doc);
|
|
3260
|
+
if (state === void 0) {
|
|
3261
|
+
state = {
|
|
3262
|
+
stack: [],
|
|
3263
|
+
release: void 0
|
|
3264
|
+
};
|
|
3265
|
+
inertStates.set(doc, state);
|
|
3266
|
+
}
|
|
3267
|
+
return state;
|
|
3268
|
+
}
|
|
3269
|
+
function restoreInert(doc) {
|
|
3270
|
+
const state = inertStates.get(doc);
|
|
3271
|
+
if (state === void 0) return;
|
|
3272
|
+
state.release?.();
|
|
3273
|
+
state.release = void 0;
|
|
3274
|
+
state.stack.length = 0;
|
|
3275
|
+
}
|
|
3276
|
+
/** A layer opened from a kept element, or a node portalled from inside one,
|
|
3277
|
+
* belongs to it, like a menu a modal popover opened: it stays usable. */
|
|
3278
|
+
function inertOptions(except) {
|
|
3279
|
+
return {
|
|
3280
|
+
except,
|
|
3281
|
+
keeps: (element) => inPersistentLayer(element) || except.some((kept) => layerTreeContains(kept, element))
|
|
3282
|
+
};
|
|
3283
|
+
}
|
|
3284
|
+
/** Applies the document's inert pass again, for a layer that just opened from
|
|
3285
|
+
* a kept one and may have been made inert while it was closed. */
|
|
3286
|
+
function refreshInert(doc) {
|
|
3287
|
+
const state = inertStates.get(doc);
|
|
3288
|
+
const top = state?.stack[state.stack.length - 1];
|
|
3289
|
+
if (state === void 0 || top === void 0) return;
|
|
3290
|
+
state.release?.();
|
|
3291
|
+
state.release = applyInertRegions(doc, inertOptions(top.except));
|
|
3292
|
+
}
|
|
3293
|
+
/**
|
|
3294
|
+
* Makes everything outside the given elements inert, or restores it. Live
|
|
3295
|
+
* regions stay usable, and so does anything portalled from a kept element.
|
|
3296
|
+
*
|
|
3297
|
+
* Scoped per `Document` and stacked: nested modals push onto the document's
|
|
3298
|
+
* modal stack, updating the exempt regions to the topmost modal and restoring
|
|
3299
|
+
* the previous modal's exemptions (or the full page when empty) on release.
|
|
3300
|
+
*/
|
|
3301
|
+
function setInertOuter(inert, except = [], doc = except[0]?.ownerDocument ?? document) {
|
|
3302
|
+
if (!inert) {
|
|
3303
|
+
restoreInert(doc);
|
|
3304
|
+
return () => {};
|
|
3305
|
+
}
|
|
3306
|
+
const state = getInertState(doc);
|
|
3307
|
+
const entry = { except: [...except] };
|
|
3308
|
+
state.stack.push(entry);
|
|
3309
|
+
state.release?.();
|
|
3310
|
+
state.release = applyInertRegions(doc, inertOptions(entry.except));
|
|
3311
|
+
let released = false;
|
|
3312
|
+
return () => {
|
|
3313
|
+
if (released) return;
|
|
3314
|
+
released = true;
|
|
3315
|
+
const index = state.stack.indexOf(entry);
|
|
3316
|
+
if (index !== -1) state.stack.splice(index, 1);
|
|
3317
|
+
state.release?.();
|
|
3318
|
+
state.release = void 0;
|
|
3319
|
+
const top = state.stack[state.stack.length - 1];
|
|
3320
|
+
if (top !== void 0) state.release = applyInertRegions(doc, inertOptions(top.except));
|
|
3321
|
+
};
|
|
3322
|
+
}
|
|
3323
|
+
/**
|
|
3324
|
+
* Locks page scroll while preserving the scrollbar's width.
|
|
3325
|
+
*
|
|
3326
|
+
* The arithmetic is the platform foundation's `scrollLockEdits`, applied by its
|
|
3327
|
+
* adapter: the scrollbar's width is compensated on the side the scrollbar is
|
|
3328
|
+
* actually on (left in a right-to-left page), added to whatever padding body
|
|
3329
|
+
* already had, and never invented when the document cannot report a client width.
|
|
3330
|
+
* The gutter is not rounded, so a fractional scrollbar leaves no sub-pixel gap.
|
|
3331
|
+
*
|
|
3332
|
+
* Counted per document, so nested overlays share one lock and only the last
|
|
3333
|
+
* release restores the styles.
|
|
3334
|
+
*/
|
|
3335
|
+
function lockScroll(doc = document) {
|
|
3336
|
+
return lockPageScroll(doc);
|
|
3337
|
+
}
|
|
3338
|
+
let announcer;
|
|
3339
|
+
/** Routes announcements to a function instead of the live regions, for tests.
|
|
3340
|
+
* `undefined` puts the real regions back. */
|
|
3341
|
+
function setAnnouncer(next) {
|
|
3342
|
+
announcer = next;
|
|
3343
|
+
}
|
|
3344
|
+
const announcerStates = /* @__PURE__ */ new WeakMap();
|
|
3345
|
+
function getAnnouncerState(doc) {
|
|
3346
|
+
let state = announcerStates.get(doc);
|
|
3347
|
+
if (state === void 0) {
|
|
3348
|
+
state = {
|
|
3349
|
+
regions: {},
|
|
3350
|
+
writers: {
|
|
3351
|
+
polite: { timer: void 0 },
|
|
3352
|
+
assertive: { timer: void 0 }
|
|
3353
|
+
}
|
|
3354
|
+
};
|
|
3355
|
+
announcerStates.set(doc, state);
|
|
3356
|
+
}
|
|
3357
|
+
return state;
|
|
3358
|
+
}
|
|
3359
|
+
function ensureRegion(doc, state, priority) {
|
|
3360
|
+
const existing = state.regions[priority];
|
|
3361
|
+
if (existing?.isConnected) return existing;
|
|
3362
|
+
const region = existing ?? doc.createElement("div");
|
|
3363
|
+
if (!existing) {
|
|
3364
|
+
region.setAttribute(ANNOUNCER_ATTRIBUTE, priority);
|
|
3365
|
+
region.setAttribute("role", priority === "assertive" ? "alert" : "status");
|
|
3366
|
+
region.setAttribute("aria-live", priority);
|
|
3367
|
+
region.setAttribute("aria-atomic", "true");
|
|
3368
|
+
region.setAttribute("style", ANNOUNCER_HIDDEN);
|
|
3369
|
+
state.regions[priority] = region;
|
|
3370
|
+
}
|
|
3371
|
+
(doc.body ?? doc.documentElement).append(region);
|
|
3372
|
+
return region;
|
|
3373
|
+
}
|
|
3374
|
+
/** Creates both live regions ahead of the first announcement, so a screen
|
|
3375
|
+
* reader already follows them when it comes. */
|
|
3376
|
+
function prepareAnnouncements(doc = document) {
|
|
3377
|
+
if (announcer || doc.body === null) return;
|
|
3378
|
+
const state = getAnnouncerState(doc);
|
|
3379
|
+
ensureRegion(doc, state, "polite");
|
|
3380
|
+
ensureRegion(doc, state, "assertive");
|
|
3381
|
+
}
|
|
3382
|
+
/**
|
|
3383
|
+
* Announces text through a live region.
|
|
3384
|
+
*
|
|
3385
|
+
* The region is cleared immediately and written one task later. Writing both in
|
|
3386
|
+
* the same task leaves the accessible name unchanged from the screen reader's
|
|
3387
|
+
* point of view, so the second `"Saved"` is never read; clearing first is what
|
|
3388
|
+
* makes repetition audible. A newer announcement replaces the pending one, so a
|
|
3389
|
+
* burst of messages is not queued behind a stale one.
|
|
3390
|
+
*/
|
|
3391
|
+
function announce(text, options = {}) {
|
|
3392
|
+
const priority = options.priority ?? "polite";
|
|
3393
|
+
if (text === "") return;
|
|
3394
|
+
if (announcer) {
|
|
3395
|
+
announcer(text, { priority });
|
|
3396
|
+
return;
|
|
3397
|
+
}
|
|
3398
|
+
const doc = options.doc ?? document;
|
|
3399
|
+
const state = getAnnouncerState(doc);
|
|
3400
|
+
const region = ensureRegion(doc, state, priority);
|
|
3401
|
+
region.textContent = "";
|
|
3402
|
+
const writer = state.writers[priority];
|
|
3403
|
+
if (writer.timer !== void 0) clearTimeout(writer.timer);
|
|
3404
|
+
writer.timer = setTimeout(() => {
|
|
3405
|
+
writer.timer = void 0;
|
|
3406
|
+
region.textContent = text;
|
|
3407
|
+
}, 0);
|
|
3408
|
+
}
|
|
3409
|
+
// src/scroll-area.ts
|
|
3410
|
+
function finite(value) {
|
|
3411
|
+
return Number.isFinite(value) ? value : 0;
|
|
3412
|
+
}
|
|
3413
|
+
/**
|
|
3414
|
+
* Create the imperative channel a scroll-area primitive consumes.
|
|
3415
|
+
*
|
|
3416
|
+
* Every method reads the element lazily, so the channel may be created before
|
|
3417
|
+
* the refs resolve and keeps working when a framework re-renders the same node.
|
|
3418
|
+
*/
|
|
3419
|
+
function createScrollAreaChannel(elements) {
|
|
3420
|
+
return {
|
|
3421
|
+
read() {
|
|
3422
|
+
const viewport = elements.viewport();
|
|
3423
|
+
if (viewport === null) return {
|
|
3424
|
+
x: 0,
|
|
3425
|
+
y: 0,
|
|
3426
|
+
clientWidth: 0,
|
|
3427
|
+
clientHeight: 0,
|
|
3428
|
+
scrollWidth: 0,
|
|
3429
|
+
scrollHeight: 0
|
|
3430
|
+
};
|
|
3431
|
+
return {
|
|
3432
|
+
x: finite(viewport.scrollLeft),
|
|
3433
|
+
y: finite(viewport.scrollTop),
|
|
3434
|
+
clientWidth: finite(viewport.clientWidth),
|
|
3435
|
+
clientHeight: finite(viewport.clientHeight),
|
|
3436
|
+
scrollWidth: finite(viewport.scrollWidth),
|
|
3437
|
+
scrollHeight: finite(viewport.scrollHeight)
|
|
3438
|
+
};
|
|
3439
|
+
},
|
|
3440
|
+
scroll(axis, value) {
|
|
3441
|
+
const viewport = elements.viewport();
|
|
3442
|
+
if (viewport === null) return;
|
|
3443
|
+
if (axis === "y") viewport.scrollTop = value;
|
|
3444
|
+
else viewport.scrollLeft = value;
|
|
3445
|
+
},
|
|
3446
|
+
trackLength(axis) {
|
|
3447
|
+
const track = elements.track(axis);
|
|
3448
|
+
if (track === null) return 0;
|
|
3449
|
+
return axis === "y" ? finite(track.clientHeight) : finite(track.clientWidth);
|
|
3450
|
+
},
|
|
3451
|
+
focusThumb(axis) {
|
|
3452
|
+
elements.thumb(axis)?.focus();
|
|
3453
|
+
},
|
|
3454
|
+
captureThumb(axis, pointerId) {
|
|
3455
|
+
elements.thumb(axis)?.setPointerCapture?.(pointerId);
|
|
3456
|
+
},
|
|
3457
|
+
releaseThumb(axis, pointerId) {
|
|
3458
|
+
elements.thumb(axis)?.releasePointerCapture?.(pointerId);
|
|
3459
|
+
}
|
|
3460
|
+
};
|
|
3461
|
+
}
|
|
3462
|
+
// src/waterfall.ts
|
|
3463
|
+
/** Create a lazy measurement channel for one waterfall scroll container. */
|
|
3464
|
+
function createWaterfallChannel(viewport) {
|
|
3465
|
+
return { read() {
|
|
3466
|
+
const element = viewport();
|
|
3467
|
+
if (element === null) return {
|
|
3468
|
+
scrollTop: 0,
|
|
3469
|
+
viewportHeight: 0
|
|
3470
|
+
};
|
|
3471
|
+
return {
|
|
3472
|
+
scrollTop: Number.isFinite(element.scrollTop) ? element.scrollTop : 0,
|
|
3473
|
+
viewportHeight: Number.isFinite(element.clientHeight) ? element.clientHeight : 0
|
|
3474
|
+
};
|
|
3475
|
+
} };
|
|
3476
|
+
}
|
|
3477
|
+
// src/direction.ts
|
|
3478
|
+
function elementTarget(input) {
|
|
3479
|
+
const target = input?.target;
|
|
3480
|
+
if (target === null || target === void 0 || typeof target !== "object") return null;
|
|
3481
|
+
const candidate = target;
|
|
3482
|
+
return typeof candidate.closest === "function" ? candidate : null;
|
|
3483
|
+
}
|
|
3484
|
+
/** Resolve the reading direction for one keyboard event's target. */
|
|
3485
|
+
function directionOfKeyEvent(input) {
|
|
3486
|
+
return directionOfElement(elementTarget(input));
|
|
3487
|
+
}
|
|
3488
|
+
/** Resolve the reading direction for one element. */
|
|
3489
|
+
function directionOfElement(target) {
|
|
3490
|
+
if (target === null) return "ltr";
|
|
3491
|
+
if (typeof getComputedStyle === "function") try {
|
|
3492
|
+
let node = target;
|
|
3493
|
+
while (node !== null) {
|
|
3494
|
+
const direction = getComputedStyle(node).direction;
|
|
3495
|
+
if (direction === "rtl" || direction === "ltr") return direction;
|
|
3496
|
+
node = node.parentElement;
|
|
3497
|
+
}
|
|
3498
|
+
} catch {}
|
|
3499
|
+
try {
|
|
3500
|
+
return target.closest("[dir]")?.getAttribute("dir")?.toLowerCase() === "rtl" ? "rtl" : "ltr";
|
|
3501
|
+
} catch {
|
|
3502
|
+
return "ltr";
|
|
3503
|
+
}
|
|
3504
|
+
}
|
|
3505
|
+
// src/rating.ts
|
|
3506
|
+
function targetOf(input) {
|
|
3507
|
+
const target = input?.target;
|
|
3508
|
+
return target !== null && target !== void 0 && typeof target.closest === "function" ? target : null;
|
|
3509
|
+
}
|
|
3510
|
+
function starOfInput(input) {
|
|
3511
|
+
const target = targetOf(input);
|
|
3512
|
+
if (target === null) return null;
|
|
3513
|
+
try {
|
|
3514
|
+
return target.closest?.("[data-index]") ?? null;
|
|
3515
|
+
} catch {
|
|
3516
|
+
return null;
|
|
3517
|
+
}
|
|
3518
|
+
}
|
|
3519
|
+
function indexOfStar(star) {
|
|
3520
|
+
const index = Number(star?.getAttribute("data-index"));
|
|
3521
|
+
return Number.isInteger(index) && index >= 0 ? index : void 0;
|
|
3522
|
+
}
|
|
3523
|
+
/** The star index an opaque input happened on, or undefined when it did not. */
|
|
3524
|
+
function ratingStarIndexForInput(input) {
|
|
3525
|
+
return indexOfStar(starOfInput(input));
|
|
3526
|
+
}
|
|
3527
|
+
/**
|
|
3528
|
+
* Horizontal press position inside a star, as a 0..1 fraction.
|
|
3529
|
+
*
|
|
3530
|
+
* Undefined means there is no trustworthy measurement (no star, no layout, or
|
|
3531
|
+
* a different star than the part asked about), so core uses its whole-star
|
|
3532
|
+
* fallback instead of inventing a half.
|
|
3533
|
+
*/
|
|
3534
|
+
function ratingStarFractionForPress(input, index) {
|
|
3535
|
+
const star = starOfInput(input);
|
|
3536
|
+
if (star === null || indexOfStar(star) !== index) return;
|
|
3537
|
+
const rect = star.getBoundingClientRect();
|
|
3538
|
+
if (rect.width <= 0) return;
|
|
3539
|
+
const clientX = input?.clientX;
|
|
3540
|
+
const x = typeof clientX === "number" ? clientX : rect.left;
|
|
3541
|
+
return Math.min(1, Math.max(0, (x - rect.left) / rect.width));
|
|
3542
|
+
}
|
|
3543
|
+
// src/carousel.ts
|
|
3544
|
+
function targetElement(input) {
|
|
3545
|
+
const target = input?.target;
|
|
3546
|
+
return target instanceof Element ? target : void 0;
|
|
3547
|
+
}
|
|
3548
|
+
function regionOf(input) {
|
|
3549
|
+
const region = targetElement(input)?.closest("[data-part=\"region\"]");
|
|
3550
|
+
return region instanceof HTMLElement ? region : void 0;
|
|
3551
|
+
}
|
|
3552
|
+
function viewportOf$1(input) {
|
|
3553
|
+
const viewport = targetElement(input)?.closest("[data-part=\"viewport\"]");
|
|
3554
|
+
return viewport instanceof HTMLElement ? viewport : void 0;
|
|
3555
|
+
}
|
|
3556
|
+
function pointerIdOf$1(input) {
|
|
3557
|
+
const value = input?.pointerId;
|
|
3558
|
+
return typeof value === "number" ? value : void 0;
|
|
3559
|
+
}
|
|
3560
|
+
/** One slide's travel in pixels, measured by the adapter. */
|
|
3561
|
+
function carouselViewportExtent(input, vertical) {
|
|
3562
|
+
const viewport = viewportOf$1(input);
|
|
3563
|
+
if (viewport === void 0) return 0;
|
|
3564
|
+
const size = vertical ? viewport.clientHeight : viewport.clientWidth;
|
|
3565
|
+
return Number.isFinite(size) && size > 0 ? size : 0;
|
|
3566
|
+
}
|
|
3567
|
+
/** Captures the gesture on the carousel region when the browser supports it. */
|
|
3568
|
+
function carouselCaptureRegion(input) {
|
|
3569
|
+
const region = regionOf(input);
|
|
3570
|
+
const pointerId = pointerIdOf$1(input);
|
|
3571
|
+
if (region === void 0 || pointerId === void 0) return;
|
|
3572
|
+
try {
|
|
3573
|
+
region.setPointerCapture(pointerId);
|
|
3574
|
+
} catch {}
|
|
3575
|
+
}
|
|
3576
|
+
/** Releases the carousel region's capture. */
|
|
3577
|
+
function carouselReleaseRegion(input) {
|
|
3578
|
+
const region = regionOf(input);
|
|
3579
|
+
const pointerId = pointerIdOf$1(input);
|
|
3580
|
+
if (region === void 0 || pointerId === void 0) return;
|
|
3581
|
+
try {
|
|
3582
|
+
region.releasePointerCapture(pointerId);
|
|
3583
|
+
} catch {}
|
|
3584
|
+
}
|
|
3585
|
+
// src/image-cropper.ts
|
|
3586
|
+
function viewportOf(input) {
|
|
3587
|
+
const target = input?.target;
|
|
3588
|
+
if (!(target instanceof Element)) return void 0;
|
|
3589
|
+
const viewport = target.closest("[data-viewport]");
|
|
3590
|
+
return viewport instanceof HTMLElement ? viewport : void 0;
|
|
3591
|
+
}
|
|
3592
|
+
function pointerIdOf(input) {
|
|
3593
|
+
const value = input?.pointerId;
|
|
3594
|
+
return typeof value === "number" ? value : void 0;
|
|
3595
|
+
}
|
|
3596
|
+
/** The viewport's layout box, the one fact only layout knows. */
|
|
3597
|
+
function imageCropperViewportSize(input) {
|
|
3598
|
+
const viewport = viewportOf(input);
|
|
3599
|
+
if (viewport === void 0) return void 0;
|
|
3600
|
+
const rect = viewport.getBoundingClientRect();
|
|
3601
|
+
return {
|
|
3602
|
+
width: Number.isFinite(rect.width) && rect.width > 0 ? rect.width : 0,
|
|
3603
|
+
height: Number.isFinite(rect.height) && rect.height > 0 ? rect.height : 0
|
|
3604
|
+
};
|
|
3605
|
+
}
|
|
3606
|
+
/** The pointer's position from the viewport's centre, in pixels. */
|
|
3607
|
+
function imageCropperAnchor(input, box) {
|
|
3608
|
+
const viewport = viewportOf(input);
|
|
3609
|
+
if (viewport === void 0) return void 0;
|
|
3610
|
+
if (!(box.width > 0) || !(box.height > 0)) return void 0;
|
|
3611
|
+
const raw = input;
|
|
3612
|
+
if (typeof raw?.clientX !== "number" || typeof raw.clientY !== "number") return void 0;
|
|
3613
|
+
const rect = viewport.getBoundingClientRect();
|
|
3614
|
+
return {
|
|
3615
|
+
x: raw.clientX - rect.left - box.width / 2,
|
|
3616
|
+
y: raw.clientY - rect.top - box.height / 2
|
|
3617
|
+
};
|
|
3618
|
+
}
|
|
3619
|
+
/** The pointer's position inside the viewport, from its top left corner. */
|
|
3620
|
+
function imageCropperViewportPoint(input) {
|
|
3621
|
+
const viewport = viewportOf(input);
|
|
3622
|
+
if (viewport === void 0) return void 0;
|
|
3623
|
+
const raw = input;
|
|
3624
|
+
if (typeof raw?.clientX !== "number" || typeof raw.clientY !== "number") return void 0;
|
|
3625
|
+
const rect = viewport.getBoundingClientRect();
|
|
3626
|
+
return {
|
|
3627
|
+
x: raw.clientX - rect.left,
|
|
3628
|
+
y: raw.clientY - rect.top
|
|
3629
|
+
};
|
|
3630
|
+
}
|
|
3631
|
+
/** Captures the gesture on the viewport when the browser supports it. */
|
|
3632
|
+
function imageCropperCaptureViewport(input) {
|
|
3633
|
+
const viewport = viewportOf(input);
|
|
3634
|
+
const pointerId = pointerIdOf(input);
|
|
3635
|
+
if (viewport === void 0 || pointerId === void 0) return;
|
|
3636
|
+
try {
|
|
3637
|
+
viewport.setPointerCapture(pointerId);
|
|
3638
|
+
} catch {}
|
|
3639
|
+
}
|
|
3640
|
+
/** Releases the viewport's capture. */
|
|
3641
|
+
function imageCropperReleaseViewport(input) {
|
|
3642
|
+
const viewport = viewportOf(input);
|
|
3643
|
+
const pointerId = pointerIdOf(input);
|
|
3644
|
+
if (viewport === void 0 || pointerId === void 0) return;
|
|
3645
|
+
try {
|
|
3646
|
+
viewport.releasePointerCapture(pointerId);
|
|
3647
|
+
} catch {}
|
|
3648
|
+
}
|
|
3649
|
+
// src/pad.ts
|
|
3650
|
+
/** The pads with a live session; one gesture per pad at a time. */
|
|
3651
|
+
const activeSessions = /* @__PURE__ */ new WeakMap();
|
|
3652
|
+
function pointIn(pad, clientX, clientY) {
|
|
3653
|
+
const rect = pad.getBoundingClientRect();
|
|
3654
|
+
return {
|
|
3655
|
+
x: rect.width <= 0 ? 0 : Math.min(1, Math.max(0, (clientX - rect.left) / rect.width)),
|
|
3656
|
+
y: rect.height <= 0 ? 0 : Math.min(1, Math.max(0, (clientY - rect.top) / rect.height))
|
|
3657
|
+
};
|
|
3658
|
+
}
|
|
3659
|
+
/**
|
|
3660
|
+
* Starts a pad drag from one opaque pointer input. Returns the session's ender
|
|
3661
|
+
* when the press was claimed, undefined when it was not (no pointer facts, no
|
|
3662
|
+
* pad under the target, a non-primary press, or the pad is already dragged).
|
|
3663
|
+
*
|
|
3664
|
+
* The session emits the position on the press, on every move and on the
|
|
3665
|
+
* release, each with the phase it is in, and ends itself on the gesture
|
|
3666
|
+
* button's release or a cancel. The ender is idempotent; the caller keeps it
|
|
3667
|
+
* for unmounts that arrive mid-drag.
|
|
3668
|
+
*/
|
|
3669
|
+
function startPadSession(input, padSelector, emit) {
|
|
3670
|
+
const raw = input;
|
|
3671
|
+
if (raw === null || typeof raw.pointerId !== "number" || typeof raw.clientX !== "number" || typeof raw.clientY !== "number") return;
|
|
3672
|
+
if (raw.isPrimary === false) return void 0;
|
|
3673
|
+
const button = typeof raw.button === "number" ? raw.button : 0;
|
|
3674
|
+
if (button !== 0) return void 0;
|
|
3675
|
+
const target = raw.target;
|
|
3676
|
+
if (!(target instanceof Element)) return void 0;
|
|
3677
|
+
const found = target.closest(padSelector);
|
|
3678
|
+
if (found === null || activeSessions.has(found)) return void 0;
|
|
3679
|
+
const pad = found;
|
|
3680
|
+
const doc = pad.ownerDocument;
|
|
3681
|
+
const session = {
|
|
3682
|
+
pad,
|
|
3683
|
+
pointerId: raw.pointerId,
|
|
3684
|
+
button,
|
|
3685
|
+
doc,
|
|
3686
|
+
emit,
|
|
3687
|
+
ended: false,
|
|
3688
|
+
last: {
|
|
3689
|
+
x: 0,
|
|
3690
|
+
y: 0
|
|
3691
|
+
}
|
|
3692
|
+
};
|
|
3693
|
+
const end = () => {
|
|
3694
|
+
if (session.ended) return;
|
|
3695
|
+
session.ended = true;
|
|
3696
|
+
doc.removeEventListener("pointermove", onMove, true);
|
|
3697
|
+
doc.removeEventListener("pointerup", onUp, true);
|
|
3698
|
+
doc.removeEventListener("pointercancel", onCancel, true);
|
|
3699
|
+
try {
|
|
3700
|
+
pad.releasePointerCapture?.(session.pointerId);
|
|
3701
|
+
} catch {}
|
|
3702
|
+
if (activeSessions.get(pad) === session) activeSessions.delete(pad);
|
|
3703
|
+
};
|
|
3704
|
+
function factsOf(event) {
|
|
3705
|
+
const e = event;
|
|
3706
|
+
return typeof e.pointerId === "number" && typeof e.clientX === "number" && typeof e.clientY === "number" ? {
|
|
3707
|
+
pointerId: e.pointerId,
|
|
3708
|
+
clientX: e.clientX,
|
|
3709
|
+
clientY: e.clientY,
|
|
3710
|
+
button: typeof e.button === "number" ? e.button : 0
|
|
3711
|
+
} : void 0;
|
|
3712
|
+
}
|
|
3713
|
+
function onMove(event) {
|
|
3714
|
+
if (session.ended) return;
|
|
3715
|
+
const facts = factsOf(event);
|
|
3716
|
+
if (facts === void 0 || facts.pointerId !== session.pointerId) return;
|
|
3717
|
+
session.last = pointIn(pad, facts.clientX, facts.clientY);
|
|
3718
|
+
session.emit(session.last, "move");
|
|
3719
|
+
}
|
|
3720
|
+
function onUp(event) {
|
|
3721
|
+
if (session.ended) return;
|
|
3722
|
+
const facts = factsOf(event);
|
|
3723
|
+
if (facts === void 0 || facts.pointerId !== session.pointerId) return;
|
|
3724
|
+
if (facts.button !== session.button) return;
|
|
3725
|
+
session.last = pointIn(pad, facts.clientX, facts.clientY);
|
|
3726
|
+
session.emit(session.last, "end");
|
|
3727
|
+
end();
|
|
3728
|
+
}
|
|
3729
|
+
function onCancel(event) {
|
|
3730
|
+
if (session.ended) return;
|
|
3731
|
+
const facts = factsOf(event);
|
|
3732
|
+
if (facts === void 0 || facts.pointerId !== session.pointerId) return;
|
|
3733
|
+
session.emit(session.last, "cancel");
|
|
3734
|
+
end();
|
|
3735
|
+
}
|
|
3736
|
+
activeSessions.set(pad, session);
|
|
3737
|
+
doc.addEventListener("pointermove", onMove, true);
|
|
3738
|
+
doc.addEventListener("pointerup", onUp, true);
|
|
3739
|
+
doc.addEventListener("pointercancel", onCancel, true);
|
|
3740
|
+
try {
|
|
3741
|
+
pad.setPointerCapture?.(session.pointerId);
|
|
3742
|
+
} catch {}
|
|
3743
|
+
raw.preventDefault?.();
|
|
3744
|
+
if (pad instanceof HTMLElement) pad.focus();
|
|
3745
|
+
session.last = pointIn(pad, raw.clientX, raw.clientY);
|
|
3746
|
+
emit(session.last, "press");
|
|
3747
|
+
return end;
|
|
3748
|
+
}
|
|
3749
|
+
// src/drag-track.ts
|
|
3750
|
+
const NO_TRAVEL = {
|
|
3751
|
+
dx: 0,
|
|
3752
|
+
dy: 0
|
|
3753
|
+
};
|
|
3754
|
+
/**
|
|
3755
|
+
* Starts a drag from one opaque pointer input. Returns the session's ender when
|
|
3756
|
+
* the press was claimed, undefined when it was not (no pointer facts, a
|
|
3757
|
+
* non-primary press, or a press of another button).
|
|
3758
|
+
*
|
|
3759
|
+
* The session emits zero travel on the press, the travel so far on every move,
|
|
3760
|
+
* and the final travel on the release. The ender is idempotent; the caller
|
|
3761
|
+
* keeps it for unmounts that arrive mid-drag.
|
|
3762
|
+
*/
|
|
3763
|
+
function startDragSession(input, emit) {
|
|
3764
|
+
const raw = input;
|
|
3765
|
+
if (raw === null || typeof raw.pointerId !== "number" || typeof raw.clientX !== "number" || typeof raw.clientY !== "number") return;
|
|
3766
|
+
if (raw.isPrimary === false) return void 0;
|
|
3767
|
+
const button = typeof raw.button === "number" ? raw.button : 0;
|
|
3768
|
+
if (button !== 0) return void 0;
|
|
3769
|
+
const target = raw.target;
|
|
3770
|
+
if (!(target instanceof Element)) return void 0;
|
|
3771
|
+
const doc = target.ownerDocument;
|
|
3772
|
+
const session = {
|
|
3773
|
+
pointerId: raw.pointerId,
|
|
3774
|
+
button,
|
|
3775
|
+
doc,
|
|
3776
|
+
from: {
|
|
3777
|
+
x: raw.clientX,
|
|
3778
|
+
y: raw.clientY
|
|
3779
|
+
},
|
|
3780
|
+
ended: false,
|
|
3781
|
+
last: NO_TRAVEL
|
|
3782
|
+
};
|
|
3783
|
+
const end = () => {
|
|
3784
|
+
if (session.ended) return;
|
|
3785
|
+
session.ended = true;
|
|
3786
|
+
doc.removeEventListener("pointermove", onMove, true);
|
|
3787
|
+
doc.removeEventListener("pointerup", onUp, true);
|
|
3788
|
+
doc.removeEventListener("pointercancel", onCancel, true);
|
|
3789
|
+
doc.removeEventListener("keydown", onKey, true);
|
|
3790
|
+
try {
|
|
3791
|
+
target.releasePointerCapture?.(session.pointerId);
|
|
3792
|
+
} catch {}
|
|
3793
|
+
};
|
|
3794
|
+
function factsOf(event) {
|
|
3795
|
+
const e = event;
|
|
3796
|
+
return typeof e.pointerId === "number" && typeof e.clientX === "number" && typeof e.clientY === "number" ? {
|
|
3797
|
+
pointerId: e.pointerId,
|
|
3798
|
+
clientX: e.clientX,
|
|
3799
|
+
clientY: e.clientY,
|
|
3800
|
+
button: typeof e.button === "number" ? e.button : 0
|
|
3801
|
+
} : void 0;
|
|
3802
|
+
}
|
|
3803
|
+
function travelTo(clientX, clientY) {
|
|
3804
|
+
return {
|
|
3805
|
+
dx: clientX - session.from.x,
|
|
3806
|
+
dy: clientY - session.from.y
|
|
3807
|
+
};
|
|
3808
|
+
}
|
|
3809
|
+
function onMove(event) {
|
|
3810
|
+
if (session.ended) return;
|
|
3811
|
+
const facts = factsOf(event);
|
|
3812
|
+
if (facts === void 0 || facts.pointerId !== session.pointerId) return;
|
|
3813
|
+
session.last = travelTo(facts.clientX, facts.clientY);
|
|
3814
|
+
emit(session.last, "move");
|
|
3815
|
+
}
|
|
3816
|
+
function onUp(event) {
|
|
3817
|
+
if (session.ended) return;
|
|
3818
|
+
const facts = factsOf(event);
|
|
3819
|
+
if (facts === void 0 || facts.pointerId !== session.pointerId) return;
|
|
3820
|
+
if (facts.button !== session.button) return;
|
|
3821
|
+
session.last = travelTo(facts.clientX, facts.clientY);
|
|
3822
|
+
emit(session.last, "end");
|
|
3823
|
+
end();
|
|
3824
|
+
}
|
|
3825
|
+
function onCancel(event) {
|
|
3826
|
+
if (session.ended) return;
|
|
3827
|
+
const facts = factsOf(event);
|
|
3828
|
+
if (facts === void 0 || facts.pointerId !== session.pointerId) return;
|
|
3829
|
+
emit(session.last, "cancel");
|
|
3830
|
+
end();
|
|
3831
|
+
}
|
|
3832
|
+
function onKey(event) {
|
|
3833
|
+
if (session.ended) return;
|
|
3834
|
+
if (event.key !== "Escape") return;
|
|
3835
|
+
emit(session.last, "cancel");
|
|
3836
|
+
end();
|
|
3837
|
+
}
|
|
3838
|
+
doc.addEventListener("pointermove", onMove, true);
|
|
3839
|
+
doc.addEventListener("pointerup", onUp, true);
|
|
3840
|
+
doc.addEventListener("pointercancel", onCancel, true);
|
|
3841
|
+
doc.addEventListener("keydown", onKey, true);
|
|
3842
|
+
try {
|
|
3843
|
+
target.setPointerCapture?.(session.pointerId);
|
|
3844
|
+
} catch {}
|
|
3845
|
+
raw.preventDefault?.();
|
|
3846
|
+
if (target instanceof HTMLElement) target.focus();
|
|
3847
|
+
emit(NO_TRAVEL, "press");
|
|
3848
|
+
return end;
|
|
3849
|
+
}
|
|
3850
|
+
// src/resizable.ts
|
|
3851
|
+
/**
|
|
3852
|
+
* How wide a hit area is at least, in CSS pixels, centred on the handle. A thin
|
|
3853
|
+
* line stays easy to grab, and a touch gets the smallest target size the
|
|
3854
|
+
* accessibility guidelines ask for.
|
|
3855
|
+
*/
|
|
3856
|
+
const HIT_SIZE = {
|
|
3857
|
+
fine: 10,
|
|
3858
|
+
coarse: 24
|
|
3859
|
+
};
|
|
3860
|
+
/** Sizes nearer than this are the same, so a limit reads as reached. */
|
|
3861
|
+
const EPSILON = 1e-6;
|
|
3862
|
+
const targets = /* @__PURE__ */ new Set();
|
|
3863
|
+
const listening = /* @__PURE__ */ new WeakSet();
|
|
3864
|
+
let hovered = [];
|
|
3865
|
+
let active = [];
|
|
3866
|
+
let activeTouch = false;
|
|
3867
|
+
let releaseTouch = () => {};
|
|
3868
|
+
/** The pixels a group's panels share: what the panels measure, whatever the handles take. */
|
|
3869
|
+
function resizableSpace(root, direction) {
|
|
3870
|
+
const side = direction === "horizontal" ? "width" : "height";
|
|
3871
|
+
let total = 0;
|
|
3872
|
+
for (const panel of root.querySelectorAll("[data-part=\"panel\"]")) {
|
|
3873
|
+
if (panel.closest("[data-part=\"resizable\"]") !== root) continue;
|
|
3874
|
+
total += panel.getBoundingClientRect()[side];
|
|
3875
|
+
}
|
|
3876
|
+
return total;
|
|
3877
|
+
}
|
|
3878
|
+
/** Makes a handle findable by the presses and hovers near it. Returns the cleanup. */
|
|
3879
|
+
function registerResizableHandle(target) {
|
|
3880
|
+
targets.add(target);
|
|
3881
|
+
listen(target.el.ownerDocument);
|
|
3882
|
+
return () => {
|
|
3883
|
+
targets.delete(target);
|
|
3884
|
+
if (hovered.includes(target)) setHovered(hovered.filter((other) => other !== target));
|
|
3885
|
+
releaseResizableHandle(target);
|
|
3886
|
+
};
|
|
3887
|
+
}
|
|
3888
|
+
/** Ends a handle's drag in the page's eyes: its cursor and the drag styles go. */
|
|
3889
|
+
function releaseResizableHandle(target) {
|
|
3890
|
+
active = active.filter((other) => other !== target);
|
|
3891
|
+
if (active.length === 0) releaseTouch();
|
|
3892
|
+
updateCursor(target.el.ownerDocument);
|
|
3893
|
+
}
|
|
3894
|
+
/** The cursor may have changed, as it does when a handle reaches a limit. */
|
|
3895
|
+
function refreshResizableCursor(doc) {
|
|
3896
|
+
updateCursor(doc);
|
|
3897
|
+
}
|
|
3898
|
+
/** The cursor for a handle: its axis, or at a limit the one way it can still go. */
|
|
3899
|
+
function resizableCursor(value, direction, rtl) {
|
|
3900
|
+
const vertical = direction === "vertical";
|
|
3901
|
+
const both = vertical ? "ns-resize" : "ew-resize";
|
|
3902
|
+
if (value === void 0) return both;
|
|
3903
|
+
const atMin = value.now <= value.min + EPSILON;
|
|
3904
|
+
if (atMin === value.now >= value.max - EPSILON) return both;
|
|
3905
|
+
if (vertical) return atMin ? "s-resize" : "n-resize";
|
|
3906
|
+
return atMin !== rtl ? "e-resize" : "w-resize";
|
|
3907
|
+
}
|
|
3908
|
+
/**
|
|
3909
|
+
* Tracks one drag from the press that started it: every move is reported as a
|
|
3910
|
+
* point, and the end says whether Escape cancelled it. The returned ender is
|
|
3911
|
+
* idempotent, for a handle that leaves the page mid-drag.
|
|
3912
|
+
*/
|
|
3913
|
+
function trackResizableDrag(el, event, handlers) {
|
|
3914
|
+
const doc = el.ownerDocument;
|
|
3915
|
+
const pointerId = event.pointerId;
|
|
3916
|
+
let ended = false;
|
|
3917
|
+
try {
|
|
3918
|
+
el.setPointerCapture(pointerId);
|
|
3919
|
+
} catch {}
|
|
3920
|
+
const stop = () => {
|
|
3921
|
+
doc.removeEventListener("pointermove", move);
|
|
3922
|
+
doc.removeEventListener("pointerup", up);
|
|
3923
|
+
doc.removeEventListener("pointercancel", cancel);
|
|
3924
|
+
doc.removeEventListener("keydown", key, true);
|
|
3925
|
+
try {
|
|
3926
|
+
el.releasePointerCapture(pointerId);
|
|
3927
|
+
} catch {}
|
|
3928
|
+
};
|
|
3929
|
+
const finish = (next, canceled) => {
|
|
3930
|
+
if (ended) return;
|
|
3931
|
+
ended = true;
|
|
3932
|
+
stop();
|
|
3933
|
+
handlers.onEnd(next, canceled);
|
|
3934
|
+
};
|
|
3935
|
+
const move = (next) => {
|
|
3936
|
+
const pointer = next;
|
|
3937
|
+
if (pointer.pointerId !== pointerId) return;
|
|
3938
|
+
handlers.onMove({
|
|
3939
|
+
x: pointer.clientX,
|
|
3940
|
+
y: pointer.clientY,
|
|
3941
|
+
event: pointer
|
|
3942
|
+
});
|
|
3943
|
+
};
|
|
3944
|
+
const up = (next) => {
|
|
3945
|
+
const pointer = next;
|
|
3946
|
+
if (pointer.pointerId !== pointerId) return;
|
|
3947
|
+
finish(pointer, false);
|
|
3948
|
+
};
|
|
3949
|
+
const cancel = (next) => {
|
|
3950
|
+
const pointer = next;
|
|
3951
|
+
if (pointer.pointerId !== pointerId) return;
|
|
3952
|
+
finish(pointer, false);
|
|
3953
|
+
};
|
|
3954
|
+
const key = (next) => {
|
|
3955
|
+
if (next.key !== "Escape") return;
|
|
3956
|
+
next.preventDefault();
|
|
3957
|
+
next.stopPropagation();
|
|
3958
|
+
finish(void 0, true);
|
|
3959
|
+
};
|
|
3960
|
+
doc.addEventListener("pointermove", move);
|
|
3961
|
+
doc.addEventListener("pointerup", up);
|
|
3962
|
+
doc.addEventListener("pointercancel", cancel);
|
|
3963
|
+
doc.addEventListener("keydown", key, true);
|
|
3964
|
+
return () => {
|
|
3965
|
+
finish(void 0, false);
|
|
3966
|
+
};
|
|
3967
|
+
}
|
|
3968
|
+
/** Whether a press landed inside one of a handle's two elements. */
|
|
3969
|
+
function owns(target, top) {
|
|
3970
|
+
return target.el.contains(top) || target.root()?.contains(top) === true;
|
|
3971
|
+
}
|
|
3972
|
+
function someOwns(top) {
|
|
3973
|
+
for (const target of targets) if (owns(target, top)) return true;
|
|
3974
|
+
return false;
|
|
3975
|
+
}
|
|
3976
|
+
/** The handles at a point: the nearest, plus one of the other axis where two meet. */
|
|
3977
|
+
function hitTest(x, y, coarse, top) {
|
|
3978
|
+
if (top !== null && !someOwns(top)) return [];
|
|
3979
|
+
const pad = coarse ? HIT_SIZE.coarse : HIT_SIZE.fine;
|
|
3980
|
+
const hits = [];
|
|
3981
|
+
for (const target of targets) {
|
|
3982
|
+
const axis = target.axis();
|
|
3983
|
+
if (axis === void 0 || !target.el.isConnected) continue;
|
|
3984
|
+
const rect = target.el.getBoundingClientRect();
|
|
3985
|
+
if (rect.width === 0 && rect.height === 0) continue;
|
|
3986
|
+
const thickness = axis === "x" ? rect.width : rect.height;
|
|
3987
|
+
const reach = Math.max(0, (pad - thickness) / 2);
|
|
3988
|
+
const dx = Math.max(rect.left - x, 0, x - rect.right);
|
|
3989
|
+
const dy = Math.max(rect.top - y, 0, y - rect.bottom);
|
|
3990
|
+
if (dx <= reach && dy <= reach) hits.push({
|
|
3991
|
+
target,
|
|
3992
|
+
axis,
|
|
3993
|
+
distance: Math.hypot(dx, dy)
|
|
3994
|
+
});
|
|
3995
|
+
}
|
|
3996
|
+
if (top !== null && !hits.some((hit) => owns(hit.target, top))) return [];
|
|
3997
|
+
hits.sort((a, b) => a.distance - b.distance);
|
|
3998
|
+
const nearest = hits[0];
|
|
3999
|
+
if (nearest === void 0) return [];
|
|
4000
|
+
const crossing = hits.find((hit) => hit.axis !== nearest.axis);
|
|
4001
|
+
return crossing === void 0 ? [nearest.target] : [nearest.target, crossing.target];
|
|
4002
|
+
}
|
|
4003
|
+
function listen(doc) {
|
|
4004
|
+
if (listening.has(doc)) return;
|
|
4005
|
+
listening.add(doc);
|
|
4006
|
+
const view = doc.defaultView ?? window;
|
|
4007
|
+
let frame = 0;
|
|
4008
|
+
let point;
|
|
4009
|
+
doc.addEventListener("pointerdown", (event) => {
|
|
4010
|
+
if (event.button !== 0 || !event.isPrimary || event.defaultPrevented || targets.size === 0) return;
|
|
4011
|
+
const top = event.target instanceof Element ? event.target : null;
|
|
4012
|
+
const hits = hitTest(event.clientX, event.clientY, event.pointerType !== "mouse", top);
|
|
4013
|
+
if (hits.length === 0) return;
|
|
4014
|
+
event.preventDefault();
|
|
4015
|
+
if (top !== null && !hits.some((hit) => hit.el.contains(top))) swallowNextClick(doc);
|
|
4016
|
+
active = hits;
|
|
4017
|
+
activeTouch = event.pointerType === "touch";
|
|
4018
|
+
if (activeTouch) holdTouch(doc);
|
|
4019
|
+
for (const hit of hits) hit.start(event, () => releaseResizableHandle(hit));
|
|
4020
|
+
updateCursor(doc);
|
|
4021
|
+
});
|
|
4022
|
+
doc.addEventListener("pointermove", (event) => {
|
|
4023
|
+
if (event.buttons !== 0 || active.length > 0 || targets.size === 0) return;
|
|
4024
|
+
point = {
|
|
4025
|
+
x: event.clientX,
|
|
4026
|
+
y: event.clientY,
|
|
4027
|
+
coarse: event.pointerType !== "mouse"
|
|
4028
|
+
};
|
|
4029
|
+
frame ||= view.requestAnimationFrame(() => {
|
|
4030
|
+
frame = 0;
|
|
4031
|
+
if (point === void 0 || active.length > 0) return;
|
|
4032
|
+
const top = typeof doc.elementFromPoint === "function" ? doc.elementFromPoint(point.x, point.y) : null;
|
|
4033
|
+
setHovered(hitTest(point.x, point.y, point.coarse, top));
|
|
4034
|
+
updateCursor(doc);
|
|
4035
|
+
});
|
|
4036
|
+
}, { passive: true });
|
|
4037
|
+
doc.documentElement.addEventListener("pointerleave", () => {
|
|
4038
|
+
point = void 0;
|
|
4039
|
+
setHovered([]);
|
|
4040
|
+
updateCursor(doc);
|
|
4041
|
+
});
|
|
4042
|
+
}
|
|
4043
|
+
/** Swallows the click a press beside a handle would otherwise send on release. */
|
|
4044
|
+
function swallowNextClick(doc) {
|
|
4045
|
+
const swallow = (event) => {
|
|
4046
|
+
event.preventDefault();
|
|
4047
|
+
event.stopPropagation();
|
|
4048
|
+
doc.removeEventListener("click", swallow, true);
|
|
4049
|
+
};
|
|
4050
|
+
doc.addEventListener("click", swallow, true);
|
|
4051
|
+
(doc.defaultView ?? window).setTimeout(() => {
|
|
4052
|
+
doc.removeEventListener("click", swallow, true);
|
|
4053
|
+
}, 0);
|
|
4054
|
+
}
|
|
4055
|
+
/**
|
|
4056
|
+
* A press in the touch hit area but beside the handle lands where the page can
|
|
4057
|
+
* pan, and the browser would cancel the drag to scroll it.
|
|
4058
|
+
*/
|
|
4059
|
+
function holdTouch(doc) {
|
|
4060
|
+
releaseTouch();
|
|
4061
|
+
const hold = (event) => {
|
|
4062
|
+
event.preventDefault();
|
|
4063
|
+
};
|
|
4064
|
+
const end = (event) => {
|
|
4065
|
+
if (event.touches.length === 0) releaseTouch();
|
|
4066
|
+
};
|
|
4067
|
+
releaseTouch = () => {
|
|
4068
|
+
doc.removeEventListener("touchmove", hold, true);
|
|
4069
|
+
doc.removeEventListener("touchend", end, true);
|
|
4070
|
+
doc.removeEventListener("touchcancel", end, true);
|
|
4071
|
+
releaseTouch = () => {};
|
|
4072
|
+
};
|
|
4073
|
+
doc.addEventListener("touchmove", hold, {
|
|
4074
|
+
capture: true,
|
|
4075
|
+
passive: false
|
|
4076
|
+
});
|
|
4077
|
+
doc.addEventListener("touchend", end, true);
|
|
4078
|
+
doc.addEventListener("touchcancel", end, true);
|
|
4079
|
+
}
|
|
4080
|
+
function setHovered(next) {
|
|
4081
|
+
for (const target of hovered) if (!next.includes(target)) target.hover(false);
|
|
4082
|
+
for (const target of next) if (!hovered.includes(target)) target.hover(true);
|
|
4083
|
+
hovered = next;
|
|
4084
|
+
}
|
|
4085
|
+
/**
|
|
4086
|
+
* The cursor shows everywhere while a handle is hovered or dragged, so it stays
|
|
4087
|
+
* as the pointer passes over a panel's content. A drag also stops text
|
|
4088
|
+
* selection and keeps an iframe from swallowing the moves.
|
|
4089
|
+
*/
|
|
4090
|
+
function updateCursor(doc) {
|
|
4091
|
+
const shown = active.length > 0 ? active : hovered;
|
|
4092
|
+
if (shown.length === 0 || active.length > 0 && activeTouch) {
|
|
4093
|
+
setPageStyle(doc, "");
|
|
4094
|
+
return;
|
|
4095
|
+
}
|
|
4096
|
+
const first = shown[0];
|
|
4097
|
+
setPageStyle(doc, `*,*::before,*::after{cursor:${shown.length > 1 || first === void 0 ? "move" : first.cursor()}!important;${active.length > 0 ? "user-select:none!important;-webkit-user-select:none!important}iframe{pointer-events:none!important" : ""}}`);
|
|
4098
|
+
}
|
|
4099
|
+
const sheets = /* @__PURE__ */ new WeakMap();
|
|
4100
|
+
/**
|
|
4101
|
+
* One style sheet per document, adopted after the page's own, so its important
|
|
4102
|
+
* rules win. Writing a sheet restyles the whole page, so the same text is never
|
|
4103
|
+
* written twice.
|
|
4104
|
+
*/
|
|
4105
|
+
function setPageStyle(doc, css) {
|
|
4106
|
+
let sheet = sheets.get(doc);
|
|
4107
|
+
if (sheet === void 0) {
|
|
4108
|
+
if (css === "") return;
|
|
4109
|
+
sheet = {
|
|
4110
|
+
css: "",
|
|
4111
|
+
set: pageSheet(doc)
|
|
4112
|
+
};
|
|
4113
|
+
sheets.set(doc, sheet);
|
|
4114
|
+
}
|
|
4115
|
+
if (sheet.css === css) return;
|
|
4116
|
+
sheet.css = css;
|
|
4117
|
+
sheet.set(css);
|
|
4118
|
+
}
|
|
4119
|
+
function pageSheet(doc) {
|
|
4120
|
+
try {
|
|
4121
|
+
const view = doc.defaultView;
|
|
4122
|
+
if (view === null) throw new Error("no view");
|
|
4123
|
+
const sheet = new view.CSSStyleSheet();
|
|
4124
|
+
doc.adoptedStyleSheets = [...doc.adoptedStyleSheets, sheet];
|
|
4125
|
+
return (css) => {
|
|
4126
|
+
sheet.replaceSync(css);
|
|
4127
|
+
};
|
|
4128
|
+
} catch {
|
|
4129
|
+
const style = doc.createElement("style");
|
|
4130
|
+
style.dataset["beveloResizable"] = "";
|
|
4131
|
+
doc.head.append(style);
|
|
4132
|
+
return (css) => {
|
|
4133
|
+
style.textContent = css;
|
|
4134
|
+
};
|
|
4135
|
+
}
|
|
4136
|
+
}
|
|
4137
|
+
// src/roving.ts
|
|
4138
|
+
function caretOf(el) {
|
|
4139
|
+
if (!(el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement)) return;
|
|
4140
|
+
return {
|
|
4141
|
+
start: el.selectionStart,
|
|
4142
|
+
end: el.selectionEnd,
|
|
4143
|
+
length: el.value.length,
|
|
4144
|
+
rtl: isRtlElement(el),
|
|
4145
|
+
usesUpDown: el instanceof HTMLTextAreaElement || el.type === "number"
|
|
4146
|
+
};
|
|
4147
|
+
}
|
|
4148
|
+
/**
|
|
4149
|
+
* Whether a key belongs to the caret of the text field it was pressed in.
|
|
4150
|
+
*
|
|
4151
|
+
* Home and End always move the caret. A horizontal arrow does while the caret
|
|
4152
|
+
* can still travel the way it points, or while text is selected; at the end it
|
|
4153
|
+
* points at, the key is free for whatever moves between the fields' neighbours.
|
|
4154
|
+
* A field with no caret at all, like a checkbox, keeps no arrow.
|
|
4155
|
+
*/
|
|
4156
|
+
function fieldKeepsCaretKey(event, el) {
|
|
4157
|
+
const caret = caretOf(el);
|
|
4158
|
+
if (caret === void 0) return false;
|
|
4159
|
+
if (event.key === "Home" || event.key === "End") return true;
|
|
4160
|
+
if (event.key === "ArrowUp" || event.key === "ArrowDown") return caret.usesUpDown;
|
|
4161
|
+
if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return false;
|
|
4162
|
+
if (caret.start === null || caret.end === null) return false;
|
|
4163
|
+
if (caret.start !== caret.end) return true;
|
|
4164
|
+
return event.key === (caret.rtl ? "ArrowRight" : "ArrowLeft") ? caret.start > 0 : caret.end < caret.length;
|
|
4165
|
+
}
|
|
4166
|
+
/**
|
|
4167
|
+
* Puts a text field's caret at the end a move pointed at, so pressing the same
|
|
4168
|
+
* key again travels on instead of walking back through the text.
|
|
4169
|
+
*/
|
|
4170
|
+
function placeFieldCaret(el, atEnd) {
|
|
4171
|
+
if (!(el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) || el.selectionStart === null) return;
|
|
4172
|
+
const at = atEnd ? el.value.length : 0;
|
|
4173
|
+
el.setSelectionRange(at, at);
|
|
4174
|
+
}
|
|
4175
|
+
/**
|
|
4176
|
+
* The writing direction an element sits in: computed CSS first, then the
|
|
4177
|
+
* nearest `dir`, because a stylesheet can set the direction without the
|
|
4178
|
+
* attribute and jsdom answers neither reliably.
|
|
4179
|
+
*/
|
|
4180
|
+
function isRtlElement(el) {
|
|
4181
|
+
if (typeof getComputedStyle === "function") try {
|
|
4182
|
+
const direction = getComputedStyle(el).direction;
|
|
4183
|
+
if (direction === "rtl" || direction === "ltr") return direction === "rtl";
|
|
4184
|
+
} catch {}
|
|
4185
|
+
return el.closest("[dir]")?.getAttribute("dir")?.toLowerCase() === "rtl";
|
|
4186
|
+
}
|
|
4187
|
+
const DEFAULT_ORIENTATION = "horizontal";
|
|
4188
|
+
const DEFAULT_LOOP = false;
|
|
4189
|
+
/**
|
|
4190
|
+
* The items a roving group treats as "chosen".
|
|
4191
|
+
*
|
|
4192
|
+
* aria-current is included because a pagination's current page is the tab stop
|
|
4193
|
+
* even though its role carries no checked/selected state; without it the first
|
|
4194
|
+
* page would win and a user tabbing in would land away from the page they are
|
|
4195
|
+
* on. A timeline's current step is the same case.
|
|
4196
|
+
*/
|
|
4197
|
+
const CHOSEN_SELECTOR = "[aria-checked=\"true\"], [aria-pressed=\"true\"], [aria-selected=\"true\"], [aria-current=\"page\"], [aria-current=\"step\"]";
|
|
4198
|
+
function writeTabIndex(item, value) {
|
|
4199
|
+
if (item.getAttribute("tabindex") !== value) item.setAttribute("tabindex", value);
|
|
4200
|
+
}
|
|
4201
|
+
function isDisabled(item) {
|
|
4202
|
+
return item.hasAttribute("disabled") || item.getAttribute("aria-disabled") === "true";
|
|
4203
|
+
}
|
|
4204
|
+
/**
|
|
4205
|
+
* Creates a roving-focus engine over the items inside `container`.
|
|
4206
|
+
*
|
|
4207
|
+
* Exactly one item carries tabindex=0 at any time: the item that had focus
|
|
4208
|
+
* last, else the chosen item, else the first enabled one. Disabled items are
|
|
4209
|
+
* skipped by every movement. A container whose items are all disabled has no tab
|
|
4210
|
+
* stop at all — the honest answer, rather than a stop that goes nowhere.
|
|
4211
|
+
*/
|
|
4212
|
+
function createRovingFocus(container, options = {}) {
|
|
4213
|
+
const orientation = options.orientation ?? DEFAULT_ORIENTATION;
|
|
4214
|
+
const loop = options.loop ?? DEFAULT_LOOP;
|
|
4215
|
+
let stopped = false;
|
|
4216
|
+
let destroyed = false;
|
|
4217
|
+
let lastFocused;
|
|
4218
|
+
let tabStopItem;
|
|
4219
|
+
const items = () => {
|
|
4220
|
+
if (options.itemSelector === void 0) {
|
|
4221
|
+
const children = container.children;
|
|
4222
|
+
const out = [];
|
|
4223
|
+
for (let i = 0; i < children.length; i += 1) {
|
|
4224
|
+
const child = children[i];
|
|
4225
|
+
if (child instanceof HTMLElement) out.push(child);
|
|
4226
|
+
}
|
|
4227
|
+
return out;
|
|
4228
|
+
}
|
|
4229
|
+
return [...container.querySelectorAll(options.itemSelector)];
|
|
4230
|
+
};
|
|
4231
|
+
const focusableWhenDisabled = options.focusableWhenDisabled ?? false;
|
|
4232
|
+
const isSkipped = (item) => item.hidden || !focusableWhenDisabled && isDisabled(item);
|
|
4233
|
+
const enabledItems = (list) => {
|
|
4234
|
+
const out = [];
|
|
4235
|
+
for (let i = 0; i < list.length; i += 1) {
|
|
4236
|
+
const item = list[i];
|
|
4237
|
+
if (!isSkipped(item)) out.push(item);
|
|
4238
|
+
}
|
|
4239
|
+
return out;
|
|
4240
|
+
};
|
|
4241
|
+
const chosenItem = (list) => list.find((item) => item.matches(CHOSEN_SELECTOR));
|
|
4242
|
+
const setTabStop = (list, target) => {
|
|
4243
|
+
for (let i = 0; i < list.length; i += 1) {
|
|
4244
|
+
const item = list[i];
|
|
4245
|
+
writeTabIndex(item, item === target ? "0" : "-1");
|
|
4246
|
+
}
|
|
4247
|
+
};
|
|
4248
|
+
const clearTabStop = (list) => {
|
|
4249
|
+
for (let i = 0; i < list.length; i += 1) writeTabIndex(list[i], "-1");
|
|
4250
|
+
};
|
|
4251
|
+
const itemOf = (list, target) => {
|
|
4252
|
+
if (!(target instanceof Element)) return;
|
|
4253
|
+
for (let i = 0; i < list.length; i += 1) {
|
|
4254
|
+
const item = list[i];
|
|
4255
|
+
if (item === target || item.contains(target)) return item;
|
|
4256
|
+
}
|
|
4257
|
+
};
|
|
4258
|
+
const focusedItem = (list) => {
|
|
4259
|
+
const active = document.activeElement;
|
|
4260
|
+
if (active instanceof Element) {
|
|
4261
|
+
const hit = itemOf(list, active);
|
|
4262
|
+
if (hit !== void 0) return hit;
|
|
4263
|
+
if (lastFocused === void 0 && (active === container || container.contains(active))) return;
|
|
4264
|
+
}
|
|
4265
|
+
if (lastFocused !== void 0 && list.includes(lastFocused)) return lastFocused;
|
|
4266
|
+
if (tabStopItem !== void 0 && list.includes(tabStopItem)) return tabStopItem;
|
|
4267
|
+
};
|
|
4268
|
+
const focusItem = (list, item, event) => {
|
|
4269
|
+
lastFocused = item;
|
|
4270
|
+
tabStopItem = item;
|
|
4271
|
+
setTabStop(list, item);
|
|
4272
|
+
item.focus();
|
|
4273
|
+
options.onFocus?.(item, event);
|
|
4274
|
+
};
|
|
4275
|
+
const step = (delta, event, list) => {
|
|
4276
|
+
const active = document.activeElement;
|
|
4277
|
+
const activeEl = active instanceof Element ? active : null;
|
|
4278
|
+
const enabled = [];
|
|
4279
|
+
let activeHit;
|
|
4280
|
+
let activeEnabledIndex = -1;
|
|
4281
|
+
for (let i = 0; i < list.length; i += 1) {
|
|
4282
|
+
const item = list[i];
|
|
4283
|
+
if (activeEl !== null && activeHit === void 0 && (item === activeEl || item.contains(activeEl))) activeHit = item;
|
|
4284
|
+
if (!isSkipped(item)) {
|
|
4285
|
+
if (item === activeHit) activeEnabledIndex = enabled.length;
|
|
4286
|
+
enabled.push(item);
|
|
4287
|
+
}
|
|
4288
|
+
}
|
|
4289
|
+
if (enabled.length === 0) return;
|
|
4290
|
+
let current = activeHit;
|
|
4291
|
+
let index = activeEnabledIndex;
|
|
4292
|
+
if (current === void 0) {
|
|
4293
|
+
if (activeEl !== null && lastFocused === void 0 && (activeEl === container || container.contains(activeEl))) current = void 0;
|
|
4294
|
+
else if (lastFocused !== void 0 && list.includes(lastFocused)) current = lastFocused;
|
|
4295
|
+
else if (tabStopItem !== void 0 && list.includes(tabStopItem)) current = tabStopItem;
|
|
4296
|
+
if (current !== void 0) index = enabled.indexOf(current);
|
|
4297
|
+
}
|
|
4298
|
+
let target;
|
|
4299
|
+
if (index === -1) target = delta > 0 ? enabled[0] : enabled[enabled.length - 1];
|
|
4300
|
+
else {
|
|
4301
|
+
let next = index + delta;
|
|
4302
|
+
if (next < 0 || next >= enabled.length) {
|
|
4303
|
+
if (!loop) return;
|
|
4304
|
+
next = (next + enabled.length) % enabled.length;
|
|
4305
|
+
}
|
|
4306
|
+
target = enabled[next];
|
|
4307
|
+
if (target === current) return;
|
|
4308
|
+
}
|
|
4309
|
+
if (target) focusItem(list, target, event);
|
|
4310
|
+
};
|
|
4311
|
+
const edge = (to, event, list) => {
|
|
4312
|
+
let target;
|
|
4313
|
+
if (to === "first") for (let i = 0; i < list.length; i += 1) {
|
|
4314
|
+
const item = list[i];
|
|
4315
|
+
if (!isSkipped(item)) {
|
|
4316
|
+
target = item;
|
|
4317
|
+
break;
|
|
4318
|
+
}
|
|
4319
|
+
}
|
|
4320
|
+
else for (let i = list.length - 1; i >= 0; i -= 1) {
|
|
4321
|
+
const item = list[i];
|
|
4322
|
+
if (!isSkipped(item)) {
|
|
4323
|
+
target = item;
|
|
4324
|
+
break;
|
|
4325
|
+
}
|
|
4326
|
+
}
|
|
4327
|
+
if (!target || target === focusedItem(list)) return;
|
|
4328
|
+
focusItem(list, target, event);
|
|
4329
|
+
};
|
|
4330
|
+
const page = (delta, event, list) => {
|
|
4331
|
+
const enabled = enabledItems(list);
|
|
4332
|
+
const from = itemOf(enabled, event.target);
|
|
4333
|
+
const target = from === void 0 ? void 0 : enabled[enabled.indexOf(from) + delta];
|
|
4334
|
+
if (!target) return;
|
|
4335
|
+
event.preventDefault();
|
|
4336
|
+
focusItem(list, target, event);
|
|
4337
|
+
};
|
|
4338
|
+
const onKeyDown = (event) => {
|
|
4339
|
+
if (stopped || destroyed || event.defaultPrevented) return;
|
|
4340
|
+
let list;
|
|
4341
|
+
const getList = () => list ??= items();
|
|
4342
|
+
if (options.keeps !== void 0) {
|
|
4343
|
+
const current = itemOf(getList(), document.activeElement);
|
|
4344
|
+
if (current !== void 0 && options.keeps(current, event)) return;
|
|
4345
|
+
}
|
|
4346
|
+
const key = event.key;
|
|
4347
|
+
const isPage = (options.pageKeys ?? false) && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey && (key === "PageDown" || key === "PageUp");
|
|
4348
|
+
const isArrow = orientation === "vertical" ? key === "ArrowDown" || key === "ArrowUp" : key === "ArrowRight" || key === "ArrowLeft";
|
|
4349
|
+
if (!isPage && !isArrow && !(key === "Home" || key === "End")) return;
|
|
4350
|
+
if (isPage) {
|
|
4351
|
+
page(key === "PageDown" ? 1 : -1, event, getList());
|
|
4352
|
+
return;
|
|
4353
|
+
}
|
|
4354
|
+
if ((options.itemTargetOnly ?? false) && (event.shiftKey || !getList().includes(event.target))) return;
|
|
4355
|
+
if (isArrow) {
|
|
4356
|
+
const forward = orientation === "vertical" ? "ArrowDown" : isRtlElement(container) ? "ArrowLeft" : "ArrowRight";
|
|
4357
|
+
event.preventDefault();
|
|
4358
|
+
step(key === forward ? 1 : -1, event, getList());
|
|
4359
|
+
return;
|
|
4360
|
+
}
|
|
4361
|
+
event.preventDefault();
|
|
4362
|
+
edge(key === "Home" ? "first" : "last", event, getList());
|
|
4363
|
+
};
|
|
4364
|
+
const onFocusIn = (event) => {
|
|
4365
|
+
if (stopped || destroyed) return;
|
|
4366
|
+
const list = items();
|
|
4367
|
+
const item = itemOf(list, event.target);
|
|
4368
|
+
if (!item || isSkipped(item)) return;
|
|
4369
|
+
lastFocused = item;
|
|
4370
|
+
tabStopItem = item;
|
|
4371
|
+
setTabStop(list, item);
|
|
4372
|
+
};
|
|
4373
|
+
container.addEventListener("keydown", onKeyDown);
|
|
4374
|
+
container.addEventListener("focusin", onFocusIn);
|
|
4375
|
+
const refresh = () => {
|
|
4376
|
+
if (destroyed) return;
|
|
4377
|
+
const list = items();
|
|
4378
|
+
if (list.length === 0) {
|
|
4379
|
+
lastFocused = void 0;
|
|
4380
|
+
tabStopItem = void 0;
|
|
4381
|
+
return;
|
|
4382
|
+
}
|
|
4383
|
+
const enabled = enabledItems(list);
|
|
4384
|
+
let target;
|
|
4385
|
+
if (lastFocused && enabled.includes(lastFocused)) target = lastFocused;
|
|
4386
|
+
if (!target && options.chooses !== false) target = chosenItem(enabled);
|
|
4387
|
+
if (!target) target = enabled[0];
|
|
4388
|
+
if (!target) {
|
|
4389
|
+
clearTabStop(list);
|
|
4390
|
+
lastFocused = void 0;
|
|
4391
|
+
tabStopItem = void 0;
|
|
4392
|
+
return;
|
|
4393
|
+
}
|
|
4394
|
+
if (lastFocused !== void 0 && !enabled.includes(lastFocused)) lastFocused = void 0;
|
|
4395
|
+
tabStopItem = target;
|
|
4396
|
+
setTabStop(list, target);
|
|
4397
|
+
};
|
|
4398
|
+
refresh();
|
|
4399
|
+
const stop = () => {
|
|
4400
|
+
const current = items().find((item) => item.getAttribute("tabindex") === "0");
|
|
4401
|
+
if (!stopped) {
|
|
4402
|
+
stopped = true;
|
|
4403
|
+
container.removeEventListener("keydown", onKeyDown);
|
|
4404
|
+
container.removeEventListener("focusin", onFocusIn);
|
|
4405
|
+
}
|
|
4406
|
+
return current;
|
|
4407
|
+
};
|
|
4408
|
+
return {
|
|
4409
|
+
stop,
|
|
4410
|
+
focus(index) {
|
|
4411
|
+
if (destroyed) return;
|
|
4412
|
+
const list = items();
|
|
4413
|
+
const item = list[index];
|
|
4414
|
+
if (!item || isSkipped(item)) return;
|
|
4415
|
+
lastFocused = item;
|
|
4416
|
+
tabStopItem = item;
|
|
4417
|
+
setTabStop(list, item);
|
|
4418
|
+
item.focus();
|
|
4419
|
+
},
|
|
4420
|
+
refresh,
|
|
4421
|
+
destroy() {
|
|
4422
|
+
if (destroyed) return;
|
|
4423
|
+
stop();
|
|
4424
|
+
destroyed = true;
|
|
4425
|
+
for (const item of items()) item.removeAttribute("tabindex");
|
|
4426
|
+
lastFocused = void 0;
|
|
4427
|
+
tabStopItem = void 0;
|
|
4428
|
+
}
|
|
4429
|
+
};
|
|
4430
|
+
}
|
|
4431
|
+
// src/timeline.ts
|
|
4432
|
+
const ITEM_SELECTOR = "[data-slot=\"timeline-item\"]";
|
|
4433
|
+
function flag(item, name, on) {
|
|
4434
|
+
if (on) {
|
|
4435
|
+
if (!item.hasAttribute(name)) item.setAttribute(name, "");
|
|
4436
|
+
return;
|
|
4437
|
+
}
|
|
4438
|
+
item.removeAttribute(name);
|
|
4439
|
+
}
|
|
4440
|
+
function holdsItem(node) {
|
|
4441
|
+
return node instanceof Element && (node.matches(ITEM_SELECTOR) || node.querySelector(ITEM_SELECTOR) !== null);
|
|
4442
|
+
}
|
|
4443
|
+
/** Keeps the event marks inside `root` true to the page's own order. */
|
|
4444
|
+
function createTimelinePositions(root, options = {}) {
|
|
4445
|
+
let destroyed = false;
|
|
4446
|
+
let queued = false;
|
|
4447
|
+
const items = () => [...root.querySelectorAll(ITEM_SELECTOR)];
|
|
4448
|
+
const refresh = () => {
|
|
4449
|
+
if (destroyed) return;
|
|
4450
|
+
const list = items();
|
|
4451
|
+
const feed = options.feed?.() ?? false;
|
|
4452
|
+
root.style.setProperty("--timeline-count", String(list.length));
|
|
4453
|
+
list.forEach((item, index) => {
|
|
4454
|
+
item.style.setProperty("--timeline-index", String(index));
|
|
4455
|
+
flag(item, "data-first", index === 0);
|
|
4456
|
+
flag(item, "data-last", index === list.length - 1);
|
|
4457
|
+
if (feed) {
|
|
4458
|
+
item.setAttribute("aria-posinset", String(index + 1));
|
|
4459
|
+
item.setAttribute("aria-setsize", String(list.length));
|
|
4460
|
+
return;
|
|
4461
|
+
}
|
|
4462
|
+
item.removeAttribute("aria-posinset");
|
|
4463
|
+
item.removeAttribute("aria-setsize");
|
|
4464
|
+
});
|
|
4465
|
+
};
|
|
4466
|
+
refresh();
|
|
4467
|
+
const observer = typeof MutationObserver === "undefined" ? void 0 : new MutationObserver((records) => {
|
|
4468
|
+
if (!records.some((record) => [...record.addedNodes].some(holdsItem)) || queued) return;
|
|
4469
|
+
queued = true;
|
|
4470
|
+
queueMicrotask(() => {
|
|
4471
|
+
queued = false;
|
|
4472
|
+
refresh();
|
|
4473
|
+
});
|
|
4474
|
+
});
|
|
4475
|
+
observer?.observe(root, {
|
|
4476
|
+
childList: true,
|
|
4477
|
+
subtree: true
|
|
4478
|
+
});
|
|
4479
|
+
return {
|
|
4480
|
+
refresh,
|
|
4481
|
+
destroy() {
|
|
4482
|
+
if (destroyed) return;
|
|
4483
|
+
destroyed = true;
|
|
4484
|
+
observer?.disconnect();
|
|
4485
|
+
root.style.removeProperty("--timeline-count");
|
|
4486
|
+
for (const item of items()) {
|
|
4487
|
+
item.style.removeProperty("--timeline-index");
|
|
4488
|
+
item.removeAttribute("data-first");
|
|
4489
|
+
item.removeAttribute("data-last");
|
|
4490
|
+
item.removeAttribute("aria-posinset");
|
|
4491
|
+
item.removeAttribute("aria-setsize");
|
|
4492
|
+
}
|
|
4493
|
+
}
|
|
4494
|
+
};
|
|
4495
|
+
}
|
|
4496
|
+
// src/waterfall-chart-roving.ts
|
|
4497
|
+
/** Creates the DOM half of the waterfall chart's roving-focus contract. */
|
|
4498
|
+
function createWaterfallChartRoving() {
|
|
4499
|
+
let root;
|
|
4500
|
+
let engine;
|
|
4501
|
+
const rowElements = () => root === void 0 ? [] : [...root.querySelectorAll("[data-slot=\"waterfall-row\"]")];
|
|
4502
|
+
const rowElement = (id) => rowElements().find((element) => element.getAttribute("data-id") === id);
|
|
4503
|
+
const sync = () => {
|
|
4504
|
+
if (root === void 0) return;
|
|
4505
|
+
if (engine === void 0) {
|
|
4506
|
+
engine = createRovingFocus(root, {
|
|
4507
|
+
orientation: "vertical",
|
|
4508
|
+
itemSelector: "[data-slot=\"waterfall-row\"]"
|
|
4509
|
+
});
|
|
4510
|
+
return;
|
|
4511
|
+
}
|
|
4512
|
+
engine.refresh();
|
|
4513
|
+
};
|
|
4514
|
+
return {
|
|
4515
|
+
attach(element) {
|
|
4516
|
+
const next = element instanceof HTMLElement ? element : void 0;
|
|
4517
|
+
if (next === root) return;
|
|
4518
|
+
if (next === void 0) {
|
|
4519
|
+
engine?.destroy();
|
|
4520
|
+
engine = void 0;
|
|
4521
|
+
root = void 0;
|
|
4522
|
+
return;
|
|
4523
|
+
}
|
|
4524
|
+
root = next;
|
|
4525
|
+
sync();
|
|
4526
|
+
},
|
|
4527
|
+
sync,
|
|
4528
|
+
focus(id) {
|
|
4529
|
+
rowElement(id)?.focus();
|
|
4530
|
+
},
|
|
4531
|
+
focusedRowId() {
|
|
4532
|
+
const active = document.activeElement;
|
|
4533
|
+
if (root === void 0 || !(active instanceof Element) || !root.contains(active)) return;
|
|
4534
|
+
return active.closest("[data-slot=\"waterfall-row\"]")?.getAttribute("data-id") ?? void 0;
|
|
4535
|
+
},
|
|
4536
|
+
destroy() {
|
|
4537
|
+
engine?.destroy();
|
|
4538
|
+
engine = void 0;
|
|
4539
|
+
root = void 0;
|
|
4540
|
+
}
|
|
4541
|
+
};
|
|
4542
|
+
}
|
|
4543
|
+
// src/event-target.ts
|
|
4544
|
+
/** Whether one input's target is or sits inside an element matching selector. */
|
|
4545
|
+
function inputTargetWithin(input, selector) {
|
|
4546
|
+
const target = input?.target;
|
|
4547
|
+
return typeof target?.closest === "function" && target.closest(selector) !== null;
|
|
4548
|
+
}
|
|
4549
|
+
const TABLE_CONTROL_SELECTOR = "button, a[href], input, select, textarea, [role='button'], [role='checkbox'], [role='link']";
|
|
4550
|
+
/** Whether a table press belongs to the row rather than a control inside it. */
|
|
4551
|
+
function tablePressBelongsToRow(input) {
|
|
4552
|
+
return !inputTargetWithin(input, TABLE_CONTROL_SELECTOR);
|
|
4553
|
+
}
|
|
4554
|
+
/** Whether a tree-grid press belongs to the row rather than its twisty. */
|
|
4555
|
+
function treeGridPressBelongsToRow(input) {
|
|
4556
|
+
return !inputTargetWithin(input, "[data-toggle]");
|
|
4557
|
+
}
|
|
4558
|
+
/** Whether a pointer/focus movement remains inside the target's nearest region. */
|
|
4559
|
+
function inputMovementStaysWithin(input, selector) {
|
|
4560
|
+
const target = input?.target;
|
|
4561
|
+
if (!(target instanceof Element)) return false;
|
|
4562
|
+
const region = target.closest(selector);
|
|
4563
|
+
if (region === null) return false;
|
|
4564
|
+
const next = input?.relatedTarget;
|
|
4565
|
+
return next instanceof Node && region.contains(next);
|
|
4566
|
+
}
|
|
4567
|
+
// src/index.ts
|
|
4568
|
+
/** How a change came about, for a callback's source field. */
|
|
4569
|
+
function resolveSource(event) {
|
|
4570
|
+
if (event === void 0) return "programmatic";
|
|
4571
|
+
const type = event.type;
|
|
4572
|
+
if (type.startsWith("key")) return "keyboard";
|
|
4573
|
+
if (type.startsWith("touch")) return "touch";
|
|
4574
|
+
if ("pointerType" in event) {
|
|
4575
|
+
const pointer = event.pointerType;
|
|
4576
|
+
if (pointer === "pen") return "pen";
|
|
4577
|
+
if (pointer === "touch") return "touch";
|
|
4578
|
+
return "mouse";
|
|
4579
|
+
}
|
|
4580
|
+
if (type.startsWith("pointer") || type === "click" || type === "dblclick" || type.startsWith("mouse")) return "mouse";
|
|
4581
|
+
return "virtual";
|
|
4582
|
+
}
|
|
4583
|
+
export { BREAKPOINTS, ENDING_STYLE_ATTRIBUTE, IDLE_AFTER, MEDIA_QUERIES, PRESENCE_TIMEOUT, RESIZE_TOLERANCE, SCROLL_TOLERANCE, STARTING_STYLE_ATTRIBUTE, activeElement, announce, applyInertRegions, applyResizeBatch, beginEnter, bindDismissals, bindFocusFlow, bindKeyboardInset, bindLayerPresence, bindMediaQueries, bindObservation, bindPersistentLayer, bindViewportMetrics, breakpointOf, canReadClipboard, carouselCaptureRegion, carouselReleaseRegion, carouselViewportExtent, clipboardFailure, clipboardStrategy, coveredBottom, createClipboardPort, createRovingFocus, createScrollAreaChannel, createTimelinePositions, createWaterfallChannel, createWaterfallChartRoving, crossedVisibility, directionOfElement, directionOfKeyEvent, existingLayerRoot, fieldKeepsCaretKey, focusFirst, focusPull, focusables, hoverCapability, idleThreshold, imageCropperAnchor, imageCropperCaptureViewport, imageCropperReleaseViewport, imageCropperViewportPoint, imageCropperViewportSize, inPersistentLayer, inertKeys, inputMovementStaysWithin, inputProfile, inputTargetWithin, isFocused, isTabbable, isTopLayer, isTouchDevice, isZoomed, keycapOrder, keycaps, layerRoot, layers, lockPageScroll, lockScroll, mediaChanges, mediaSnapshot, nextTabbable, observationPlan, onScreen, placeFieldCaret, platformOf, pointerActivity, pointerIdle, pointerState, portalOwnerOf, prefersReducedMotion, prepareAnnouncements, promoteTopLayer, pushLayer, ratingStarFractionForPress, ratingStarIndexForInput, readMediaSnapshot, readPlatform, readViewportMetrics, refreshResizableCursor, registerPortal, registerResizableHandle, releaseResizableHandle, rememberFocus, resizableCursor, resizableSpace, resolveSource, restoreFocus, sameViewport, scrollEdges, scrollLockEdits, setAnnouncer, setInertOuter, setLayerRoot, startDragSession, startPadSession, supportsTopLayer, tabOrder, tablePressBelongsToRow, trackFocusHeld, trackPointer, trackResizableDrag, trapFocus, treeGridPressBelongsToRow, viewportMetrics, visibilityPolicy, visibilityState, visibleRect, whenSettled };
|