@y14e/portal 1.4.4 → 1.4.6
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/dist/index.bundle.cjs +161 -183
- package/dist/index.bundle.js +161 -183
- package/dist/index.cjs +21 -14
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +21 -14
- package/package.json +3 -3
package/dist/index.bundle.cjs
CHANGED
|
@@ -29,87 +29,114 @@ function saveAttributes(element_or_elements, name_or_names) {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
// node_modules/power-focusable/dist/index.js
|
|
32
|
+
function containsComposed(container, element) {
|
|
33
|
+
let current = element;
|
|
34
|
+
while (current) {
|
|
35
|
+
if (current === container) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
function getComposedChildren(element) {
|
|
43
|
+
if (element instanceof ShadowRoot) {
|
|
44
|
+
return [...element.children];
|
|
45
|
+
}
|
|
46
|
+
if (!(element instanceof Element)) {
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
if (element instanceof HTMLSlotElement) {
|
|
50
|
+
const assigned = element.assignedElements({ flatten: true });
|
|
51
|
+
if (assigned.length) {
|
|
52
|
+
return assigned;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (element instanceof HTMLElement && element.shadowRoot?.mode === "open") {
|
|
56
|
+
return [...element.shadowRoot.children];
|
|
57
|
+
}
|
|
58
|
+
return [...element.children];
|
|
59
|
+
}
|
|
32
60
|
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"])';
|
|
33
61
|
var FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX = FOCUSABLE_SELECTOR.replace(
|
|
34
62
|
/(,\s*)?\[tabindex="-1"\]/g,
|
|
35
63
|
""
|
|
36
64
|
);
|
|
37
|
-
function
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
65
|
+
function focusElement(element) {
|
|
66
|
+
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
67
|
+
}
|
|
68
|
+
function getActiveElement() {
|
|
69
|
+
let current = document.activeElement;
|
|
70
|
+
while (current?.shadowRoot?.activeElement) {
|
|
71
|
+
current = current.shadowRoot.activeElement;
|
|
41
72
|
}
|
|
42
|
-
|
|
43
|
-
|
|
73
|
+
return current;
|
|
74
|
+
}
|
|
75
|
+
function getTabIndex(element) {
|
|
76
|
+
return "tabIndex" in element ? Number(element.tabIndex) : -1;
|
|
77
|
+
}
|
|
78
|
+
function isDisabled(element) {
|
|
79
|
+
return "disabled" in element && !!element.disabled;
|
|
80
|
+
}
|
|
81
|
+
function isFormControl(element) {
|
|
82
|
+
return ["BUTTON", "INPUT", "SELECT", "TEXTAREA"].includes(element.tagName);
|
|
83
|
+
}
|
|
84
|
+
function isInert(element) {
|
|
85
|
+
return "inert" in element && !!element.inert;
|
|
86
|
+
}
|
|
87
|
+
function isUngroupedRadio(input) {
|
|
88
|
+
return input.type === "radio" && !!input.name;
|
|
89
|
+
}
|
|
90
|
+
function resolveOptions(options) {
|
|
91
|
+
let {
|
|
92
|
+
anchor = getActiveElement(),
|
|
93
|
+
composed = false,
|
|
44
94
|
filter,
|
|
45
95
|
include,
|
|
46
|
-
skipNegativeTabIndexCheck,
|
|
47
|
-
skipVisibilityCheck
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (!
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
skipNegativeTabIndexCheck,
|
|
56
|
-
skipVisibilityCheck
|
|
57
|
-
}) && candidates.push(match);
|
|
96
|
+
skipNegativeTabIndexCheck = false,
|
|
97
|
+
skipVisibilityCheck = false,
|
|
98
|
+
wrap = false
|
|
99
|
+
} = options;
|
|
100
|
+
if (!(anchor instanceof Element)) {
|
|
101
|
+
const active = getActiveElement();
|
|
102
|
+
if (active instanceof Element) {
|
|
103
|
+
console.warn("Invalid anchor element. Fallback: active element.");
|
|
104
|
+
anchor = active;
|
|
58
105
|
}
|
|
59
|
-
} else {
|
|
60
|
-
let traverse2 = function(node) {
|
|
61
|
-
if (isFocusable(node, {
|
|
62
|
-
skipNegativeTabIndexCheck,
|
|
63
|
-
skipVisibilityCheck
|
|
64
|
-
}) || include?.(node)) {
|
|
65
|
-
candidates.push(node);
|
|
66
|
-
}
|
|
67
|
-
(!composed ? [...node.children] : getComposedChildren(node)).map(
|
|
68
|
-
traverse2
|
|
69
|
-
);
|
|
70
|
-
};
|
|
71
|
-
(!composed ? [...container.children] : getComposedChildren(container)).map(
|
|
72
|
-
traverse2
|
|
73
|
-
);
|
|
74
106
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
function getNextFocusable(container = document.body, options = {}) {
|
|
80
|
-
return getRelativeFocusable(container, 1, options);
|
|
81
|
-
}
|
|
82
|
-
function getPreviousFocusable(container = document.body, options = {}) {
|
|
83
|
-
return getRelativeFocusable(container, -1, options);
|
|
84
|
-
}
|
|
85
|
-
function isFocusable(element, options = {}) {
|
|
86
|
-
if (!(element instanceof Element)) {
|
|
87
|
-
console.warn("Invalid element");
|
|
88
|
-
return false;
|
|
107
|
+
if (typeof composed !== "boolean") {
|
|
108
|
+
console.warn("Invalid composed option. Fallback: false.");
|
|
109
|
+
composed = false;
|
|
89
110
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
111
|
+
if (filter !== void 0 && typeof filter !== "function") {
|
|
112
|
+
console.warn("Invalid filter function. Fallback: no filter function (undefined).");
|
|
113
|
+
filter = void 0;
|
|
93
114
|
}
|
|
94
|
-
if (
|
|
95
|
-
|
|
115
|
+
if (include !== void 0 && typeof include !== "function") {
|
|
116
|
+
console.warn("Invalid include function. Fallback: no include function (undefined).");
|
|
117
|
+
include = void 0;
|
|
96
118
|
}
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return false;
|
|
119
|
+
if (typeof skipNegativeTabIndexCheck !== "boolean") {
|
|
120
|
+
console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
|
|
121
|
+
skipNegativeTabIndexCheck = false;
|
|
101
122
|
}
|
|
102
|
-
if (
|
|
103
|
-
|
|
123
|
+
if (typeof skipVisibilityCheck !== "boolean") {
|
|
124
|
+
console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
|
|
125
|
+
skipVisibilityCheck = false;
|
|
104
126
|
}
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
visibilityProperty: true
|
|
109
|
-
})) {
|
|
110
|
-
return false;
|
|
127
|
+
if (typeof wrap !== "boolean") {
|
|
128
|
+
console.warn("Invalid wrap option. Fallback: false.");
|
|
129
|
+
wrap = false;
|
|
111
130
|
}
|
|
112
|
-
return
|
|
131
|
+
return {
|
|
132
|
+
anchor,
|
|
133
|
+
composed,
|
|
134
|
+
filter,
|
|
135
|
+
include,
|
|
136
|
+
skipNegativeTabIndexCheck,
|
|
137
|
+
skipVisibilityCheck,
|
|
138
|
+
wrap
|
|
139
|
+
};
|
|
113
140
|
}
|
|
114
141
|
function getRelativeFocusable(container, offset, options) {
|
|
115
142
|
if (!(container instanceof Element)) {
|
|
@@ -174,10 +201,8 @@ function isDisabledDeep(element) {
|
|
|
174
201
|
if (isInert(current)) {
|
|
175
202
|
return true;
|
|
176
203
|
}
|
|
177
|
-
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
|
|
178
|
-
|
|
179
|
-
return true;
|
|
180
|
-
}
|
|
204
|
+
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current) && !current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
|
|
205
|
+
return true;
|
|
181
206
|
}
|
|
182
207
|
current = current.parentNode;
|
|
183
208
|
}
|
|
@@ -186,10 +211,7 @@ function isDisabledDeep(element) {
|
|
|
186
211
|
function normalizeRadioGroup(elements) {
|
|
187
212
|
let map = null;
|
|
188
213
|
for (const element of elements) {
|
|
189
|
-
if (!(element instanceof HTMLInputElement)) {
|
|
190
|
-
continue;
|
|
191
|
-
}
|
|
192
|
-
if (!isUngroupedRadio(element)) {
|
|
214
|
+
if (!(element instanceof HTMLInputElement) || !isUngroupedRadio(element)) {
|
|
193
215
|
continue;
|
|
194
216
|
}
|
|
195
217
|
if (!map) {
|
|
@@ -234,117 +256,80 @@ function sortByTabIndex(elements) {
|
|
|
234
256
|
}
|
|
235
257
|
return ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b)).concat(natural);
|
|
236
258
|
}
|
|
237
|
-
function
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
return true;
|
|
242
|
-
}
|
|
243
|
-
current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
|
|
244
|
-
}
|
|
245
|
-
return false;
|
|
246
|
-
}
|
|
247
|
-
function getComposedChildren(node) {
|
|
248
|
-
if (node instanceof ShadowRoot) {
|
|
249
|
-
return [...node.children];
|
|
250
|
-
}
|
|
251
|
-
if (!(node instanceof Element)) {
|
|
252
|
-
return [];
|
|
259
|
+
function getFocusables(container = document.body, options = {}) {
|
|
260
|
+
if (!(container instanceof Element)) {
|
|
261
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
262
|
+
container = document.body;
|
|
253
263
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
264
|
+
const { composed, filter, include, skipNegativeTabIndexCheck, skipVisibilityCheck } = resolveOptions(options);
|
|
265
|
+
const candidates = [];
|
|
266
|
+
if (!composed && !include) {
|
|
267
|
+
for (const match of container.querySelectorAll(
|
|
268
|
+
!skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
|
|
269
|
+
)) {
|
|
270
|
+
match && isFocusable(match, { skipNegativeTabIndexCheck, skipVisibilityCheck }) && candidates.push(match);
|
|
258
271
|
}
|
|
272
|
+
} else {
|
|
273
|
+
let traverse2 = function(element) {
|
|
274
|
+
if (isFocusable(element, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(element)) {
|
|
275
|
+
candidates.push(element);
|
|
276
|
+
}
|
|
277
|
+
(!composed ? [...element.children] : getComposedChildren(element)).map(traverse2);
|
|
278
|
+
};
|
|
279
|
+
(!composed ? [...container.children] : getComposedChildren(container)).map(traverse2);
|
|
259
280
|
}
|
|
260
|
-
|
|
261
|
-
return [...node.shadowRoot.children];
|
|
262
|
-
}
|
|
263
|
-
return [...node.children];
|
|
264
|
-
}
|
|
265
|
-
function focusElement(element) {
|
|
266
|
-
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
267
|
-
}
|
|
268
|
-
function getActiveElement() {
|
|
269
|
-
let current = document.activeElement;
|
|
270
|
-
while (current?.shadowRoot?.activeElement) {
|
|
271
|
-
current = current.shadowRoot.activeElement;
|
|
272
|
-
}
|
|
273
|
-
return current;
|
|
274
|
-
}
|
|
275
|
-
function getTabIndex(element) {
|
|
276
|
-
return "tabIndex" in element ? Number(element.tabIndex) : 0;
|
|
277
|
-
}
|
|
278
|
-
function isDisabled(element) {
|
|
279
|
-
return "disabled" in element && !!element.disabled;
|
|
281
|
+
return normalizeRadioGroup(sortByTabIndex(!filter ? candidates : candidates.filter(filter)));
|
|
280
282
|
}
|
|
281
|
-
function
|
|
282
|
-
return
|
|
283
|
-
}
|
|
284
|
-
function isInert(element) {
|
|
285
|
-
return "inert" in element && !!element.inert;
|
|
283
|
+
function getNextFocusable(container = document.body, options = {}) {
|
|
284
|
+
return getRelativeFocusable(container, 1, options);
|
|
286
285
|
}
|
|
287
|
-
function
|
|
288
|
-
return
|
|
286
|
+
function getPreviousFocusable(container = document.body, options = {}) {
|
|
287
|
+
return getRelativeFocusable(container, -1, options);
|
|
289
288
|
}
|
|
290
|
-
function
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
filter,
|
|
295
|
-
include,
|
|
296
|
-
skipNegativeTabIndexCheck = false,
|
|
297
|
-
skipVisibilityCheck = false,
|
|
298
|
-
wrap = false
|
|
299
|
-
} = options;
|
|
300
|
-
if (!(anchor instanceof Element)) {
|
|
301
|
-
const active = getActiveElement();
|
|
302
|
-
if (active instanceof Element) {
|
|
303
|
-
console.warn("Invalid anchor element. Fallback: active element.");
|
|
304
|
-
anchor = active;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
if (typeof composed !== "boolean") {
|
|
308
|
-
console.warn("Invalid composed option. Fallback: false.");
|
|
309
|
-
composed = false;
|
|
289
|
+
function isFocusable(element, options = {}) {
|
|
290
|
+
if (!(element instanceof Element)) {
|
|
291
|
+
console.warn("Invalid element");
|
|
292
|
+
return false;
|
|
310
293
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
);
|
|
315
|
-
filter = void 0;
|
|
294
|
+
const { skipNegativeTabIndexCheck, skipVisibilityCheck } = resolveOptions(options);
|
|
295
|
+
if (element.hasAttribute("hidden") || isInert(element)) {
|
|
296
|
+
return false;
|
|
316
297
|
}
|
|
317
|
-
if (
|
|
318
|
-
|
|
319
|
-
"Invalid include function. Fallback: no include function (undefined)."
|
|
320
|
-
);
|
|
321
|
-
include = void 0;
|
|
298
|
+
if (!skipNegativeTabIndexCheck && getTabIndex(element) < 0) {
|
|
299
|
+
return false;
|
|
322
300
|
}
|
|
323
|
-
if (
|
|
324
|
-
|
|
325
|
-
|
|
301
|
+
if (!element.matches(
|
|
302
|
+
!skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
|
|
303
|
+
)) {
|
|
304
|
+
return false;
|
|
326
305
|
}
|
|
327
|
-
if (
|
|
328
|
-
|
|
329
|
-
skipVisibilityCheck = false;
|
|
306
|
+
if (isDisabledDeep(element)) {
|
|
307
|
+
return false;
|
|
330
308
|
}
|
|
331
|
-
if (
|
|
332
|
-
|
|
333
|
-
|
|
309
|
+
if (!skipVisibilityCheck && !element.checkVisibility({
|
|
310
|
+
contentVisibilityAuto: true,
|
|
311
|
+
opacityProperty: true,
|
|
312
|
+
visibilityProperty: true
|
|
313
|
+
})) {
|
|
314
|
+
return false;
|
|
334
315
|
}
|
|
335
|
-
return
|
|
336
|
-
anchor,
|
|
337
|
-
composed,
|
|
338
|
-
filter,
|
|
339
|
-
include,
|
|
340
|
-
skipNegativeTabIndexCheck,
|
|
341
|
-
skipVisibilityCheck,
|
|
342
|
-
wrap
|
|
343
|
-
};
|
|
316
|
+
return true;
|
|
344
317
|
}
|
|
345
318
|
|
|
346
|
-
// src/
|
|
319
|
+
// src/constants.ts
|
|
347
320
|
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;";
|
|
321
|
+
|
|
322
|
+
// src/options.ts
|
|
323
|
+
function resolveOptions2(options) {
|
|
324
|
+
let { noInlineStyle = false } = options;
|
|
325
|
+
if (typeof noInlineStyle !== "boolean") {
|
|
326
|
+
console.warn("Invalid noInlineStyle option. Fallback: false.");
|
|
327
|
+
noInlineStyle = false;
|
|
328
|
+
}
|
|
329
|
+
return { noInlineStyle };
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// src/index.ts
|
|
348
333
|
function createPortal(host, container = document.body, options = {}) {
|
|
349
334
|
if (!(host instanceof Element)) {
|
|
350
335
|
console.warn("Invalid host element");
|
|
@@ -380,7 +365,7 @@ var Portal = class {
|
|
|
380
365
|
container = document.body;
|
|
381
366
|
}
|
|
382
367
|
this.#container = container;
|
|
383
|
-
this.#settings =
|
|
368
|
+
this.#settings = resolveOptions2(options);
|
|
384
369
|
this.#entranceSentinel = this.#createSentinel();
|
|
385
370
|
this.#exitSentinel = this.#createSentinel();
|
|
386
371
|
this.#initialize();
|
|
@@ -409,17 +394,15 @@ var Portal = class {
|
|
|
409
394
|
for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
|
|
410
395
|
sentinel.addEventListener("focus", this.#onFocus, { signal });
|
|
411
396
|
}
|
|
412
|
-
this.#host.addEventListener("keydown", this.#onKeyDown, {
|
|
413
|
-
signal
|
|
414
|
-
});
|
|
397
|
+
this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
|
|
415
398
|
this.#host.setAttribute("data-portaled", "");
|
|
416
399
|
}
|
|
417
400
|
#onFocus = (event) => {
|
|
418
|
-
const { currentTarget:
|
|
401
|
+
const { currentTarget: target, relatedTarget: previous } = event;
|
|
419
402
|
if (!(previous instanceof Element)) {
|
|
420
403
|
return;
|
|
421
404
|
}
|
|
422
|
-
if (
|
|
405
|
+
if (target === this.#entranceSentinel) {
|
|
423
406
|
if (this.#host.contains(previous)) {
|
|
424
407
|
this.#moveFocus("previous");
|
|
425
408
|
return;
|
|
@@ -438,6 +421,9 @@ var Portal = class {
|
|
|
438
421
|
last ? focusElement(last) : this.#moveFocus("previous");
|
|
439
422
|
};
|
|
440
423
|
#onKeyDown = (event) => {
|
|
424
|
+
if (!(event instanceof KeyboardEvent)) {
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
441
427
|
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
442
428
|
if (key !== "Tab" || altKey || ctrlKey || metaKey) {
|
|
443
429
|
return;
|
|
@@ -506,14 +492,6 @@ var Portal = class {
|
|
|
506
492
|
const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
|
|
507
493
|
focusable && focusElement(focusable);
|
|
508
494
|
}
|
|
509
|
-
#resolveOptions(options) {
|
|
510
|
-
let { noInlineStyle = false } = options;
|
|
511
|
-
if (typeof noInlineStyle !== "boolean") {
|
|
512
|
-
console.warn("Invalid noInlineStyle option. Fallback: false.");
|
|
513
|
-
noInlineStyle = false;
|
|
514
|
-
}
|
|
515
|
-
return { noInlineStyle };
|
|
516
|
-
}
|
|
517
495
|
};
|
|
518
496
|
|
|
519
497
|
exports.createPortal = createPortal;
|
package/dist/index.bundle.js
CHANGED
|
@@ -27,87 +27,114 @@ function saveAttributes(element_or_elements, name_or_names) {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
// node_modules/power-focusable/dist/index.js
|
|
30
|
+
function containsComposed(container, element) {
|
|
31
|
+
let current = element;
|
|
32
|
+
while (current) {
|
|
33
|
+
if (current === container) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
function getComposedChildren(element) {
|
|
41
|
+
if (element instanceof ShadowRoot) {
|
|
42
|
+
return [...element.children];
|
|
43
|
+
}
|
|
44
|
+
if (!(element instanceof Element)) {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
if (element instanceof HTMLSlotElement) {
|
|
48
|
+
const assigned = element.assignedElements({ flatten: true });
|
|
49
|
+
if (assigned.length) {
|
|
50
|
+
return assigned;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (element instanceof HTMLElement && element.shadowRoot?.mode === "open") {
|
|
54
|
+
return [...element.shadowRoot.children];
|
|
55
|
+
}
|
|
56
|
+
return [...element.children];
|
|
57
|
+
}
|
|
30
58
|
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"])';
|
|
31
59
|
var FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX = FOCUSABLE_SELECTOR.replace(
|
|
32
60
|
/(,\s*)?\[tabindex="-1"\]/g,
|
|
33
61
|
""
|
|
34
62
|
);
|
|
35
|
-
function
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
63
|
+
function focusElement(element) {
|
|
64
|
+
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
65
|
+
}
|
|
66
|
+
function getActiveElement() {
|
|
67
|
+
let current = document.activeElement;
|
|
68
|
+
while (current?.shadowRoot?.activeElement) {
|
|
69
|
+
current = current.shadowRoot.activeElement;
|
|
39
70
|
}
|
|
40
|
-
|
|
41
|
-
|
|
71
|
+
return current;
|
|
72
|
+
}
|
|
73
|
+
function getTabIndex(element) {
|
|
74
|
+
return "tabIndex" in element ? Number(element.tabIndex) : -1;
|
|
75
|
+
}
|
|
76
|
+
function isDisabled(element) {
|
|
77
|
+
return "disabled" in element && !!element.disabled;
|
|
78
|
+
}
|
|
79
|
+
function isFormControl(element) {
|
|
80
|
+
return ["BUTTON", "INPUT", "SELECT", "TEXTAREA"].includes(element.tagName);
|
|
81
|
+
}
|
|
82
|
+
function isInert(element) {
|
|
83
|
+
return "inert" in element && !!element.inert;
|
|
84
|
+
}
|
|
85
|
+
function isUngroupedRadio(input) {
|
|
86
|
+
return input.type === "radio" && !!input.name;
|
|
87
|
+
}
|
|
88
|
+
function resolveOptions(options) {
|
|
89
|
+
let {
|
|
90
|
+
anchor = getActiveElement(),
|
|
91
|
+
composed = false,
|
|
42
92
|
filter,
|
|
43
93
|
include,
|
|
44
|
-
skipNegativeTabIndexCheck,
|
|
45
|
-
skipVisibilityCheck
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (!
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
skipNegativeTabIndexCheck,
|
|
54
|
-
skipVisibilityCheck
|
|
55
|
-
}) && candidates.push(match);
|
|
94
|
+
skipNegativeTabIndexCheck = false,
|
|
95
|
+
skipVisibilityCheck = false,
|
|
96
|
+
wrap = false
|
|
97
|
+
} = options;
|
|
98
|
+
if (!(anchor instanceof Element)) {
|
|
99
|
+
const active = getActiveElement();
|
|
100
|
+
if (active instanceof Element) {
|
|
101
|
+
console.warn("Invalid anchor element. Fallback: active element.");
|
|
102
|
+
anchor = active;
|
|
56
103
|
}
|
|
57
|
-
} else {
|
|
58
|
-
let traverse2 = function(node) {
|
|
59
|
-
if (isFocusable(node, {
|
|
60
|
-
skipNegativeTabIndexCheck,
|
|
61
|
-
skipVisibilityCheck
|
|
62
|
-
}) || include?.(node)) {
|
|
63
|
-
candidates.push(node);
|
|
64
|
-
}
|
|
65
|
-
(!composed ? [...node.children] : getComposedChildren(node)).map(
|
|
66
|
-
traverse2
|
|
67
|
-
);
|
|
68
|
-
};
|
|
69
|
-
(!composed ? [...container.children] : getComposedChildren(container)).map(
|
|
70
|
-
traverse2
|
|
71
|
-
);
|
|
72
104
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
function getNextFocusable(container = document.body, options = {}) {
|
|
78
|
-
return getRelativeFocusable(container, 1, options);
|
|
79
|
-
}
|
|
80
|
-
function getPreviousFocusable(container = document.body, options = {}) {
|
|
81
|
-
return getRelativeFocusable(container, -1, options);
|
|
82
|
-
}
|
|
83
|
-
function isFocusable(element, options = {}) {
|
|
84
|
-
if (!(element instanceof Element)) {
|
|
85
|
-
console.warn("Invalid element");
|
|
86
|
-
return false;
|
|
105
|
+
if (typeof composed !== "boolean") {
|
|
106
|
+
console.warn("Invalid composed option. Fallback: false.");
|
|
107
|
+
composed = false;
|
|
87
108
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
109
|
+
if (filter !== void 0 && typeof filter !== "function") {
|
|
110
|
+
console.warn("Invalid filter function. Fallback: no filter function (undefined).");
|
|
111
|
+
filter = void 0;
|
|
91
112
|
}
|
|
92
|
-
if (
|
|
93
|
-
|
|
113
|
+
if (include !== void 0 && typeof include !== "function") {
|
|
114
|
+
console.warn("Invalid include function. Fallback: no include function (undefined).");
|
|
115
|
+
include = void 0;
|
|
94
116
|
}
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return false;
|
|
117
|
+
if (typeof skipNegativeTabIndexCheck !== "boolean") {
|
|
118
|
+
console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
|
|
119
|
+
skipNegativeTabIndexCheck = false;
|
|
99
120
|
}
|
|
100
|
-
if (
|
|
101
|
-
|
|
121
|
+
if (typeof skipVisibilityCheck !== "boolean") {
|
|
122
|
+
console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
|
|
123
|
+
skipVisibilityCheck = false;
|
|
102
124
|
}
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
visibilityProperty: true
|
|
107
|
-
})) {
|
|
108
|
-
return false;
|
|
125
|
+
if (typeof wrap !== "boolean") {
|
|
126
|
+
console.warn("Invalid wrap option. Fallback: false.");
|
|
127
|
+
wrap = false;
|
|
109
128
|
}
|
|
110
|
-
return
|
|
129
|
+
return {
|
|
130
|
+
anchor,
|
|
131
|
+
composed,
|
|
132
|
+
filter,
|
|
133
|
+
include,
|
|
134
|
+
skipNegativeTabIndexCheck,
|
|
135
|
+
skipVisibilityCheck,
|
|
136
|
+
wrap
|
|
137
|
+
};
|
|
111
138
|
}
|
|
112
139
|
function getRelativeFocusable(container, offset, options) {
|
|
113
140
|
if (!(container instanceof Element)) {
|
|
@@ -172,10 +199,8 @@ function isDisabledDeep(element) {
|
|
|
172
199
|
if (isInert(current)) {
|
|
173
200
|
return true;
|
|
174
201
|
}
|
|
175
|
-
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
|
|
176
|
-
|
|
177
|
-
return true;
|
|
178
|
-
}
|
|
202
|
+
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current) && !current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
|
|
203
|
+
return true;
|
|
179
204
|
}
|
|
180
205
|
current = current.parentNode;
|
|
181
206
|
}
|
|
@@ -184,10 +209,7 @@ function isDisabledDeep(element) {
|
|
|
184
209
|
function normalizeRadioGroup(elements) {
|
|
185
210
|
let map = null;
|
|
186
211
|
for (const element of elements) {
|
|
187
|
-
if (!(element instanceof HTMLInputElement)) {
|
|
188
|
-
continue;
|
|
189
|
-
}
|
|
190
|
-
if (!isUngroupedRadio(element)) {
|
|
212
|
+
if (!(element instanceof HTMLInputElement) || !isUngroupedRadio(element)) {
|
|
191
213
|
continue;
|
|
192
214
|
}
|
|
193
215
|
if (!map) {
|
|
@@ -232,117 +254,80 @@ function sortByTabIndex(elements) {
|
|
|
232
254
|
}
|
|
233
255
|
return ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b)).concat(natural);
|
|
234
256
|
}
|
|
235
|
-
function
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
return true;
|
|
240
|
-
}
|
|
241
|
-
current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
|
|
242
|
-
}
|
|
243
|
-
return false;
|
|
244
|
-
}
|
|
245
|
-
function getComposedChildren(node) {
|
|
246
|
-
if (node instanceof ShadowRoot) {
|
|
247
|
-
return [...node.children];
|
|
248
|
-
}
|
|
249
|
-
if (!(node instanceof Element)) {
|
|
250
|
-
return [];
|
|
257
|
+
function getFocusables(container = document.body, options = {}) {
|
|
258
|
+
if (!(container instanceof Element)) {
|
|
259
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
260
|
+
container = document.body;
|
|
251
261
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
262
|
+
const { composed, filter, include, skipNegativeTabIndexCheck, skipVisibilityCheck } = resolveOptions(options);
|
|
263
|
+
const candidates = [];
|
|
264
|
+
if (!composed && !include) {
|
|
265
|
+
for (const match of container.querySelectorAll(
|
|
266
|
+
!skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
|
|
267
|
+
)) {
|
|
268
|
+
match && isFocusable(match, { skipNegativeTabIndexCheck, skipVisibilityCheck }) && candidates.push(match);
|
|
256
269
|
}
|
|
270
|
+
} else {
|
|
271
|
+
let traverse2 = function(element) {
|
|
272
|
+
if (isFocusable(element, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(element)) {
|
|
273
|
+
candidates.push(element);
|
|
274
|
+
}
|
|
275
|
+
(!composed ? [...element.children] : getComposedChildren(element)).map(traverse2);
|
|
276
|
+
};
|
|
277
|
+
(!composed ? [...container.children] : getComposedChildren(container)).map(traverse2);
|
|
257
278
|
}
|
|
258
|
-
|
|
259
|
-
return [...node.shadowRoot.children];
|
|
260
|
-
}
|
|
261
|
-
return [...node.children];
|
|
262
|
-
}
|
|
263
|
-
function focusElement(element) {
|
|
264
|
-
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
265
|
-
}
|
|
266
|
-
function getActiveElement() {
|
|
267
|
-
let current = document.activeElement;
|
|
268
|
-
while (current?.shadowRoot?.activeElement) {
|
|
269
|
-
current = current.shadowRoot.activeElement;
|
|
270
|
-
}
|
|
271
|
-
return current;
|
|
272
|
-
}
|
|
273
|
-
function getTabIndex(element) {
|
|
274
|
-
return "tabIndex" in element ? Number(element.tabIndex) : 0;
|
|
275
|
-
}
|
|
276
|
-
function isDisabled(element) {
|
|
277
|
-
return "disabled" in element && !!element.disabled;
|
|
279
|
+
return normalizeRadioGroup(sortByTabIndex(!filter ? candidates : candidates.filter(filter)));
|
|
278
280
|
}
|
|
279
|
-
function
|
|
280
|
-
return
|
|
281
|
-
}
|
|
282
|
-
function isInert(element) {
|
|
283
|
-
return "inert" in element && !!element.inert;
|
|
281
|
+
function getNextFocusable(container = document.body, options = {}) {
|
|
282
|
+
return getRelativeFocusable(container, 1, options);
|
|
284
283
|
}
|
|
285
|
-
function
|
|
286
|
-
return
|
|
284
|
+
function getPreviousFocusable(container = document.body, options = {}) {
|
|
285
|
+
return getRelativeFocusable(container, -1, options);
|
|
287
286
|
}
|
|
288
|
-
function
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
filter,
|
|
293
|
-
include,
|
|
294
|
-
skipNegativeTabIndexCheck = false,
|
|
295
|
-
skipVisibilityCheck = false,
|
|
296
|
-
wrap = false
|
|
297
|
-
} = options;
|
|
298
|
-
if (!(anchor instanceof Element)) {
|
|
299
|
-
const active = getActiveElement();
|
|
300
|
-
if (active instanceof Element) {
|
|
301
|
-
console.warn("Invalid anchor element. Fallback: active element.");
|
|
302
|
-
anchor = active;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
if (typeof composed !== "boolean") {
|
|
306
|
-
console.warn("Invalid composed option. Fallback: false.");
|
|
307
|
-
composed = false;
|
|
287
|
+
function isFocusable(element, options = {}) {
|
|
288
|
+
if (!(element instanceof Element)) {
|
|
289
|
+
console.warn("Invalid element");
|
|
290
|
+
return false;
|
|
308
291
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
);
|
|
313
|
-
filter = void 0;
|
|
292
|
+
const { skipNegativeTabIndexCheck, skipVisibilityCheck } = resolveOptions(options);
|
|
293
|
+
if (element.hasAttribute("hidden") || isInert(element)) {
|
|
294
|
+
return false;
|
|
314
295
|
}
|
|
315
|
-
if (
|
|
316
|
-
|
|
317
|
-
"Invalid include function. Fallback: no include function (undefined)."
|
|
318
|
-
);
|
|
319
|
-
include = void 0;
|
|
296
|
+
if (!skipNegativeTabIndexCheck && getTabIndex(element) < 0) {
|
|
297
|
+
return false;
|
|
320
298
|
}
|
|
321
|
-
if (
|
|
322
|
-
|
|
323
|
-
|
|
299
|
+
if (!element.matches(
|
|
300
|
+
!skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
|
|
301
|
+
)) {
|
|
302
|
+
return false;
|
|
324
303
|
}
|
|
325
|
-
if (
|
|
326
|
-
|
|
327
|
-
skipVisibilityCheck = false;
|
|
304
|
+
if (isDisabledDeep(element)) {
|
|
305
|
+
return false;
|
|
328
306
|
}
|
|
329
|
-
if (
|
|
330
|
-
|
|
331
|
-
|
|
307
|
+
if (!skipVisibilityCheck && !element.checkVisibility({
|
|
308
|
+
contentVisibilityAuto: true,
|
|
309
|
+
opacityProperty: true,
|
|
310
|
+
visibilityProperty: true
|
|
311
|
+
})) {
|
|
312
|
+
return false;
|
|
332
313
|
}
|
|
333
|
-
return
|
|
334
|
-
anchor,
|
|
335
|
-
composed,
|
|
336
|
-
filter,
|
|
337
|
-
include,
|
|
338
|
-
skipNegativeTabIndexCheck,
|
|
339
|
-
skipVisibilityCheck,
|
|
340
|
-
wrap
|
|
341
|
-
};
|
|
314
|
+
return true;
|
|
342
315
|
}
|
|
343
316
|
|
|
344
|
-
// src/
|
|
317
|
+
// src/constants.ts
|
|
345
318
|
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;";
|
|
319
|
+
|
|
320
|
+
// src/options.ts
|
|
321
|
+
function resolveOptions2(options) {
|
|
322
|
+
let { noInlineStyle = false } = options;
|
|
323
|
+
if (typeof noInlineStyle !== "boolean") {
|
|
324
|
+
console.warn("Invalid noInlineStyle option. Fallback: false.");
|
|
325
|
+
noInlineStyle = false;
|
|
326
|
+
}
|
|
327
|
+
return { noInlineStyle };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// src/index.ts
|
|
346
331
|
function createPortal(host, container = document.body, options = {}) {
|
|
347
332
|
if (!(host instanceof Element)) {
|
|
348
333
|
console.warn("Invalid host element");
|
|
@@ -378,7 +363,7 @@ var Portal = class {
|
|
|
378
363
|
container = document.body;
|
|
379
364
|
}
|
|
380
365
|
this.#container = container;
|
|
381
|
-
this.#settings =
|
|
366
|
+
this.#settings = resolveOptions2(options);
|
|
382
367
|
this.#entranceSentinel = this.#createSentinel();
|
|
383
368
|
this.#exitSentinel = this.#createSentinel();
|
|
384
369
|
this.#initialize();
|
|
@@ -407,17 +392,15 @@ var Portal = class {
|
|
|
407
392
|
for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
|
|
408
393
|
sentinel.addEventListener("focus", this.#onFocus, { signal });
|
|
409
394
|
}
|
|
410
|
-
this.#host.addEventListener("keydown", this.#onKeyDown, {
|
|
411
|
-
signal
|
|
412
|
-
});
|
|
395
|
+
this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
|
|
413
396
|
this.#host.setAttribute("data-portaled", "");
|
|
414
397
|
}
|
|
415
398
|
#onFocus = (event) => {
|
|
416
|
-
const { currentTarget:
|
|
399
|
+
const { currentTarget: target, relatedTarget: previous } = event;
|
|
417
400
|
if (!(previous instanceof Element)) {
|
|
418
401
|
return;
|
|
419
402
|
}
|
|
420
|
-
if (
|
|
403
|
+
if (target === this.#entranceSentinel) {
|
|
421
404
|
if (this.#host.contains(previous)) {
|
|
422
405
|
this.#moveFocus("previous");
|
|
423
406
|
return;
|
|
@@ -436,6 +419,9 @@ var Portal = class {
|
|
|
436
419
|
last ? focusElement(last) : this.#moveFocus("previous");
|
|
437
420
|
};
|
|
438
421
|
#onKeyDown = (event) => {
|
|
422
|
+
if (!(event instanceof KeyboardEvent)) {
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
439
425
|
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
440
426
|
if (key !== "Tab" || altKey || ctrlKey || metaKey) {
|
|
441
427
|
return;
|
|
@@ -504,14 +490,6 @@ var Portal = class {
|
|
|
504
490
|
const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
|
|
505
491
|
focusable && focusElement(focusable);
|
|
506
492
|
}
|
|
507
|
-
#resolveOptions(options) {
|
|
508
|
-
let { noInlineStyle = false } = options;
|
|
509
|
-
if (typeof noInlineStyle !== "boolean") {
|
|
510
|
-
console.warn("Invalid noInlineStyle option. Fallback: false.");
|
|
511
|
-
noInlineStyle = false;
|
|
512
|
-
}
|
|
513
|
-
return { noInlineStyle };
|
|
514
|
-
}
|
|
515
493
|
};
|
|
516
494
|
|
|
517
495
|
export { createPortal };
|
package/dist/index.cjs
CHANGED
|
@@ -4,7 +4,21 @@ var attributeUtils = require('@y14e/attribute-utils');
|
|
|
4
4
|
var powerFocusable = require('power-focusable');
|
|
5
5
|
|
|
6
6
|
// src/index.ts
|
|
7
|
+
|
|
8
|
+
// src/constants.ts
|
|
7
9
|
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;";
|
|
10
|
+
|
|
11
|
+
// src/options.ts
|
|
12
|
+
function resolveOptions(options) {
|
|
13
|
+
let { noInlineStyle = false } = options;
|
|
14
|
+
if (typeof noInlineStyle !== "boolean") {
|
|
15
|
+
console.warn("Invalid noInlineStyle option. Fallback: false.");
|
|
16
|
+
noInlineStyle = false;
|
|
17
|
+
}
|
|
18
|
+
return { noInlineStyle };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
8
22
|
function createPortal(host, container = document.body, options = {}) {
|
|
9
23
|
if (!(host instanceof Element)) {
|
|
10
24
|
console.warn("Invalid host element");
|
|
@@ -40,7 +54,7 @@ var Portal = class {
|
|
|
40
54
|
container = document.body;
|
|
41
55
|
}
|
|
42
56
|
this.#container = container;
|
|
43
|
-
this.#settings =
|
|
57
|
+
this.#settings = resolveOptions(options);
|
|
44
58
|
this.#entranceSentinel = this.#createSentinel();
|
|
45
59
|
this.#exitSentinel = this.#createSentinel();
|
|
46
60
|
this.#initialize();
|
|
@@ -69,17 +83,15 @@ var Portal = class {
|
|
|
69
83
|
for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
|
|
70
84
|
sentinel.addEventListener("focus", this.#onFocus, { signal });
|
|
71
85
|
}
|
|
72
|
-
this.#host.addEventListener("keydown", this.#onKeyDown, {
|
|
73
|
-
signal
|
|
74
|
-
});
|
|
86
|
+
this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
|
|
75
87
|
this.#host.setAttribute("data-portaled", "");
|
|
76
88
|
}
|
|
77
89
|
#onFocus = (event) => {
|
|
78
|
-
const { currentTarget:
|
|
90
|
+
const { currentTarget: target, relatedTarget: previous } = event;
|
|
79
91
|
if (!(previous instanceof Element)) {
|
|
80
92
|
return;
|
|
81
93
|
}
|
|
82
|
-
if (
|
|
94
|
+
if (target === this.#entranceSentinel) {
|
|
83
95
|
if (this.#host.contains(previous)) {
|
|
84
96
|
this.#moveFocus("previous");
|
|
85
97
|
return;
|
|
@@ -98,6 +110,9 @@ var Portal = class {
|
|
|
98
110
|
last ? powerFocusable.focusElement(last) : this.#moveFocus("previous");
|
|
99
111
|
};
|
|
100
112
|
#onKeyDown = (event) => {
|
|
113
|
+
if (!(event instanceof KeyboardEvent)) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
101
116
|
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
102
117
|
if (key !== "Tab" || altKey || ctrlKey || metaKey) {
|
|
103
118
|
return;
|
|
@@ -166,14 +181,6 @@ var Portal = class {
|
|
|
166
181
|
const focusable = direction === "previous" ? powerFocusable.getPreviousFocusable(document.body, options) : powerFocusable.getNextFocusable(document.body, options);
|
|
167
182
|
focusable && powerFocusable.focusElement(focusable);
|
|
168
183
|
}
|
|
169
|
-
#resolveOptions(options) {
|
|
170
|
-
let { noInlineStyle = false } = options;
|
|
171
|
-
if (typeof noInlineStyle !== "boolean") {
|
|
172
|
-
console.warn("Invalid noInlineStyle option. Fallback: false.");
|
|
173
|
-
noInlineStyle = false;
|
|
174
|
-
}
|
|
175
|
-
return { noInlineStyle };
|
|
176
|
-
}
|
|
177
184
|
};
|
|
178
185
|
|
|
179
186
|
exports.createPortal = createPortal;
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -2,7 +2,21 @@ import { restoreAttributes, saveAttributes } from '@y14e/attribute-utils';
|
|
|
2
2
|
import { containsComposed, focusElement, getActiveElement, getFocusables, getPreviousFocusable, getNextFocusable } from 'power-focusable';
|
|
3
3
|
|
|
4
4
|
// src/index.ts
|
|
5
|
+
|
|
6
|
+
// src/constants.ts
|
|
5
7
|
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;";
|
|
8
|
+
|
|
9
|
+
// src/options.ts
|
|
10
|
+
function resolveOptions(options) {
|
|
11
|
+
let { noInlineStyle = false } = options;
|
|
12
|
+
if (typeof noInlineStyle !== "boolean") {
|
|
13
|
+
console.warn("Invalid noInlineStyle option. Fallback: false.");
|
|
14
|
+
noInlineStyle = false;
|
|
15
|
+
}
|
|
16
|
+
return { noInlineStyle };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
6
20
|
function createPortal(host, container = document.body, options = {}) {
|
|
7
21
|
if (!(host instanceof Element)) {
|
|
8
22
|
console.warn("Invalid host element");
|
|
@@ -38,7 +52,7 @@ var Portal = class {
|
|
|
38
52
|
container = document.body;
|
|
39
53
|
}
|
|
40
54
|
this.#container = container;
|
|
41
|
-
this.#settings =
|
|
55
|
+
this.#settings = resolveOptions(options);
|
|
42
56
|
this.#entranceSentinel = this.#createSentinel();
|
|
43
57
|
this.#exitSentinel = this.#createSentinel();
|
|
44
58
|
this.#initialize();
|
|
@@ -67,17 +81,15 @@ var Portal = class {
|
|
|
67
81
|
for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
|
|
68
82
|
sentinel.addEventListener("focus", this.#onFocus, { signal });
|
|
69
83
|
}
|
|
70
|
-
this.#host.addEventListener("keydown", this.#onKeyDown, {
|
|
71
|
-
signal
|
|
72
|
-
});
|
|
84
|
+
this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
|
|
73
85
|
this.#host.setAttribute("data-portaled", "");
|
|
74
86
|
}
|
|
75
87
|
#onFocus = (event) => {
|
|
76
|
-
const { currentTarget:
|
|
88
|
+
const { currentTarget: target, relatedTarget: previous } = event;
|
|
77
89
|
if (!(previous instanceof Element)) {
|
|
78
90
|
return;
|
|
79
91
|
}
|
|
80
|
-
if (
|
|
92
|
+
if (target === this.#entranceSentinel) {
|
|
81
93
|
if (this.#host.contains(previous)) {
|
|
82
94
|
this.#moveFocus("previous");
|
|
83
95
|
return;
|
|
@@ -96,6 +108,9 @@ var Portal = class {
|
|
|
96
108
|
last ? focusElement(last) : this.#moveFocus("previous");
|
|
97
109
|
};
|
|
98
110
|
#onKeyDown = (event) => {
|
|
111
|
+
if (!(event instanceof KeyboardEvent)) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
99
114
|
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
100
115
|
if (key !== "Tab" || altKey || ctrlKey || metaKey) {
|
|
101
116
|
return;
|
|
@@ -164,14 +179,6 @@ var Portal = class {
|
|
|
164
179
|
const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
|
|
165
180
|
focusable && focusElement(focusable);
|
|
166
181
|
}
|
|
167
|
-
#resolveOptions(options) {
|
|
168
|
-
let { noInlineStyle = false } = options;
|
|
169
|
-
if (typeof noInlineStyle !== "boolean") {
|
|
170
|
-
console.warn("Invalid noInlineStyle option. Fallback: false.");
|
|
171
|
-
noInlineStyle = false;
|
|
172
|
-
}
|
|
173
|
-
return { noInlineStyle };
|
|
174
|
-
}
|
|
175
182
|
};
|
|
176
183
|
|
|
177
184
|
export { createPortal };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@y14e/portal",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.6",
|
|
4
4
|
"description": "Lightweight DOM portal (teleport) utility with fully focus management",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"a11y",
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"esbuild": true
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@y14e/attribute-utils": "^2.0.
|
|
62
|
-
"power-focusable": "^4.
|
|
61
|
+
"@y14e/attribute-utils": "^2.0.18",
|
|
62
|
+
"power-focusable": "^4.5.9"
|
|
63
63
|
}
|
|
64
64
|
}
|