@y14e/portal 1.4.5 → 1.4.7

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.
@@ -39,23 +39,23 @@ function containsComposed(container, element) {
39
39
  }
40
40
  return false;
41
41
  }
42
- function getComposedChildren(node) {
43
- if (node instanceof ShadowRoot) {
44
- return [...node.children];
42
+ function getComposedChildren(element) {
43
+ if (element instanceof ShadowRoot) {
44
+ return [...element.children];
45
45
  }
46
- if (!(node instanceof Element)) {
46
+ if (!(element instanceof Element)) {
47
47
  return [];
48
48
  }
49
- if (node instanceof HTMLSlotElement) {
50
- const assigned = node.assignedElements({ flatten: true });
49
+ if (element instanceof HTMLSlotElement) {
50
+ const assigned = element.assignedElements({ flatten: true });
51
51
  if (assigned.length) {
52
52
  return assigned;
53
53
  }
54
54
  }
55
- if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
56
- return [...node.shadowRoot.children];
55
+ if (element instanceof HTMLElement && element.shadowRoot?.mode === "open") {
56
+ return [...element.shadowRoot.children];
57
57
  }
58
- return [...node.children];
58
+ return [...element.children];
59
59
  }
60
60
  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"])';
61
61
  var FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX = FOCUSABLE_SELECTOR.replace(
@@ -201,10 +201,8 @@ function isDisabledDeep(element) {
201
201
  if (isInert(current)) {
202
202
  return true;
203
203
  }
204
- if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
205
- if (!current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
206
- return true;
207
- }
204
+ if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current) && !current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
205
+ return true;
208
206
  }
209
207
  current = current.parentNode;
210
208
  }
@@ -213,10 +211,7 @@ function isDisabledDeep(element) {
213
211
  function normalizeRadioGroup(elements) {
214
212
  let map = null;
215
213
  for (const element of elements) {
216
- if (!(element instanceof HTMLInputElement)) {
217
- continue;
218
- }
219
- if (!isUngroupedRadio(element)) {
214
+ if (!(element instanceof HTMLInputElement) || !isUngroupedRadio(element)) {
220
215
  continue;
221
216
  }
222
217
  if (!map) {
@@ -235,7 +230,7 @@ function normalizeRadioGroup(elements) {
235
230
  (radios.get(name) ?? radios.set(name, []).get(name))?.push(element);
236
231
  }
237
232
  if (!map) {
238
- return elements;
233
+ return elements.slice();
239
234
  }
240
235
  const placeholder = /* @__PURE__ */ new Set();
241
236
  for (const v of map.values()) {
@@ -246,20 +241,12 @@ function normalizeRadioGroup(elements) {
246
241
  }
247
242
  }
248
243
  }
249
- return elements.filter((element) => {
250
- if (!(element instanceof HTMLInputElement)) {
251
- return true;
252
- }
253
- return !isUngroupedRadio(element) || placeholder.has(element);
254
- });
244
+ return elements.filter(
245
+ (e) => !(e instanceof HTMLInputElement) || !isUngroupedRadio(e) || placeholder.has(e)
246
+ );
255
247
  }
256
248
  function sortByTabIndex(elements) {
257
- const ordered = [];
258
- const natural = [];
259
- for (const element of elements) {
260
- (getTabIndex(element) > 0 ? ordered : natural).push(element);
261
- }
262
- return ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b)).concat(natural);
249
+ return elements.filter((e) => getTabIndex(e) > 0).sort((a, b) => getTabIndex(a) - getTabIndex(b)).concat(elements.filter((e) => getTabIndex(e) <= 0));
263
250
  }
264
251
  function getFocusables(container = document.body, options = {}) {
265
252
  if (!(container instanceof Element)) {
@@ -275,11 +262,11 @@ function getFocusables(container = document.body, options = {}) {
275
262
  match && isFocusable(match, { skipNegativeTabIndexCheck, skipVisibilityCheck }) && candidates.push(match);
276
263
  }
277
264
  } else {
278
- let traverse2 = function(node) {
279
- if (isFocusable(node, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(node)) {
280
- candidates.push(node);
265
+ let traverse2 = function(element) {
266
+ if (isFocusable(element, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(element)) {
267
+ candidates.push(element);
281
268
  }
282
- (!composed ? [...node.children] : getComposedChildren(node)).map(traverse2);
269
+ (!composed ? [...element.children] : getComposedChildren(element)).map(traverse2);
283
270
  };
284
271
  (!composed ? [...container.children] : getComposedChildren(container)).map(traverse2);
285
272
  }
@@ -409,23 +396,26 @@ var Portal = class {
409
396
  }
410
397
  if (target === this.#entranceSentinel) {
411
398
  if (this.#host.contains(previous)) {
412
- this.#moveFocus("previous");
399
+ this.#moveFocus(true);
413
400
  return;
414
401
  }
415
402
  this.#update();
416
403
  const first = [...this.#focusables][0];
417
- first ? focusElement(first) : this.#moveFocus("next");
404
+ first ? focusElement(first) : this.#moveFocus(false);
418
405
  return;
419
406
  }
420
407
  if (this.#host.contains(previous)) {
421
- this.#moveFocus("next");
408
+ this.#moveFocus(false);
422
409
  return;
423
410
  }
424
411
  this.#update();
425
412
  const last = [...this.#focusables].at(-1);
426
- last ? focusElement(last) : this.#moveFocus("previous");
413
+ last ? focusElement(last) : this.#moveFocus(true);
427
414
  };
428
415
  #onKeyDown = (event) => {
416
+ if (!(event instanceof KeyboardEvent)) {
417
+ return;
418
+ }
429
419
  const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
430
420
  if (key !== "Tab" || altKey || ctrlKey || metaKey) {
431
421
  return;
@@ -446,7 +436,7 @@ var Portal = class {
446
436
  return;
447
437
  }
448
438
  event.preventDefault();
449
- this.#moveFocus(shiftKey ? "previous" : "next");
439
+ this.#moveFocus(shiftKey);
450
440
  };
451
441
  #update() {
452
442
  const current = /* @__PURE__ */ new Set([
@@ -486,12 +476,11 @@ var Portal = class {
486
476
  include: (element) => this.#focusables.has(element)
487
477
  });
488
478
  }
489
- #moveFocus(direction) {
490
- const options = {
491
- anchor: direction === "previous" ? this.#entranceSentinel : this.#exitSentinel,
479
+ #moveFocus(isPrevious) {
480
+ const focusable = (isPrevious ? getPreviousFocusable : getNextFocusable)(document.body, {
481
+ anchor: isPrevious ? this.#entranceSentinel : this.#exitSentinel,
492
482
  composed: true
493
- };
494
- const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
483
+ });
495
484
  focusable && focusElement(focusable);
496
485
  }
497
486
  };
@@ -37,23 +37,23 @@ function containsComposed(container, element) {
37
37
  }
38
38
  return false;
39
39
  }
40
- function getComposedChildren(node) {
41
- if (node instanceof ShadowRoot) {
42
- return [...node.children];
40
+ function getComposedChildren(element) {
41
+ if (element instanceof ShadowRoot) {
42
+ return [...element.children];
43
43
  }
44
- if (!(node instanceof Element)) {
44
+ if (!(element instanceof Element)) {
45
45
  return [];
46
46
  }
47
- if (node instanceof HTMLSlotElement) {
48
- const assigned = node.assignedElements({ flatten: true });
47
+ if (element instanceof HTMLSlotElement) {
48
+ const assigned = element.assignedElements({ flatten: true });
49
49
  if (assigned.length) {
50
50
  return assigned;
51
51
  }
52
52
  }
53
- if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
54
- return [...node.shadowRoot.children];
53
+ if (element instanceof HTMLElement && element.shadowRoot?.mode === "open") {
54
+ return [...element.shadowRoot.children];
55
55
  }
56
- return [...node.children];
56
+ return [...element.children];
57
57
  }
58
58
  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"])';
59
59
  var FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX = FOCUSABLE_SELECTOR.replace(
@@ -199,10 +199,8 @@ function isDisabledDeep(element) {
199
199
  if (isInert(current)) {
200
200
  return true;
201
201
  }
202
- if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
203
- if (!current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
204
- return true;
205
- }
202
+ if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current) && !current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
203
+ return true;
206
204
  }
207
205
  current = current.parentNode;
208
206
  }
@@ -211,10 +209,7 @@ function isDisabledDeep(element) {
211
209
  function normalizeRadioGroup(elements) {
212
210
  let map = null;
213
211
  for (const element of elements) {
214
- if (!(element instanceof HTMLInputElement)) {
215
- continue;
216
- }
217
- if (!isUngroupedRadio(element)) {
212
+ if (!(element instanceof HTMLInputElement) || !isUngroupedRadio(element)) {
218
213
  continue;
219
214
  }
220
215
  if (!map) {
@@ -233,7 +228,7 @@ function normalizeRadioGroup(elements) {
233
228
  (radios.get(name) ?? radios.set(name, []).get(name))?.push(element);
234
229
  }
235
230
  if (!map) {
236
- return elements;
231
+ return elements.slice();
237
232
  }
238
233
  const placeholder = /* @__PURE__ */ new Set();
239
234
  for (const v of map.values()) {
@@ -244,20 +239,12 @@ function normalizeRadioGroup(elements) {
244
239
  }
245
240
  }
246
241
  }
247
- return elements.filter((element) => {
248
- if (!(element instanceof HTMLInputElement)) {
249
- return true;
250
- }
251
- return !isUngroupedRadio(element) || placeholder.has(element);
252
- });
242
+ return elements.filter(
243
+ (e) => !(e instanceof HTMLInputElement) || !isUngroupedRadio(e) || placeholder.has(e)
244
+ );
253
245
  }
254
246
  function sortByTabIndex(elements) {
255
- const ordered = [];
256
- const natural = [];
257
- for (const element of elements) {
258
- (getTabIndex(element) > 0 ? ordered : natural).push(element);
259
- }
260
- return ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b)).concat(natural);
247
+ return elements.filter((e) => getTabIndex(e) > 0).sort((a, b) => getTabIndex(a) - getTabIndex(b)).concat(elements.filter((e) => getTabIndex(e) <= 0));
261
248
  }
262
249
  function getFocusables(container = document.body, options = {}) {
263
250
  if (!(container instanceof Element)) {
@@ -273,11 +260,11 @@ function getFocusables(container = document.body, options = {}) {
273
260
  match && isFocusable(match, { skipNegativeTabIndexCheck, skipVisibilityCheck }) && candidates.push(match);
274
261
  }
275
262
  } else {
276
- let traverse2 = function(node) {
277
- if (isFocusable(node, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(node)) {
278
- candidates.push(node);
263
+ let traverse2 = function(element) {
264
+ if (isFocusable(element, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(element)) {
265
+ candidates.push(element);
279
266
  }
280
- (!composed ? [...node.children] : getComposedChildren(node)).map(traverse2);
267
+ (!composed ? [...element.children] : getComposedChildren(element)).map(traverse2);
281
268
  };
282
269
  (!composed ? [...container.children] : getComposedChildren(container)).map(traverse2);
283
270
  }
@@ -407,23 +394,26 @@ var Portal = class {
407
394
  }
408
395
  if (target === this.#entranceSentinel) {
409
396
  if (this.#host.contains(previous)) {
410
- this.#moveFocus("previous");
397
+ this.#moveFocus(true);
411
398
  return;
412
399
  }
413
400
  this.#update();
414
401
  const first = [...this.#focusables][0];
415
- first ? focusElement(first) : this.#moveFocus("next");
402
+ first ? focusElement(first) : this.#moveFocus(false);
416
403
  return;
417
404
  }
418
405
  if (this.#host.contains(previous)) {
419
- this.#moveFocus("next");
406
+ this.#moveFocus(false);
420
407
  return;
421
408
  }
422
409
  this.#update();
423
410
  const last = [...this.#focusables].at(-1);
424
- last ? focusElement(last) : this.#moveFocus("previous");
411
+ last ? focusElement(last) : this.#moveFocus(true);
425
412
  };
426
413
  #onKeyDown = (event) => {
414
+ if (!(event instanceof KeyboardEvent)) {
415
+ return;
416
+ }
427
417
  const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
428
418
  if (key !== "Tab" || altKey || ctrlKey || metaKey) {
429
419
  return;
@@ -444,7 +434,7 @@ var Portal = class {
444
434
  return;
445
435
  }
446
436
  event.preventDefault();
447
- this.#moveFocus(shiftKey ? "previous" : "next");
437
+ this.#moveFocus(shiftKey);
448
438
  };
449
439
  #update() {
450
440
  const current = /* @__PURE__ */ new Set([
@@ -484,12 +474,11 @@ var Portal = class {
484
474
  include: (element) => this.#focusables.has(element)
485
475
  });
486
476
  }
487
- #moveFocus(direction) {
488
- const options = {
489
- anchor: direction === "previous" ? this.#entranceSentinel : this.#exitSentinel,
477
+ #moveFocus(isPrevious) {
478
+ const focusable = (isPrevious ? getPreviousFocusable : getNextFocusable)(document.body, {
479
+ anchor: isPrevious ? this.#entranceSentinel : this.#exitSentinel,
490
480
  composed: true
491
- };
492
- const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
481
+ });
493
482
  focusable && focusElement(focusable);
494
483
  }
495
484
  };
package/dist/index.cjs CHANGED
@@ -93,23 +93,26 @@ var Portal = class {
93
93
  }
94
94
  if (target === this.#entranceSentinel) {
95
95
  if (this.#host.contains(previous)) {
96
- this.#moveFocus("previous");
96
+ this.#moveFocus(true);
97
97
  return;
98
98
  }
99
99
  this.#update();
100
100
  const first = [...this.#focusables][0];
101
- first ? powerFocusable.focusElement(first) : this.#moveFocus("next");
101
+ first ? powerFocusable.focusElement(first) : this.#moveFocus(false);
102
102
  return;
103
103
  }
104
104
  if (this.#host.contains(previous)) {
105
- this.#moveFocus("next");
105
+ this.#moveFocus(false);
106
106
  return;
107
107
  }
108
108
  this.#update();
109
109
  const last = [...this.#focusables].at(-1);
110
- last ? powerFocusable.focusElement(last) : this.#moveFocus("previous");
110
+ last ? powerFocusable.focusElement(last) : this.#moveFocus(true);
111
111
  };
112
112
  #onKeyDown = (event) => {
113
+ if (!(event instanceof KeyboardEvent)) {
114
+ return;
115
+ }
113
116
  const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
114
117
  if (key !== "Tab" || altKey || ctrlKey || metaKey) {
115
118
  return;
@@ -130,7 +133,7 @@ var Portal = class {
130
133
  return;
131
134
  }
132
135
  event.preventDefault();
133
- this.#moveFocus(shiftKey ? "previous" : "next");
136
+ this.#moveFocus(shiftKey);
134
137
  };
135
138
  #update() {
136
139
  const current = /* @__PURE__ */ new Set([
@@ -170,12 +173,11 @@ var Portal = class {
170
173
  include: (element) => this.#focusables.has(element)
171
174
  });
172
175
  }
173
- #moveFocus(direction) {
174
- const options = {
175
- anchor: direction === "previous" ? this.#entranceSentinel : this.#exitSentinel,
176
+ #moveFocus(isPrevious) {
177
+ const focusable = (isPrevious ? powerFocusable.getPreviousFocusable : powerFocusable.getNextFocusable)(document.body, {
178
+ anchor: isPrevious ? this.#entranceSentinel : this.#exitSentinel,
176
179
  composed: true
177
- };
178
- const focusable = direction === "previous" ? powerFocusable.getPreviousFocusable(document.body, options) : powerFocusable.getNextFocusable(document.body, options);
180
+ });
179
181
  focusable && powerFocusable.focusElement(focusable);
180
182
  }
181
183
  };
package/dist/index.js CHANGED
@@ -91,23 +91,26 @@ var Portal = class {
91
91
  }
92
92
  if (target === this.#entranceSentinel) {
93
93
  if (this.#host.contains(previous)) {
94
- this.#moveFocus("previous");
94
+ this.#moveFocus(true);
95
95
  return;
96
96
  }
97
97
  this.#update();
98
98
  const first = [...this.#focusables][0];
99
- first ? focusElement(first) : this.#moveFocus("next");
99
+ first ? focusElement(first) : this.#moveFocus(false);
100
100
  return;
101
101
  }
102
102
  if (this.#host.contains(previous)) {
103
- this.#moveFocus("next");
103
+ this.#moveFocus(false);
104
104
  return;
105
105
  }
106
106
  this.#update();
107
107
  const last = [...this.#focusables].at(-1);
108
- last ? focusElement(last) : this.#moveFocus("previous");
108
+ last ? focusElement(last) : this.#moveFocus(true);
109
109
  };
110
110
  #onKeyDown = (event) => {
111
+ if (!(event instanceof KeyboardEvent)) {
112
+ return;
113
+ }
111
114
  const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
112
115
  if (key !== "Tab" || altKey || ctrlKey || metaKey) {
113
116
  return;
@@ -128,7 +131,7 @@ var Portal = class {
128
131
  return;
129
132
  }
130
133
  event.preventDefault();
131
- this.#moveFocus(shiftKey ? "previous" : "next");
134
+ this.#moveFocus(shiftKey);
132
135
  };
133
136
  #update() {
134
137
  const current = /* @__PURE__ */ new Set([
@@ -168,12 +171,11 @@ var Portal = class {
168
171
  include: (element) => this.#focusables.has(element)
169
172
  });
170
173
  }
171
- #moveFocus(direction) {
172
- const options = {
173
- anchor: direction === "previous" ? this.#entranceSentinel : this.#exitSentinel,
174
+ #moveFocus(isPrevious) {
175
+ const focusable = (isPrevious ? getPreviousFocusable : getNextFocusable)(document.body, {
176
+ anchor: isPrevious ? this.#entranceSentinel : this.#exitSentinel,
174
177
  composed: true
175
- };
176
- const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
178
+ });
177
179
  focusable && focusElement(focusable);
178
180
  }
179
181
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@y14e/portal",
3
- "version": "1.4.5",
3
+ "version": "1.4.7",
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.18",
62
- "power-focusable": "^4.5.6"
61
+ "@y14e/attribute-utils": "^2.0.21",
62
+ "power-focusable": "^4.5.12"
63
63
  }
64
64
  }