@y14e/portal 1.2.19 → 1.2.20
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/README.md +4 -4
- package/dist/index.bundle.cjs +601 -0
- package/dist/index.bundle.js +599 -0
- package/dist/index.cjs +20 -410
- package/dist/index.d.cts +1 -5
- package/dist/index.d.ts +1 -5
- package/dist/index.js +6 -389
- package/package.json +5 -3
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
// node_modules/@y14e/attributes-utils/dist/index.js
|
|
2
|
+
var snapshots = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
function restoreAttributes(elements) {
|
|
4
|
+
for (const element of elements) {
|
|
5
|
+
const snapshot = snapshots.get(element);
|
|
6
|
+
if (!snapshot) {
|
|
7
|
+
continue;
|
|
8
|
+
}
|
|
9
|
+
for (const [attribute, value] of snapshot.entries()) {
|
|
10
|
+
value === null ? element.removeAttribute(attribute) : element.setAttribute(attribute, value);
|
|
11
|
+
}
|
|
12
|
+
snapshots.delete(element);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function saveAttributes(elements, attributes) {
|
|
16
|
+
elements.forEach((element) => {
|
|
17
|
+
let snapshot = snapshots.get(element);
|
|
18
|
+
if (!snapshot) {
|
|
19
|
+
snapshot = /* @__PURE__ */ new Map();
|
|
20
|
+
snapshots.set(element, snapshot);
|
|
21
|
+
}
|
|
22
|
+
attributes.forEach((attribute) => {
|
|
23
|
+
snapshot.set(attribute, element.getAttribute(attribute));
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// node_modules/power-focusable/dist/index.js
|
|
29
|
+
var FOCUSABLE_SELECTOR = `:is(a[href], area[href], button, embed, iframe, input:not([type="hidden" i]), object, select, details > summary:first-of-type, textarea, [contenteditable]:not([contenteditable="false" i]), [controls], [tabindex]):not(:disabled, [hidden], [inert], [tabindex="-1"])`;
|
|
30
|
+
function getFocusables(container = document.body, options = {}) {
|
|
31
|
+
if (!(container instanceof Element)) {
|
|
32
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
33
|
+
container = document.body;
|
|
34
|
+
}
|
|
35
|
+
let {
|
|
36
|
+
composed = false,
|
|
37
|
+
filter,
|
|
38
|
+
include,
|
|
39
|
+
skipNegativeTabIndexCheck = false,
|
|
40
|
+
skipVisibilityCheck = false
|
|
41
|
+
} = options;
|
|
42
|
+
if (typeof composed !== "boolean") {
|
|
43
|
+
console.warn("Invalid composed option. Fallback: false.");
|
|
44
|
+
composed = false;
|
|
45
|
+
}
|
|
46
|
+
if (typeof filter !== "undefined" && typeof filter !== "function") {
|
|
47
|
+
console.warn(
|
|
48
|
+
"Invalid filter function. Fallback: no filter function (undefined)."
|
|
49
|
+
);
|
|
50
|
+
filter = void 0;
|
|
51
|
+
}
|
|
52
|
+
if (typeof include !== "undefined" && typeof include !== "function") {
|
|
53
|
+
console.warn(
|
|
54
|
+
"Invalid include function. Fallback: no include function (undefined)."
|
|
55
|
+
);
|
|
56
|
+
include = void 0;
|
|
57
|
+
}
|
|
58
|
+
if (typeof skipNegativeTabIndexCheck !== "boolean") {
|
|
59
|
+
console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
|
|
60
|
+
skipNegativeTabIndexCheck = false;
|
|
61
|
+
}
|
|
62
|
+
if (typeof skipVisibilityCheck !== "boolean") {
|
|
63
|
+
console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
|
|
64
|
+
skipVisibilityCheck = false;
|
|
65
|
+
}
|
|
66
|
+
const elements = [];
|
|
67
|
+
if (composed || include) {
|
|
68
|
+
let traverse2 = function(node) {
|
|
69
|
+
if (!(node instanceof Element)) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (isFocusable(node, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(node)) {
|
|
73
|
+
elements[elements.length] = node;
|
|
74
|
+
}
|
|
75
|
+
const children = getComposedChildren(node);
|
|
76
|
+
for (let i = 0, l = children.length; i < l; i++) {
|
|
77
|
+
const child = children[i];
|
|
78
|
+
child && traverse2(child);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
traverse2(container);
|
|
82
|
+
} else {
|
|
83
|
+
const candidates = container.querySelectorAll(FOCUSABLE_SELECTOR);
|
|
84
|
+
for (let i = 0, l = candidates.length; i < l; i++) {
|
|
85
|
+
const candidate = candidates[i];
|
|
86
|
+
if (candidate && isFocusable(candidate, {
|
|
87
|
+
skipNegativeTabIndexCheck,
|
|
88
|
+
skipVisibilityCheck
|
|
89
|
+
})) {
|
|
90
|
+
elements[elements.length] = candidate;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const unfiltered = normalizeRadioGroup(sortByTabIndex(elements));
|
|
95
|
+
return filter ? unfiltered.filter(filter) : unfiltered;
|
|
96
|
+
}
|
|
97
|
+
function getNextFocusable(container = document.body, options = {}) {
|
|
98
|
+
return getRelativeFocusable(container, 1, options);
|
|
99
|
+
}
|
|
100
|
+
function getPreviousFocusable(container = document.body, options = {}) {
|
|
101
|
+
return getRelativeFocusable(container, -1, options);
|
|
102
|
+
}
|
|
103
|
+
function isFocusable(element, options = {}) {
|
|
104
|
+
if (!(element instanceof Element)) {
|
|
105
|
+
console.warn("Invalid element");
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
let { skipNegativeTabIndexCheck = false, skipVisibilityCheck = false } = options;
|
|
109
|
+
if (typeof skipNegativeTabIndexCheck !== "boolean") {
|
|
110
|
+
console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
|
|
111
|
+
skipNegativeTabIndexCheck = false;
|
|
112
|
+
}
|
|
113
|
+
if (typeof skipVisibilityCheck !== "boolean") {
|
|
114
|
+
console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
|
|
115
|
+
skipVisibilityCheck = false;
|
|
116
|
+
}
|
|
117
|
+
if (element.hasAttribute("hidden") || isInert(element)) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
if (!skipNegativeTabIndexCheck && getTabIndex(element) < 0) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
if (!element.matches(
|
|
124
|
+
skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR.replace(/(,\s*)?\[tabindex="-1"\]/g, "") : FOCUSABLE_SELECTOR
|
|
125
|
+
)) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
if (isDisabledDeep(element)) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
if (!skipVisibilityCheck && !element.checkVisibility({
|
|
132
|
+
contentVisibilityAuto: true,
|
|
133
|
+
opacityProperty: true,
|
|
134
|
+
visibilityProperty: true
|
|
135
|
+
})) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
function getRelativeFocusable(container, offset, options) {
|
|
141
|
+
if (!(container instanceof Element)) {
|
|
142
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
143
|
+
container = document.body;
|
|
144
|
+
}
|
|
145
|
+
let {
|
|
146
|
+
anchor = getActiveElement(),
|
|
147
|
+
composed = false,
|
|
148
|
+
filter,
|
|
149
|
+
include,
|
|
150
|
+
skipNegativeTabIndexCheck = false,
|
|
151
|
+
skipVisibilityCheck = false,
|
|
152
|
+
wrap = false
|
|
153
|
+
} = options;
|
|
154
|
+
if (!(anchor instanceof Element)) {
|
|
155
|
+
const active = getActiveElement();
|
|
156
|
+
if (active instanceof Element) {
|
|
157
|
+
console.warn("Invalid anchor element. Fallback: active element.");
|
|
158
|
+
anchor = active;
|
|
159
|
+
} else {
|
|
160
|
+
console.warn("Invalid anchor element");
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (!containsComposed(container, anchor)) {
|
|
165
|
+
console.warn("Anchor (active) element not within container");
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
if (typeof composed !== "boolean") {
|
|
169
|
+
console.warn("Invalid composed option. Fallback: false.");
|
|
170
|
+
composed = false;
|
|
171
|
+
}
|
|
172
|
+
if (typeof filter !== "undefined" && typeof filter !== "function") {
|
|
173
|
+
console.warn(
|
|
174
|
+
"Invalid filter function. Fallback: no filter function (undefined)."
|
|
175
|
+
);
|
|
176
|
+
filter = void 0;
|
|
177
|
+
}
|
|
178
|
+
if (typeof include !== "undefined" && typeof include !== "function") {
|
|
179
|
+
console.warn(
|
|
180
|
+
"Invalid include function. Fallback: no include function (undefined)."
|
|
181
|
+
);
|
|
182
|
+
include = void 0;
|
|
183
|
+
}
|
|
184
|
+
if (typeof skipNegativeTabIndexCheck !== "boolean") {
|
|
185
|
+
console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
|
|
186
|
+
skipNegativeTabIndexCheck = false;
|
|
187
|
+
}
|
|
188
|
+
if (typeof skipVisibilityCheck !== "boolean") {
|
|
189
|
+
console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
|
|
190
|
+
skipVisibilityCheck = false;
|
|
191
|
+
}
|
|
192
|
+
if (typeof wrap !== "boolean") {
|
|
193
|
+
console.warn("Invalid wrap option. Fallback: false.");
|
|
194
|
+
wrap = false;
|
|
195
|
+
}
|
|
196
|
+
const settings = { composed, skipNegativeTabIndexCheck, skipVisibilityCheck };
|
|
197
|
+
if (filter !== void 0) {
|
|
198
|
+
Object.assign(settings, { filter });
|
|
199
|
+
}
|
|
200
|
+
if (include !== void 0) {
|
|
201
|
+
Object.assign(settings, { include });
|
|
202
|
+
}
|
|
203
|
+
const focusables = getFocusables(container, settings);
|
|
204
|
+
const { length } = focusables;
|
|
205
|
+
if (!length) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
const currentIndex = focusables.indexOf(anchor);
|
|
209
|
+
if (currentIndex === -1) {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
const offsetIndex = currentIndex + offset;
|
|
213
|
+
if ((offsetIndex < 0 || offsetIndex >= length) && !wrap) {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
return focusables[(offsetIndex + length) % length] ?? null;
|
|
217
|
+
}
|
|
218
|
+
function isDisabledDeep(element) {
|
|
219
|
+
let current = element;
|
|
220
|
+
while (current) {
|
|
221
|
+
if (current instanceof ShadowRoot) {
|
|
222
|
+
if (current.mode !== "open") {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
current = current.host;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (!(current instanceof Element)) {
|
|
229
|
+
current = current.parentNode;
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (current === element && isFormControl(current) && isDisabled(current)) {
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
if (isInert(current)) {
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
|
|
239
|
+
if (!current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
current = current.parentNode;
|
|
244
|
+
}
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
function normalizeRadioGroup(elements) {
|
|
248
|
+
let map = null;
|
|
249
|
+
for (let i = 0, l = elements.length; i < l; i++) {
|
|
250
|
+
const element = elements[i];
|
|
251
|
+
if (!(element instanceof HTMLInputElement)) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (!isUngroupedRadio(element)) {
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (!map) {
|
|
258
|
+
map = /* @__PURE__ */ new Map();
|
|
259
|
+
}
|
|
260
|
+
const key = `${element.form?.id ?? "no-form"}::${element.name}`;
|
|
261
|
+
const group = map.get(key) ?? map.set(key, []).get(key);
|
|
262
|
+
if (group) {
|
|
263
|
+
group[group.length] = element;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (!map) {
|
|
267
|
+
return elements;
|
|
268
|
+
}
|
|
269
|
+
const placeholder = /* @__PURE__ */ new Set();
|
|
270
|
+
for (const group of map.values()) {
|
|
271
|
+
placeholder.add(group.find((radio) => radio.checked) ?? group[0]);
|
|
272
|
+
}
|
|
273
|
+
return elements.filter(
|
|
274
|
+
(element) => isUngroupedRadio(element) ? placeholder.has(element) : true
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
function sortByTabIndex(elements) {
|
|
278
|
+
const ordered = [];
|
|
279
|
+
const natural = [];
|
|
280
|
+
for (let i = 0, l = elements.length; i < l; i++) {
|
|
281
|
+
const element = elements[i];
|
|
282
|
+
if (element) {
|
|
283
|
+
const target = getTabIndex(element) > 0 ? ordered : natural;
|
|
284
|
+
target[target.length] = element;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b));
|
|
288
|
+
let count = 0;
|
|
289
|
+
const sorted = new Array(ordered.length + natural.length);
|
|
290
|
+
for (let i = 0, l = ordered.length; i < l; i++) {
|
|
291
|
+
sorted[count++] = ordered[i];
|
|
292
|
+
}
|
|
293
|
+
for (let i = 0, l = natural.length; i < l; i++) {
|
|
294
|
+
sorted[count++] = natural[i];
|
|
295
|
+
}
|
|
296
|
+
return sorted;
|
|
297
|
+
}
|
|
298
|
+
function containsComposed(container, element) {
|
|
299
|
+
let current = element;
|
|
300
|
+
while (current) {
|
|
301
|
+
if (current === container) {
|
|
302
|
+
return true;
|
|
303
|
+
} else {
|
|
304
|
+
current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
function getComposedChildren(node) {
|
|
310
|
+
if (node instanceof ShadowRoot) {
|
|
311
|
+
return getChildren(node);
|
|
312
|
+
}
|
|
313
|
+
if (!(node instanceof Element)) {
|
|
314
|
+
return [];
|
|
315
|
+
}
|
|
316
|
+
if (node instanceof HTMLSlotElement) {
|
|
317
|
+
const assigned = node.assignedElements({ flatten: true });
|
|
318
|
+
if (assigned.length) {
|
|
319
|
+
return assigned;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
|
|
323
|
+
return getChildren(node.shadowRoot);
|
|
324
|
+
}
|
|
325
|
+
return getChildren(node);
|
|
326
|
+
}
|
|
327
|
+
function focusElement(element) {
|
|
328
|
+
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
329
|
+
}
|
|
330
|
+
function getActiveElement() {
|
|
331
|
+
let current = document.activeElement;
|
|
332
|
+
while (current?.shadowRoot?.activeElement) {
|
|
333
|
+
current = current.shadowRoot.activeElement;
|
|
334
|
+
}
|
|
335
|
+
return current;
|
|
336
|
+
}
|
|
337
|
+
function getChildren(node) {
|
|
338
|
+
const elements = [];
|
|
339
|
+
for (let child = node.firstElementChild; child; child = child.nextElementSibling) {
|
|
340
|
+
elements[elements.length] = child;
|
|
341
|
+
}
|
|
342
|
+
return elements;
|
|
343
|
+
}
|
|
344
|
+
function getTabIndex(element) {
|
|
345
|
+
return "tabIndex" in element ? Number(element.tabIndex) : 0;
|
|
346
|
+
}
|
|
347
|
+
function isDisabled(element) {
|
|
348
|
+
return "disabled" in element && !!element.disabled;
|
|
349
|
+
}
|
|
350
|
+
function isFormControl(element) {
|
|
351
|
+
const name = element.tagName;
|
|
352
|
+
return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
|
|
353
|
+
}
|
|
354
|
+
function isInert(element) {
|
|
355
|
+
return "inert" in element && !!element.inert;
|
|
356
|
+
}
|
|
357
|
+
function isUngroupedRadio(element) {
|
|
358
|
+
return element instanceof HTMLInputElement && element.type === "radio" && !!element.name;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// src/index.ts
|
|
362
|
+
var VISUALLY_HIDDEN_CSS = `border: 0; clip: rect(0, 0, 0, 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; user-select: none; white-space: nowrap; width: 1px;`;
|
|
363
|
+
function createPortal(host, container = document.body) {
|
|
364
|
+
if (!(host instanceof Element)) {
|
|
365
|
+
console.warn("Invalid host element");
|
|
366
|
+
return () => {
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
if (host.hasAttribute("data-portaled")) {
|
|
370
|
+
console.warn("Already portaled");
|
|
371
|
+
return () => {
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
if (containsComposed2(host, container)) {
|
|
375
|
+
console.warn("Host element cannot contain the container element");
|
|
376
|
+
return () => {
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
const portal = new Portal(host, container);
|
|
380
|
+
return () => portal.destroy();
|
|
381
|
+
}
|
|
382
|
+
var Portal = class {
|
|
383
|
+
#host;
|
|
384
|
+
#container;
|
|
385
|
+
#entranceSentinel;
|
|
386
|
+
#exitSentinel;
|
|
387
|
+
#focusables = /* @__PURE__ */ new Set();
|
|
388
|
+
#controller = null;
|
|
389
|
+
#timer;
|
|
390
|
+
#isDestroyed = false;
|
|
391
|
+
constructor(host, container) {
|
|
392
|
+
this.#host = host;
|
|
393
|
+
if (!(container instanceof Element)) {
|
|
394
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
395
|
+
container = document.body;
|
|
396
|
+
}
|
|
397
|
+
this.#container = container;
|
|
398
|
+
this.#entranceSentinel = this.#createSentinel();
|
|
399
|
+
this.#exitSentinel = this.#createSentinel();
|
|
400
|
+
this.#initialize();
|
|
401
|
+
}
|
|
402
|
+
destroy() {
|
|
403
|
+
if (this.#isDestroyed) {
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
this.#isDestroyed = true;
|
|
407
|
+
this.#controller?.abort();
|
|
408
|
+
this.#controller = null;
|
|
409
|
+
if (this.#timer !== void 0) {
|
|
410
|
+
cancelAnimationFrame(this.#timer);
|
|
411
|
+
this.#timer = void 0;
|
|
412
|
+
}
|
|
413
|
+
restoreAttributes([...this.#focusables]);
|
|
414
|
+
this.#focusables.clear();
|
|
415
|
+
this.#exitSentinel.after(this.#host);
|
|
416
|
+
this.#entranceSentinel.remove();
|
|
417
|
+
this.#exitSentinel.remove();
|
|
418
|
+
this.#host.removeAttribute("data-portaled");
|
|
419
|
+
}
|
|
420
|
+
#initialize() {
|
|
421
|
+
this.#host.before(this.#entranceSentinel);
|
|
422
|
+
this.#entranceSentinel.after(this.#exitSentinel);
|
|
423
|
+
this.#container.append(this.#host);
|
|
424
|
+
this.#update();
|
|
425
|
+
this.#controller = new AbortController();
|
|
426
|
+
const { signal } = this.#controller;
|
|
427
|
+
document.addEventListener("focusin", this.#onFocusIn, {
|
|
428
|
+
capture: true,
|
|
429
|
+
signal
|
|
430
|
+
});
|
|
431
|
+
document.addEventListener("keydown", this.#onKeyDown, {
|
|
432
|
+
capture: true,
|
|
433
|
+
signal
|
|
434
|
+
});
|
|
435
|
+
this.#host.setAttribute("data-portaled", "");
|
|
436
|
+
}
|
|
437
|
+
#onFocusIn = (event) => {
|
|
438
|
+
const current = event.target;
|
|
439
|
+
const before = event.relatedTarget;
|
|
440
|
+
if (!(before instanceof Element)) {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
if (current === this.#entranceSentinel) {
|
|
444
|
+
if (this.#host.contains(before)) {
|
|
445
|
+
this.#moveFocus("previous");
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
this.#update();
|
|
449
|
+
const first = [...this.#focusables][0];
|
|
450
|
+
if (first) {
|
|
451
|
+
focusElement(first);
|
|
452
|
+
} else {
|
|
453
|
+
const next = getNextFocusable(document.body, {
|
|
454
|
+
anchor: this.#exitSentinel,
|
|
455
|
+
composed: true
|
|
456
|
+
});
|
|
457
|
+
next && focusElement(next);
|
|
458
|
+
}
|
|
459
|
+
} else if (current === this.#exitSentinel) {
|
|
460
|
+
if (this.#host.contains(before)) {
|
|
461
|
+
this.#moveFocus("next");
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
this.#update();
|
|
465
|
+
const last = [...this.#focusables].at(-1);
|
|
466
|
+
if (last) {
|
|
467
|
+
focusElement(last);
|
|
468
|
+
} else {
|
|
469
|
+
const previous = getPreviousFocusable(document.body, {
|
|
470
|
+
anchor: this.#entranceSentinel,
|
|
471
|
+
composed: true
|
|
472
|
+
});
|
|
473
|
+
previous && focusElement(previous);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
#onKeyDown = (event) => {
|
|
478
|
+
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
479
|
+
if (key !== "Tab" || altKey || ctrlKey || metaKey) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
const active = getActiveElement();
|
|
483
|
+
if (!(active instanceof Element)) {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
if (!this.#host.contains(active)) {
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
this.#update();
|
|
490
|
+
const focusables = this.#getFocusables();
|
|
491
|
+
if (focusables.length) {
|
|
492
|
+
const index = focusables.indexOf(active);
|
|
493
|
+
if (index !== -1) {
|
|
494
|
+
event.preventDefault();
|
|
495
|
+
const focusable = focusables[index + (shiftKey ? -1 : 1)];
|
|
496
|
+
focusable ? focusElement(focusable) : this.#focusSentinel(shiftKey);
|
|
497
|
+
}
|
|
498
|
+
} else {
|
|
499
|
+
event.preventDefault();
|
|
500
|
+
this.#moveFocus(shiftKey ? "previous" : "next");
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
#update() {
|
|
504
|
+
const current = /* @__PURE__ */ new Set([
|
|
505
|
+
...this.#getFocusables(),
|
|
506
|
+
...getFocusables(this.#host, { composed: true })
|
|
507
|
+
]);
|
|
508
|
+
for (const focusable of this.#focusables) {
|
|
509
|
+
if (!current.has(focusable)) {
|
|
510
|
+
focusable.isConnected && restoreAttributes([focusable]);
|
|
511
|
+
this.#focusables.delete(focusable);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
for (const focusable of current) {
|
|
515
|
+
if (!this.#focusables.has(focusable)) {
|
|
516
|
+
this.#focusables.add(focusable);
|
|
517
|
+
saveAttributes([focusable], ["tabindex"]);
|
|
518
|
+
focusable.setAttribute("tabindex", "-1");
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
#createSentinel() {
|
|
523
|
+
const sentinel = document.createElement("span");
|
|
524
|
+
sentinel.setAttribute("aria-hidden", "true");
|
|
525
|
+
sentinel.setAttribute("data-portal-sentinel", "");
|
|
526
|
+
sentinel.setAttribute("tabindex", "0");
|
|
527
|
+
sentinel.style.cssText += VISUALLY_HIDDEN_CSS;
|
|
528
|
+
return sentinel;
|
|
529
|
+
}
|
|
530
|
+
#focusSentinel(isPrevious) {
|
|
531
|
+
this.#timer && cancelAnimationFrame(this.#timer);
|
|
532
|
+
this.#timer = requestAnimationFrame(
|
|
533
|
+
() => (isPrevious ? this.#entranceSentinel : this.#exitSentinel).focus()
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
#getFocusables() {
|
|
537
|
+
return getFocusables(this.#host, {
|
|
538
|
+
composed: true,
|
|
539
|
+
include: (element) => this.#focusables.has(element)
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
#moveFocus(direction) {
|
|
543
|
+
const options = {
|
|
544
|
+
anchor: direction === "previous" ? this.#entranceSentinel : this.#exitSentinel,
|
|
545
|
+
composed: true
|
|
546
|
+
};
|
|
547
|
+
const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
|
|
548
|
+
focusable && focusElement(focusable);
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
function containsComposed2(container, element) {
|
|
552
|
+
let current = element;
|
|
553
|
+
while (current) {
|
|
554
|
+
if (current === container) {
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
|
|
558
|
+
}
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Portal
|
|
563
|
+
* Lightweight DOM portal (teleport) utility with fully focus management.
|
|
564
|
+
* Designed for accessible dialogs, menus, overlays, popovers.
|
|
565
|
+
*
|
|
566
|
+
* @version 1.2.20
|
|
567
|
+
* @author Yusuke Kamiyamane
|
|
568
|
+
* @license MIT
|
|
569
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
570
|
+
* @see {@link https://github.com/y14e/portal}
|
|
571
|
+
*/
|
|
572
|
+
/*! Bundled license information:
|
|
573
|
+
|
|
574
|
+
@y14e/attributes-utils/dist/index.js:
|
|
575
|
+
(**
|
|
576
|
+
* Attributes Utils
|
|
577
|
+
*
|
|
578
|
+
* @version 1.1.2
|
|
579
|
+
* @author Yusuke Kamiyamane
|
|
580
|
+
* @license MIT
|
|
581
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
582
|
+
* @see {@link https://github.com/y14e/attributes-utils}
|
|
583
|
+
*)
|
|
584
|
+
|
|
585
|
+
power-focusable/dist/index.js:
|
|
586
|
+
(**
|
|
587
|
+
* Power Focusable
|
|
588
|
+
* High-precision focus management utility with full composed tree support.
|
|
589
|
+
* Handles complex focus rules including tabindex ordering, radio groups, inert.
|
|
590
|
+
*
|
|
591
|
+
* @version 4.3.3
|
|
592
|
+
* @author Yusuke Kamiyamane
|
|
593
|
+
* @license MIT
|
|
594
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
595
|
+
* @see {@link https://github.com/y14e/power-focusable}
|
|
596
|
+
*)
|
|
597
|
+
*/
|
|
598
|
+
|
|
599
|
+
export { createPortal };
|