@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
package/dist/index.js
CHANGED
|
@@ -1,362 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
-
}
|
|
1
|
+
import { restoreAttributes, saveAttributes } from '@y14e/attributes-utils';
|
|
2
|
+
import { focusElement, getNextFocusable, getPreviousFocusable, getActiveElement, getFocusables } from 'power-focusable';
|
|
360
3
|
|
|
361
4
|
// src/index.ts
|
|
362
5
|
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;`;
|
|
@@ -371,7 +14,7 @@ function createPortal(host, container = document.body) {
|
|
|
371
14
|
return () => {
|
|
372
15
|
};
|
|
373
16
|
}
|
|
374
|
-
if (
|
|
17
|
+
if (containsComposed(host, container)) {
|
|
375
18
|
console.warn("Host element cannot contain the container element");
|
|
376
19
|
return () => {
|
|
377
20
|
};
|
|
@@ -548,7 +191,7 @@ var Portal = class {
|
|
|
548
191
|
focusable && focusElement(focusable);
|
|
549
192
|
}
|
|
550
193
|
};
|
|
551
|
-
function
|
|
194
|
+
function containsComposed(container, element) {
|
|
552
195
|
let current = element;
|
|
553
196
|
while (current) {
|
|
554
197
|
if (current === container) {
|
|
@@ -563,37 +206,11 @@ function containsComposed2(container, element) {
|
|
|
563
206
|
* Lightweight DOM portal (teleport) utility with fully focus management.
|
|
564
207
|
* Designed for accessible dialogs, menus, overlays, popovers.
|
|
565
208
|
*
|
|
566
|
-
* @version 1.2.
|
|
209
|
+
* @version 1.2.20
|
|
567
210
|
* @author Yusuke Kamiyamane
|
|
568
211
|
* @license MIT
|
|
569
212
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
570
213
|
* @see {@link https://github.com/y14e/portal}
|
|
571
214
|
*/
|
|
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
215
|
|
|
599
|
-
export { createPortal
|
|
216
|
+
export { createPortal };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@y14e/portal",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.20",
|
|
4
4
|
"description": "Lightweight DOM portal (teleport) utility with fully focus management",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -47,13 +47,15 @@
|
|
|
47
47
|
},
|
|
48
48
|
"homepage": "https://github.com/y14e/portal#readme",
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@y14e/attributes-utils": "^1.1.2",
|
|
51
50
|
"bun-types": "latest",
|
|
52
|
-
"power-focusable": "^4.3.4",
|
|
53
51
|
"tsup": "^8.0.0",
|
|
54
52
|
"typescript": "^5.6.0"
|
|
55
53
|
},
|
|
56
54
|
"engines": {
|
|
57
55
|
"node": ">=18"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"@y14e/attributes-utils": "^1.1.2",
|
|
59
|
+
"power-focusable": "^4.3.4"
|
|
58
60
|
}
|
|
59
61
|
}
|