@y14e/portal 1.4.1 → 1.4.3

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 CHANGED
@@ -13,11 +13,11 @@ npm i @y14e/portal
13
13
  import { createPortal } from '@y14e/portal';
14
14
 
15
15
  // CDNs
16
- import { createPortal } from 'https://esm.sh/@y14e/portal@1.4.1';
16
+ import { createPortal } from 'https://esm.sh/@y14e/portal@<x.x.x>';
17
17
  // or
18
- import { createPortal } from 'https://cdn.jsdelivr.net/npm/@y14e/portal@1.4.1/+esm';
18
+ import { createPortal } from 'https://cdn.jsdelivr.net/npm/@y14e/portal@<x.x.x>/+esm';
19
19
  // or
20
- import { createPortal } from 'https://esm.unpkg.com/@y14e/portal@1.4.1';
20
+ import { createPortal } from 'https://esm.unpkg.com/@y14e/portal@<x.x.x>';
21
21
  ```
22
22
 
23
23
  ## 📦 APIs
@@ -47,7 +47,16 @@ function getFocusables(container = document.body, options = {}) {
47
47
  skipVisibilityCheck
48
48
  } = resolveOptions(options);
49
49
  const candidates = [];
50
- if (composed || include) {
50
+ if (!composed && !include) {
51
+ for (const match of container.querySelectorAll(
52
+ skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX : FOCUSABLE_SELECTOR
53
+ )) {
54
+ match && isFocusable(match, {
55
+ skipNegativeTabIndexCheck,
56
+ skipVisibilityCheck
57
+ }) && candidates.push(match);
58
+ }
59
+ } else {
51
60
  let traverse2 = function(node) {
52
61
  if (isFocusable(node, {
53
62
  skipNegativeTabIndexCheck,
@@ -55,23 +64,16 @@ function getFocusables(container = document.body, options = {}) {
55
64
  }) || include?.(node)) {
56
65
  candidates.push(node);
57
66
  }
58
- (composed ? getComposedChildren(node) : [...node.children]).map(traverse2);
67
+ (!composed ? [...node.children] : getComposedChildren(node)).map(
68
+ traverse2
69
+ );
59
70
  };
60
- (composed ? getComposedChildren(container) : [...container.children]).map(
71
+ (!composed ? [...container.children] : getComposedChildren(container)).map(
61
72
  traverse2
62
73
  );
63
- } else {
64
- for (const match of container.querySelectorAll(
65
- skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX : FOCUSABLE_SELECTOR
66
- )) {
67
- match && isFocusable(match, {
68
- skipNegativeTabIndexCheck,
69
- skipVisibilityCheck
70
- }) && candidates.push(match);
71
- }
72
74
  }
73
75
  return normalizeRadioGroup(
74
- sortByTabIndex(filter ? candidates.filter(filter) : candidates)
76
+ sortByTabIndex(!filter ? candidates : candidates.filter(filter))
75
77
  );
76
78
  }
77
79
  function getNextFocusable(container = document.body, options = {}) {
@@ -93,7 +95,7 @@ function isFocusable(element, options = {}) {
93
95
  return false;
94
96
  }
95
97
  if (!element.matches(
96
- skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX : FOCUSABLE_SELECTOR
98
+ !skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
97
99
  )) {
98
100
  return false;
99
101
  }
@@ -237,9 +239,8 @@ function containsComposed(container, element) {
237
239
  while (current) {
238
240
  if (current === container) {
239
241
  return true;
240
- } else {
241
- current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
242
242
  }
243
+ current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
243
244
  }
244
245
  return false;
245
246
  }
@@ -278,8 +279,7 @@ function isDisabled(element) {
278
279
  return "disabled" in element && !!element.disabled;
279
280
  }
280
281
  function isFormControl(element) {
281
- const name = element.tagName;
282
- return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
282
+ return ["BUTTON", "INPUT", "SELECT", "TEXTAREA"].includes(element.tagName);
283
283
  }
284
284
  function isInert(element) {
285
285
  return "inert" in element && !!element.inert;
@@ -425,15 +425,15 @@ var Portal = class {
425
425
  this.#update();
426
426
  const first = [...this.#focusables][0];
427
427
  first ? focusElement(first) : this.#moveFocus("next");
428
- } else {
429
- if (this.#host.contains(previous)) {
430
- this.#moveFocus("next");
431
- return;
432
- }
433
- this.#update();
434
- const last = [...this.#focusables].at(-1);
435
- last ? focusElement(last) : this.#moveFocus("previous");
428
+ return;
429
+ }
430
+ if (this.#host.contains(previous)) {
431
+ this.#moveFocus("next");
432
+ return;
436
433
  }
434
+ this.#update();
435
+ const last = [...this.#focusables].at(-1);
436
+ last ? focusElement(last) : this.#moveFocus("previous");
437
437
  };
438
438
  #onKeyDown = (event) => {
439
439
  if (!(event instanceof KeyboardEvent)) {
@@ -456,10 +456,10 @@ var Portal = class {
456
456
  const focusable = focusables[index + (shiftKey ? -1 : 1)];
457
457
  focusable ? focusElement(focusable) : this.#focusSentinel(shiftKey);
458
458
  }
459
- } else {
460
- event.preventDefault();
461
- this.#moveFocus(shiftKey ? "previous" : "next");
459
+ return;
462
460
  }
461
+ event.preventDefault();
462
+ this.#moveFocus(shiftKey ? "previous" : "next");
463
463
  };
464
464
  #update() {
465
465
  const current = /* @__PURE__ */ new Set([
@@ -516,42 +516,5 @@ var Portal = class {
516
516
  return { noInlineStyle };
517
517
  }
518
518
  };
519
- /**
520
- * Portal
521
- * Lightweight DOM portal (teleport) utility with fully focus management.
522
- * Designed for accessible dialogs, menus, overlays, popovers.
523
- *
524
- * @version 1.4.1
525
- * @author Yusuke Kamiyamane
526
- * @license MIT
527
- * @copyright Copyright (c) Yusuke Kamiyamane
528
- * @see {@link https://github.com/y14e/portal}
529
- */
530
- /*! Bundled license information:
531
-
532
- @y14e/attribute-utils/dist/index.js:
533
- (**
534
- * Attribute Utils
535
- *
536
- * @version 2.0.12
537
- * @author Yusuke Kamiyamane
538
- * @license MIT
539
- * @copyright Copyright (c) Yusuke Kamiyamane
540
- * @see {@link https://github.com/y14e/attribute-utils}
541
- *)
542
-
543
- power-focusable/dist/index.js:
544
- (**
545
- * Power Focusable
546
- * High-precision focus management utility with full composed tree support.
547
- * Handles complex focus rules including tabindex ordering, radio groups, inert.
548
- *
549
- * @version 4.4.3
550
- * @author Yusuke Kamiyamane
551
- * @license MIT
552
- * @copyright Copyright (c) Yusuke Kamiyamane
553
- * @see {@link https://github.com/y14e/power-focusable}
554
- *)
555
- */
556
519
 
557
520
  exports.createPortal = createPortal;
@@ -45,7 +45,16 @@ function getFocusables(container = document.body, options = {}) {
45
45
  skipVisibilityCheck
46
46
  } = resolveOptions(options);
47
47
  const candidates = [];
48
- if (composed || include) {
48
+ if (!composed && !include) {
49
+ for (const match of container.querySelectorAll(
50
+ skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX : FOCUSABLE_SELECTOR
51
+ )) {
52
+ match && isFocusable(match, {
53
+ skipNegativeTabIndexCheck,
54
+ skipVisibilityCheck
55
+ }) && candidates.push(match);
56
+ }
57
+ } else {
49
58
  let traverse2 = function(node) {
50
59
  if (isFocusable(node, {
51
60
  skipNegativeTabIndexCheck,
@@ -53,23 +62,16 @@ function getFocusables(container = document.body, options = {}) {
53
62
  }) || include?.(node)) {
54
63
  candidates.push(node);
55
64
  }
56
- (composed ? getComposedChildren(node) : [...node.children]).map(traverse2);
65
+ (!composed ? [...node.children] : getComposedChildren(node)).map(
66
+ traverse2
67
+ );
57
68
  };
58
- (composed ? getComposedChildren(container) : [...container.children]).map(
69
+ (!composed ? [...container.children] : getComposedChildren(container)).map(
59
70
  traverse2
60
71
  );
61
- } else {
62
- for (const match of container.querySelectorAll(
63
- skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX : FOCUSABLE_SELECTOR
64
- )) {
65
- match && isFocusable(match, {
66
- skipNegativeTabIndexCheck,
67
- skipVisibilityCheck
68
- }) && candidates.push(match);
69
- }
70
72
  }
71
73
  return normalizeRadioGroup(
72
- sortByTabIndex(filter ? candidates.filter(filter) : candidates)
74
+ sortByTabIndex(!filter ? candidates : candidates.filter(filter))
73
75
  );
74
76
  }
75
77
  function getNextFocusable(container = document.body, options = {}) {
@@ -91,7 +93,7 @@ function isFocusable(element, options = {}) {
91
93
  return false;
92
94
  }
93
95
  if (!element.matches(
94
- skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX : FOCUSABLE_SELECTOR
96
+ !skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
95
97
  )) {
96
98
  return false;
97
99
  }
@@ -235,9 +237,8 @@ function containsComposed(container, element) {
235
237
  while (current) {
236
238
  if (current === container) {
237
239
  return true;
238
- } else {
239
- current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
240
240
  }
241
+ current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
241
242
  }
242
243
  return false;
243
244
  }
@@ -276,8 +277,7 @@ function isDisabled(element) {
276
277
  return "disabled" in element && !!element.disabled;
277
278
  }
278
279
  function isFormControl(element) {
279
- const name = element.tagName;
280
- return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
280
+ return ["BUTTON", "INPUT", "SELECT", "TEXTAREA"].includes(element.tagName);
281
281
  }
282
282
  function isInert(element) {
283
283
  return "inert" in element && !!element.inert;
@@ -423,15 +423,15 @@ var Portal = class {
423
423
  this.#update();
424
424
  const first = [...this.#focusables][0];
425
425
  first ? focusElement(first) : this.#moveFocus("next");
426
- } else {
427
- if (this.#host.contains(previous)) {
428
- this.#moveFocus("next");
429
- return;
430
- }
431
- this.#update();
432
- const last = [...this.#focusables].at(-1);
433
- last ? focusElement(last) : this.#moveFocus("previous");
426
+ return;
427
+ }
428
+ if (this.#host.contains(previous)) {
429
+ this.#moveFocus("next");
430
+ return;
434
431
  }
432
+ this.#update();
433
+ const last = [...this.#focusables].at(-1);
434
+ last ? focusElement(last) : this.#moveFocus("previous");
435
435
  };
436
436
  #onKeyDown = (event) => {
437
437
  if (!(event instanceof KeyboardEvent)) {
@@ -454,10 +454,10 @@ var Portal = class {
454
454
  const focusable = focusables[index + (shiftKey ? -1 : 1)];
455
455
  focusable ? focusElement(focusable) : this.#focusSentinel(shiftKey);
456
456
  }
457
- } else {
458
- event.preventDefault();
459
- this.#moveFocus(shiftKey ? "previous" : "next");
457
+ return;
460
458
  }
459
+ event.preventDefault();
460
+ this.#moveFocus(shiftKey ? "previous" : "next");
461
461
  };
462
462
  #update() {
463
463
  const current = /* @__PURE__ */ new Set([
@@ -514,42 +514,5 @@ var Portal = class {
514
514
  return { noInlineStyle };
515
515
  }
516
516
  };
517
- /**
518
- * Portal
519
- * Lightweight DOM portal (teleport) utility with fully focus management.
520
- * Designed for accessible dialogs, menus, overlays, popovers.
521
- *
522
- * @version 1.4.1
523
- * @author Yusuke Kamiyamane
524
- * @license MIT
525
- * @copyright Copyright (c) Yusuke Kamiyamane
526
- * @see {@link https://github.com/y14e/portal}
527
- */
528
- /*! Bundled license information:
529
-
530
- @y14e/attribute-utils/dist/index.js:
531
- (**
532
- * Attribute Utils
533
- *
534
- * @version 2.0.12
535
- * @author Yusuke Kamiyamane
536
- * @license MIT
537
- * @copyright Copyright (c) Yusuke Kamiyamane
538
- * @see {@link https://github.com/y14e/attribute-utils}
539
- *)
540
-
541
- power-focusable/dist/index.js:
542
- (**
543
- * Power Focusable
544
- * High-precision focus management utility with full composed tree support.
545
- * Handles complex focus rules including tabindex ordering, radio groups, inert.
546
- *
547
- * @version 4.4.3
548
- * @author Yusuke Kamiyamane
549
- * @license MIT
550
- * @copyright Copyright (c) Yusuke Kamiyamane
551
- * @see {@link https://github.com/y14e/power-focusable}
552
- *)
553
- */
554
517
 
555
518
  export { createPortal };
package/dist/index.cjs CHANGED
@@ -85,15 +85,15 @@ var Portal = class {
85
85
  this.#update();
86
86
  const first = [...this.#focusables][0];
87
87
  first ? powerFocusable.focusElement(first) : this.#moveFocus("next");
88
- } else {
89
- if (this.#host.contains(previous)) {
90
- this.#moveFocus("next");
91
- return;
92
- }
93
- this.#update();
94
- const last = [...this.#focusables].at(-1);
95
- last ? powerFocusable.focusElement(last) : this.#moveFocus("previous");
88
+ return;
96
89
  }
90
+ if (this.#host.contains(previous)) {
91
+ this.#moveFocus("next");
92
+ return;
93
+ }
94
+ this.#update();
95
+ const last = [...this.#focusables].at(-1);
96
+ last ? powerFocusable.focusElement(last) : this.#moveFocus("previous");
97
97
  };
98
98
  #onKeyDown = (event) => {
99
99
  if (!(event instanceof KeyboardEvent)) {
@@ -116,10 +116,10 @@ var Portal = class {
116
116
  const focusable = focusables[index + (shiftKey ? -1 : 1)];
117
117
  focusable ? powerFocusable.focusElement(focusable) : this.#focusSentinel(shiftKey);
118
118
  }
119
- } else {
120
- event.preventDefault();
121
- this.#moveFocus(shiftKey ? "previous" : "next");
119
+ return;
122
120
  }
121
+ event.preventDefault();
122
+ this.#moveFocus(shiftKey ? "previous" : "next");
123
123
  };
124
124
  #update() {
125
125
  const current = /* @__PURE__ */ new Set([
@@ -176,16 +176,5 @@ var Portal = class {
176
176
  return { noInlineStyle };
177
177
  }
178
178
  };
179
- /**
180
- * Portal
181
- * Lightweight DOM portal (teleport) utility with fully focus management.
182
- * Designed for accessible dialogs, menus, overlays, popovers.
183
- *
184
- * @version 1.4.1
185
- * @author Yusuke Kamiyamane
186
- * @license MIT
187
- * @copyright Copyright (c) Yusuke Kamiyamane
188
- * @see {@link https://github.com/y14e/portal}
189
- */
190
179
 
191
180
  exports.createPortal = createPortal;
package/dist/index.d.cts CHANGED
@@ -1,14 +1,3 @@
1
- /**
2
- * Portal
3
- * Lightweight DOM portal (teleport) utility with fully focus management.
4
- * Designed for accessible dialogs, menus, overlays, popovers.
5
- *
6
- * @version 1.4.1
7
- * @author Yusuke Kamiyamane
8
- * @license MIT
9
- * @copyright Copyright (c) Yusuke Kamiyamane
10
- * @see {@link https://github.com/y14e/portal}
11
- */
12
1
  interface PortalOptions {
13
2
  noInlineStyle: boolean;
14
3
  }
package/dist/index.d.ts CHANGED
@@ -1,14 +1,3 @@
1
- /**
2
- * Portal
3
- * Lightweight DOM portal (teleport) utility with fully focus management.
4
- * Designed for accessible dialogs, menus, overlays, popovers.
5
- *
6
- * @version 1.4.1
7
- * @author Yusuke Kamiyamane
8
- * @license MIT
9
- * @copyright Copyright (c) Yusuke Kamiyamane
10
- * @see {@link https://github.com/y14e/portal}
11
- */
12
1
  interface PortalOptions {
13
2
  noInlineStyle: boolean;
14
3
  }
package/dist/index.js CHANGED
@@ -83,15 +83,15 @@ var Portal = class {
83
83
  this.#update();
84
84
  const first = [...this.#focusables][0];
85
85
  first ? focusElement(first) : this.#moveFocus("next");
86
- } else {
87
- if (this.#host.contains(previous)) {
88
- this.#moveFocus("next");
89
- return;
90
- }
91
- this.#update();
92
- const last = [...this.#focusables].at(-1);
93
- last ? focusElement(last) : this.#moveFocus("previous");
86
+ return;
94
87
  }
88
+ if (this.#host.contains(previous)) {
89
+ this.#moveFocus("next");
90
+ return;
91
+ }
92
+ this.#update();
93
+ const last = [...this.#focusables].at(-1);
94
+ last ? focusElement(last) : this.#moveFocus("previous");
95
95
  };
96
96
  #onKeyDown = (event) => {
97
97
  if (!(event instanceof KeyboardEvent)) {
@@ -114,10 +114,10 @@ var Portal = class {
114
114
  const focusable = focusables[index + (shiftKey ? -1 : 1)];
115
115
  focusable ? focusElement(focusable) : this.#focusSentinel(shiftKey);
116
116
  }
117
- } else {
118
- event.preventDefault();
119
- this.#moveFocus(shiftKey ? "previous" : "next");
117
+ return;
120
118
  }
119
+ event.preventDefault();
120
+ this.#moveFocus(shiftKey ? "previous" : "next");
121
121
  };
122
122
  #update() {
123
123
  const current = /* @__PURE__ */ new Set([
@@ -174,16 +174,5 @@ var Portal = class {
174
174
  return { noInlineStyle };
175
175
  }
176
176
  };
177
- /**
178
- * Portal
179
- * Lightweight DOM portal (teleport) utility with fully focus management.
180
- * Designed for accessible dialogs, menus, overlays, popovers.
181
- *
182
- * @version 1.4.1
183
- * @author Yusuke Kamiyamane
184
- * @license MIT
185
- * @copyright Copyright (c) Yusuke Kamiyamane
186
- * @see {@link https://github.com/y14e/portal}
187
- */
188
177
 
189
178
  export { createPortal };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@y14e/portal",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
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.12",
62
- "power-focusable": "^4.4.3"
61
+ "@y14e/attribute-utils": "^2.0.15",
62
+ "power-focusable": "^4.4.8"
63
63
  }
64
64
  }