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