@y14e/portal 1.2.19 → 1.2.21

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.
@@ -0,0 +1,591 @@
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
+ #isDestroyed = false;
390
+ constructor(host, container) {
391
+ this.#host = host;
392
+ if (!(container instanceof Element)) {
393
+ console.warn("Invalid container element. Fallback: <body> element.");
394
+ container = document.body;
395
+ }
396
+ this.#container = container;
397
+ this.#entranceSentinel = this.#createSentinel();
398
+ this.#exitSentinel = this.#createSentinel();
399
+ this.#initialize();
400
+ }
401
+ destroy() {
402
+ if (this.#isDestroyed) {
403
+ return;
404
+ }
405
+ this.#isDestroyed = true;
406
+ this.#controller?.abort();
407
+ this.#controller = null;
408
+ restoreAttributes([...this.#focusables]);
409
+ this.#focusables.clear();
410
+ this.#exitSentinel.after(this.#host);
411
+ this.#entranceSentinel.remove();
412
+ this.#exitSentinel.remove();
413
+ this.#host.removeAttribute("data-portaled");
414
+ }
415
+ #initialize() {
416
+ this.#host.before(this.#entranceSentinel);
417
+ this.#entranceSentinel.after(this.#exitSentinel);
418
+ this.#container.append(this.#host);
419
+ this.#update();
420
+ this.#controller = new AbortController();
421
+ const { signal } = this.#controller;
422
+ document.addEventListener("focusin", this.#onFocusIn, {
423
+ capture: true,
424
+ signal
425
+ });
426
+ document.addEventListener("keydown", this.#onKeyDown, {
427
+ capture: true,
428
+ signal
429
+ });
430
+ this.#host.setAttribute("data-portaled", "");
431
+ }
432
+ #onFocusIn = (event) => {
433
+ const current = event.target;
434
+ const before = event.relatedTarget;
435
+ if (!(before instanceof Element)) {
436
+ return;
437
+ }
438
+ if (current === this.#entranceSentinel) {
439
+ if (this.#host.contains(before)) {
440
+ this.#moveFocus("previous");
441
+ return;
442
+ }
443
+ this.#update();
444
+ const first = [...this.#focusables][0];
445
+ if (first) {
446
+ focusElement(first);
447
+ } else {
448
+ const next = getNextFocusable(document.body, {
449
+ anchor: this.#exitSentinel,
450
+ composed: true
451
+ });
452
+ next && focusElement(next);
453
+ }
454
+ } else if (current === this.#exitSentinel) {
455
+ if (this.#host.contains(before)) {
456
+ this.#moveFocus("next");
457
+ return;
458
+ }
459
+ this.#update();
460
+ const last = [...this.#focusables].at(-1);
461
+ if (last) {
462
+ focusElement(last);
463
+ } else {
464
+ const previous = getPreviousFocusable(document.body, {
465
+ anchor: this.#entranceSentinel,
466
+ composed: true
467
+ });
468
+ previous && focusElement(previous);
469
+ }
470
+ }
471
+ };
472
+ #onKeyDown = (event) => {
473
+ const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
474
+ if (key !== "Tab" || altKey || ctrlKey || metaKey) {
475
+ return;
476
+ }
477
+ const active = getActiveElement();
478
+ if (!(active instanceof Element)) {
479
+ return;
480
+ }
481
+ if (!this.#host.contains(active)) {
482
+ return;
483
+ }
484
+ this.#update();
485
+ const focusables = this.#getFocusables();
486
+ if (focusables.length) {
487
+ const index = focusables.indexOf(active);
488
+ if (index !== -1) {
489
+ event.preventDefault();
490
+ const focusable = focusables[index + (shiftKey ? -1 : 1)];
491
+ focusable ? focusElement(focusable) : this.#focusSentinel(shiftKey);
492
+ }
493
+ } else {
494
+ event.preventDefault();
495
+ this.#moveFocus(shiftKey ? "previous" : "next");
496
+ }
497
+ };
498
+ #update() {
499
+ const current = /* @__PURE__ */ new Set([
500
+ ...this.#getFocusables(),
501
+ ...getFocusables(this.#host, { composed: true })
502
+ ]);
503
+ for (const focusable of this.#focusables) {
504
+ if (!current.has(focusable)) {
505
+ focusable.isConnected && restoreAttributes([focusable]);
506
+ this.#focusables.delete(focusable);
507
+ }
508
+ }
509
+ for (const focusable of current) {
510
+ if (!this.#focusables.has(focusable)) {
511
+ this.#focusables.add(focusable);
512
+ saveAttributes([focusable], ["tabindex"]);
513
+ focusable.setAttribute("tabindex", "-1");
514
+ }
515
+ }
516
+ }
517
+ #createSentinel() {
518
+ const sentinel = document.createElement("span");
519
+ sentinel.setAttribute("aria-hidden", "true");
520
+ sentinel.setAttribute("data-portal-sentinel", "");
521
+ sentinel.setAttribute("tabindex", "0");
522
+ sentinel.style.cssText += VISUALLY_HIDDEN_CSS;
523
+ return sentinel;
524
+ }
525
+ #focusSentinel(isPrevious) {
526
+ (isPrevious ? this.#entranceSentinel : this.#exitSentinel).focus();
527
+ }
528
+ #getFocusables() {
529
+ return getFocusables(this.#host, {
530
+ composed: true,
531
+ include: (element) => this.#focusables.has(element)
532
+ });
533
+ }
534
+ #moveFocus(direction) {
535
+ const options = {
536
+ anchor: direction === "previous" ? this.#entranceSentinel : this.#exitSentinel,
537
+ composed: true
538
+ };
539
+ const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
540
+ focusable && focusElement(focusable);
541
+ }
542
+ };
543
+ function containsComposed2(container, element) {
544
+ let current = element;
545
+ while (current) {
546
+ if (current === container) {
547
+ return true;
548
+ }
549
+ current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
550
+ }
551
+ return false;
552
+ }
553
+ /**
554
+ * Portal
555
+ * Lightweight DOM portal (teleport) utility with fully focus management.
556
+ * Designed for accessible dialogs, menus, overlays, popovers.
557
+ *
558
+ * @version 1.2.21
559
+ * @author Yusuke Kamiyamane
560
+ * @license MIT
561
+ * @copyright Copyright (c) Yusuke Kamiyamane
562
+ * @see {@link https://github.com/y14e/portal}
563
+ */
564
+ /*! Bundled license information:
565
+
566
+ @y14e/attributes-utils/dist/index.js:
567
+ (**
568
+ * Attributes Utils
569
+ *
570
+ * @version 1.1.2
571
+ * @author Yusuke Kamiyamane
572
+ * @license MIT
573
+ * @copyright Copyright (c) Yusuke Kamiyamane
574
+ * @see {@link https://github.com/y14e/attributes-utils}
575
+ *)
576
+
577
+ power-focusable/dist/index.js:
578
+ (**
579
+ * Power Focusable
580
+ * High-precision focus management utility with full composed tree support.
581
+ * Handles complex focus rules including tabindex ordering, radio groups, inert.
582
+ *
583
+ * @version 4.3.3
584
+ * @author Yusuke Kamiyamane
585
+ * @license MIT
586
+ * @copyright Copyright (c) Yusuke Kamiyamane
587
+ * @see {@link https://github.com/y14e/power-focusable}
588
+ *)
589
+ */
590
+
591
+ export { createPortal };