@y14e/portal 1.4.2 → 1.4.4

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.2';
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.2/+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.2';
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 : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
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;
@@ -409,7 +409,9 @@ var Portal = class {
409
409
  for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
410
410
  sentinel.addEventListener("focus", this.#onFocus, { signal });
411
411
  }
412
- this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
412
+ this.#host.addEventListener("keydown", this.#onKeyDown, {
413
+ signal
414
+ });
413
415
  this.#host.setAttribute("data-portaled", "");
414
416
  }
415
417
  #onFocus = (event) => {
@@ -425,20 +427,17 @@ var Portal = class {
425
427
  this.#update();
426
428
  const first = [...this.#focusables][0];
427
429
  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");
430
+ return;
436
431
  }
437
- };
438
- #onKeyDown = (event) => {
439
- if (!(event instanceof KeyboardEvent)) {
432
+ if (this.#host.contains(previous)) {
433
+ this.#moveFocus("next");
440
434
  return;
441
435
  }
436
+ this.#update();
437
+ const last = [...this.#focusables].at(-1);
438
+ last ? focusElement(last) : this.#moveFocus("previous");
439
+ };
440
+ #onKeyDown = (event) => {
442
441
  const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
443
442
  if (key !== "Tab" || altKey || ctrlKey || metaKey) {
444
443
  return;
@@ -456,10 +455,10 @@ var Portal = class {
456
455
  const focusable = focusables[index + (shiftKey ? -1 : 1)];
457
456
  focusable ? focusElement(focusable) : this.#focusSentinel(shiftKey);
458
457
  }
459
- } else {
460
- event.preventDefault();
461
- this.#moveFocus(shiftKey ? "previous" : "next");
458
+ return;
462
459
  }
460
+ event.preventDefault();
461
+ this.#moveFocus(shiftKey ? "previous" : "next");
463
462
  };
464
463
  #update() {
465
464
  const current = /* @__PURE__ */ new Set([
@@ -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 : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
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;
@@ -407,7 +407,9 @@ var Portal = class {
407
407
  for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
408
408
  sentinel.addEventListener("focus", this.#onFocus, { signal });
409
409
  }
410
- this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
410
+ this.#host.addEventListener("keydown", this.#onKeyDown, {
411
+ signal
412
+ });
411
413
  this.#host.setAttribute("data-portaled", "");
412
414
  }
413
415
  #onFocus = (event) => {
@@ -423,20 +425,17 @@ var Portal = class {
423
425
  this.#update();
424
426
  const first = [...this.#focusables][0];
425
427
  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");
428
+ return;
434
429
  }
435
- };
436
- #onKeyDown = (event) => {
437
- if (!(event instanceof KeyboardEvent)) {
430
+ if (this.#host.contains(previous)) {
431
+ this.#moveFocus("next");
438
432
  return;
439
433
  }
434
+ this.#update();
435
+ const last = [...this.#focusables].at(-1);
436
+ last ? focusElement(last) : this.#moveFocus("previous");
437
+ };
438
+ #onKeyDown = (event) => {
440
439
  const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
441
440
  if (key !== "Tab" || altKey || ctrlKey || metaKey) {
442
441
  return;
@@ -454,10 +453,10 @@ var Portal = class {
454
453
  const focusable = focusables[index + (shiftKey ? -1 : 1)];
455
454
  focusable ? focusElement(focusable) : this.#focusSentinel(shiftKey);
456
455
  }
457
- } else {
458
- event.preventDefault();
459
- this.#moveFocus(shiftKey ? "previous" : "next");
456
+ return;
460
457
  }
458
+ event.preventDefault();
459
+ this.#moveFocus(shiftKey ? "previous" : "next");
461
460
  };
462
461
  #update() {
463
462
  const current = /* @__PURE__ */ new Set([
package/dist/index.cjs CHANGED
@@ -69,7 +69,9 @@ var Portal = class {
69
69
  for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
70
70
  sentinel.addEventListener("focus", this.#onFocus, { signal });
71
71
  }
72
- this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
72
+ this.#host.addEventListener("keydown", this.#onKeyDown, {
73
+ signal
74
+ });
73
75
  this.#host.setAttribute("data-portaled", "");
74
76
  }
75
77
  #onFocus = (event) => {
@@ -85,20 +87,17 @@ var Portal = class {
85
87
  this.#update();
86
88
  const first = [...this.#focusables][0];
87
89
  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");
90
+ return;
96
91
  }
97
- };
98
- #onKeyDown = (event) => {
99
- if (!(event instanceof KeyboardEvent)) {
92
+ if (this.#host.contains(previous)) {
93
+ this.#moveFocus("next");
100
94
  return;
101
95
  }
96
+ this.#update();
97
+ const last = [...this.#focusables].at(-1);
98
+ last ? powerFocusable.focusElement(last) : this.#moveFocus("previous");
99
+ };
100
+ #onKeyDown = (event) => {
102
101
  const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
103
102
  if (key !== "Tab" || altKey || ctrlKey || metaKey) {
104
103
  return;
@@ -116,10 +115,10 @@ var Portal = class {
116
115
  const focusable = focusables[index + (shiftKey ? -1 : 1)];
117
116
  focusable ? powerFocusable.focusElement(focusable) : this.#focusSentinel(shiftKey);
118
117
  }
119
- } else {
120
- event.preventDefault();
121
- this.#moveFocus(shiftKey ? "previous" : "next");
118
+ return;
122
119
  }
120
+ event.preventDefault();
121
+ this.#moveFocus(shiftKey ? "previous" : "next");
123
122
  };
124
123
  #update() {
125
124
  const current = /* @__PURE__ */ new Set([
package/dist/index.js CHANGED
@@ -67,7 +67,9 @@ var Portal = class {
67
67
  for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
68
68
  sentinel.addEventListener("focus", this.#onFocus, { signal });
69
69
  }
70
- this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
70
+ this.#host.addEventListener("keydown", this.#onKeyDown, {
71
+ signal
72
+ });
71
73
  this.#host.setAttribute("data-portaled", "");
72
74
  }
73
75
  #onFocus = (event) => {
@@ -83,20 +85,17 @@ var Portal = class {
83
85
  this.#update();
84
86
  const first = [...this.#focusables][0];
85
87
  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");
88
+ return;
94
89
  }
95
- };
96
- #onKeyDown = (event) => {
97
- if (!(event instanceof KeyboardEvent)) {
90
+ if (this.#host.contains(previous)) {
91
+ this.#moveFocus("next");
98
92
  return;
99
93
  }
94
+ this.#update();
95
+ const last = [...this.#focusables].at(-1);
96
+ last ? focusElement(last) : this.#moveFocus("previous");
97
+ };
98
+ #onKeyDown = (event) => {
100
99
  const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
101
100
  if (key !== "Tab" || altKey || ctrlKey || metaKey) {
102
101
  return;
@@ -114,10 +113,10 @@ var Portal = class {
114
113
  const focusable = focusables[index + (shiftKey ? -1 : 1)];
115
114
  focusable ? focusElement(focusable) : this.#focusSentinel(shiftKey);
116
115
  }
117
- } else {
118
- event.preventDefault();
119
- this.#moveFocus(shiftKey ? "previous" : "next");
116
+ return;
120
117
  }
118
+ event.preventDefault();
119
+ this.#moveFocus(shiftKey ? "previous" : "next");
121
120
  };
122
121
  #update() {
123
122
  const current = /* @__PURE__ */ new Set([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@y14e/portal",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
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.13",
62
- "power-focusable": "^4.4.4"
61
+ "@y14e/attribute-utils": "^2.0.15",
62
+ "power-focusable": "^4.4.10"
63
63
  }
64
64
  }