@y14e/portal 1.2.18 → 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/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
  };
@@ -412,7 +55,7 @@ var Portal = class {
412
55
  cancelAnimationFrame(this.#timer);
413
56
  this.#timer = void 0;
414
57
  }
415
- restoreAttributes([...this.#focusables]);
58
+ attributesUtils.restoreAttributes([...this.#focusables]);
416
59
  this.#focusables.clear();
417
60
  this.#exitSentinel.after(this.#host);
418
61
  this.#entranceSentinel.remove();
@@ -450,13 +93,13 @@ var Portal = class {
450
93
  this.#update();
451
94
  const first = [...this.#focusables][0];
452
95
  if (first) {
453
- focusElement(first);
96
+ powerFocusable.focusElement(first);
454
97
  } else {
455
- const next = getNextFocusable(document.body, {
98
+ const next = powerFocusable.getNextFocusable(document.body, {
456
99
  anchor: this.#exitSentinel,
457
100
  composed: true
458
101
  });
459
- next && focusElement(next);
102
+ next && powerFocusable.focusElement(next);
460
103
  }
461
104
  } else if (current === this.#exitSentinel) {
462
105
  if (this.#host.contains(before)) {
@@ -466,13 +109,13 @@ var Portal = class {
466
109
  this.#update();
467
110
  const last = [...this.#focusables].at(-1);
468
111
  if (last) {
469
- focusElement(last);
112
+ powerFocusable.focusElement(last);
470
113
  } else {
471
- const previous = getPreviousFocusable(document.body, {
114
+ const previous = powerFocusable.getPreviousFocusable(document.body, {
472
115
  anchor: this.#entranceSentinel,
473
116
  composed: true
474
117
  });
475
- previous && focusElement(previous);
118
+ previous && powerFocusable.focusElement(previous);
476
119
  }
477
120
  }
478
121
  };
@@ -481,7 +124,7 @@ var Portal = class {
481
124
  if (key !== "Tab" || altKey || ctrlKey || metaKey) {
482
125
  return;
483
126
  }
484
- const active = getActiveElement();
127
+ const active = powerFocusable.getActiveElement();
485
128
  if (!(active instanceof Element)) {
486
129
  return;
487
130
  }
@@ -495,7 +138,7 @@ var Portal = class {
495
138
  if (index !== -1) {
496
139
  event.preventDefault();
497
140
  const focusable = focusables[index + (shiftKey ? -1 : 1)];
498
- focusable ? focusElement(focusable) : this.#focusSentinel(shiftKey);
141
+ focusable ? powerFocusable.focusElement(focusable) : this.#focusSentinel(shiftKey);
499
142
  }
500
143
  } else {
501
144
  event.preventDefault();
@@ -505,18 +148,18 @@ var Portal = class {
505
148
  #update() {
506
149
  const current = /* @__PURE__ */ new Set([
507
150
  ...this.#getFocusables(),
508
- ...getFocusables(this.#host, { composed: true })
151
+ ...powerFocusable.getFocusables(this.#host, { composed: true })
509
152
  ]);
510
153
  for (const focusable of this.#focusables) {
511
154
  if (!current.has(focusable)) {
512
- focusable.isConnected && restoreAttributes([focusable]);
155
+ focusable.isConnected && attributesUtils.restoreAttributes([focusable]);
513
156
  this.#focusables.delete(focusable);
514
157
  }
515
158
  }
516
159
  for (const focusable of current) {
517
160
  if (!this.#focusables.has(focusable)) {
518
161
  this.#focusables.add(focusable);
519
- saveAttributes([focusable], ["tabindex"]);
162
+ attributesUtils.saveAttributes([focusable], ["tabindex"]);
520
163
  focusable.setAttribute("tabindex", "-1");
521
164
  }
522
165
  }
@@ -536,7 +179,7 @@ var Portal = class {
536
179
  );
537
180
  }
538
181
  #getFocusables() {
539
- return getFocusables(this.#host, {
182
+ return powerFocusable.getFocusables(this.#host, {
540
183
  composed: true,
541
184
  include: (element) => this.#focusables.has(element)
542
185
  });
@@ -546,11 +189,11 @@ var Portal = class {
546
189
  anchor: direction === "previous" ? this.#entranceSentinel : this.#exitSentinel,
547
190
  composed: true
548
191
  };
549
- const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
550
- focusable && focusElement(focusable);
192
+ const focusable = direction === "previous" ? powerFocusable.getPreviousFocusable(document.body, options) : powerFocusable.getNextFocusable(document.body, options);
193
+ focusable && powerFocusable.focusElement(focusable);
551
194
  }
552
195
  };
553
- function containsComposed2(container, element) {
196
+ function containsComposed(container, element) {
554
197
  let current = element;
555
198
  while (current) {
556
199
  if (current === container) {
@@ -565,42 +208,11 @@ function containsComposed2(container, element) {
565
208
  * Lightweight DOM portal (teleport) utility with fully focus management.
566
209
  * Designed for accessible dialogs, menus, overlays, popovers.
567
210
  *
568
- * @version 1.2.18
211
+ * @version 1.2.20
569
212
  * @author Yusuke Kamiyamane
570
213
  * @license MIT
571
214
  * @copyright Copyright (c) Yusuke Kamiyamane
572
215
  * @see {@link https://github.com/y14e/portal}
573
216
  */
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
217
 
601
218
  exports.createPortal = createPortal;
602
- exports.getFocusables = getFocusables;
603
- exports.getNextFocusable = getNextFocusable;
604
- exports.getPreviousFocusable = getPreviousFocusable;
605
- exports.restoreAttributes = restoreAttributes;
606
- exports.saveAttributes = saveAttributes;
package/dist/index.d.cts CHANGED
@@ -1,18 +1,14 @@
1
- export { restoreAttributes, saveAttributes } from '@y14e/attributes-utils';
2
- export { 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.18
6
+ * @version 1.2.20
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 { 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.18
6
+ * @version 1.2.20
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 };