@y14e/portal 1.4.4 → 1.4.5

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.
@@ -29,87 +29,114 @@ function saveAttributes(element_or_elements, name_or_names) {
29
29
  }
30
30
 
31
31
  // node_modules/power-focusable/dist/index.js
32
+ function containsComposed(container, element) {
33
+ let current = element;
34
+ while (current) {
35
+ if (current === container) {
36
+ return true;
37
+ }
38
+ current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
39
+ }
40
+ return false;
41
+ }
42
+ function getComposedChildren(node) {
43
+ if (node instanceof ShadowRoot) {
44
+ return [...node.children];
45
+ }
46
+ if (!(node instanceof Element)) {
47
+ return [];
48
+ }
49
+ if (node instanceof HTMLSlotElement) {
50
+ const assigned = node.assignedElements({ flatten: true });
51
+ if (assigned.length) {
52
+ return assigned;
53
+ }
54
+ }
55
+ if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
56
+ return [...node.shadowRoot.children];
57
+ }
58
+ return [...node.children];
59
+ }
32
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"])';
33
61
  var FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX = FOCUSABLE_SELECTOR.replace(
34
62
  /(,\s*)?\[tabindex="-1"\]/g,
35
63
  ""
36
64
  );
37
- function getFocusables(container = document.body, options = {}) {
38
- if (!(container instanceof Element)) {
39
- console.warn("Invalid container element. Fallback: <body> element.");
40
- container = document.body;
65
+ function focusElement(element) {
66
+ "focus" in element && typeof element.focus === "function" && element.focus();
67
+ }
68
+ function getActiveElement() {
69
+ let current = document.activeElement;
70
+ while (current?.shadowRoot?.activeElement) {
71
+ current = current.shadowRoot.activeElement;
41
72
  }
42
- const {
43
- composed,
73
+ return current;
74
+ }
75
+ function getTabIndex(element) {
76
+ return "tabIndex" in element ? Number(element.tabIndex) : -1;
77
+ }
78
+ function isDisabled(element) {
79
+ return "disabled" in element && !!element.disabled;
80
+ }
81
+ function isFormControl(element) {
82
+ return ["BUTTON", "INPUT", "SELECT", "TEXTAREA"].includes(element.tagName);
83
+ }
84
+ function isInert(element) {
85
+ return "inert" in element && !!element.inert;
86
+ }
87
+ function isUngroupedRadio(input) {
88
+ return input.type === "radio" && !!input.name;
89
+ }
90
+ function resolveOptions(options) {
91
+ let {
92
+ anchor = getActiveElement(),
93
+ composed = false,
44
94
  filter,
45
95
  include,
46
- skipNegativeTabIndexCheck,
47
- skipVisibilityCheck
48
- } = resolveOptions(options);
49
- const candidates = [];
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);
96
+ skipNegativeTabIndexCheck = false,
97
+ skipVisibilityCheck = false,
98
+ wrap = false
99
+ } = options;
100
+ if (!(anchor instanceof Element)) {
101
+ const active = getActiveElement();
102
+ if (active instanceof Element) {
103
+ console.warn("Invalid anchor element. Fallback: active element.");
104
+ anchor = active;
58
105
  }
59
- } else {
60
- let traverse2 = function(node) {
61
- if (isFocusable(node, {
62
- skipNegativeTabIndexCheck,
63
- skipVisibilityCheck
64
- }) || include?.(node)) {
65
- candidates.push(node);
66
- }
67
- (!composed ? [...node.children] : getComposedChildren(node)).map(
68
- traverse2
69
- );
70
- };
71
- (!composed ? [...container.children] : getComposedChildren(container)).map(
72
- traverse2
73
- );
74
106
  }
75
- return normalizeRadioGroup(
76
- sortByTabIndex(!filter ? candidates : candidates.filter(filter))
77
- );
78
- }
79
- function getNextFocusable(container = document.body, options = {}) {
80
- return getRelativeFocusable(container, 1, options);
81
- }
82
- function getPreviousFocusable(container = document.body, options = {}) {
83
- return getRelativeFocusable(container, -1, options);
84
- }
85
- function isFocusable(element, options = {}) {
86
- if (!(element instanceof Element)) {
87
- console.warn("Invalid element");
88
- return false;
107
+ if (typeof composed !== "boolean") {
108
+ console.warn("Invalid composed option. Fallback: false.");
109
+ composed = false;
89
110
  }
90
- const { skipNegativeTabIndexCheck, skipVisibilityCheck } = resolveOptions(options);
91
- if (element.hasAttribute("hidden") || isInert(element)) {
92
- return false;
111
+ if (filter !== void 0 && typeof filter !== "function") {
112
+ console.warn("Invalid filter function. Fallback: no filter function (undefined).");
113
+ filter = void 0;
93
114
  }
94
- if (!skipNegativeTabIndexCheck && getTabIndex(element) < 0) {
95
- return false;
115
+ if (include !== void 0 && typeof include !== "function") {
116
+ console.warn("Invalid include function. Fallback: no include function (undefined).");
117
+ include = void 0;
96
118
  }
97
- if (!element.matches(
98
- !skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
99
- )) {
100
- return false;
119
+ if (typeof skipNegativeTabIndexCheck !== "boolean") {
120
+ console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
121
+ skipNegativeTabIndexCheck = false;
101
122
  }
102
- if (isDisabledDeep(element)) {
103
- return false;
123
+ if (typeof skipVisibilityCheck !== "boolean") {
124
+ console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
125
+ skipVisibilityCheck = false;
104
126
  }
105
- if (!skipVisibilityCheck && !element.checkVisibility({
106
- contentVisibilityAuto: true,
107
- opacityProperty: true,
108
- visibilityProperty: true
109
- })) {
110
- return false;
127
+ if (typeof wrap !== "boolean") {
128
+ console.warn("Invalid wrap option. Fallback: false.");
129
+ wrap = false;
111
130
  }
112
- return true;
131
+ return {
132
+ anchor,
133
+ composed,
134
+ filter,
135
+ include,
136
+ skipNegativeTabIndexCheck,
137
+ skipVisibilityCheck,
138
+ wrap
139
+ };
113
140
  }
114
141
  function getRelativeFocusable(container, offset, options) {
115
142
  if (!(container instanceof Element)) {
@@ -234,117 +261,80 @@ function sortByTabIndex(elements) {
234
261
  }
235
262
  return ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b)).concat(natural);
236
263
  }
237
- function containsComposed(container, element) {
238
- let current = element;
239
- while (current) {
240
- if (current === container) {
241
- return true;
242
- }
243
- current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
244
- }
245
- return false;
246
- }
247
- function getComposedChildren(node) {
248
- if (node instanceof ShadowRoot) {
249
- return [...node.children];
250
- }
251
- if (!(node instanceof Element)) {
252
- return [];
264
+ function getFocusables(container = document.body, options = {}) {
265
+ if (!(container instanceof Element)) {
266
+ console.warn("Invalid container element. Fallback: <body> element.");
267
+ container = document.body;
253
268
  }
254
- if (node instanceof HTMLSlotElement) {
255
- const assigned = node.assignedElements({ flatten: true });
256
- if (assigned.length) {
257
- return assigned;
269
+ const { composed, filter, include, skipNegativeTabIndexCheck, skipVisibilityCheck } = resolveOptions(options);
270
+ const candidates = [];
271
+ if (!composed && !include) {
272
+ for (const match of container.querySelectorAll(
273
+ !skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
274
+ )) {
275
+ match && isFocusable(match, { skipNegativeTabIndexCheck, skipVisibilityCheck }) && candidates.push(match);
258
276
  }
277
+ } else {
278
+ let traverse2 = function(node) {
279
+ if (isFocusable(node, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(node)) {
280
+ candidates.push(node);
281
+ }
282
+ (!composed ? [...node.children] : getComposedChildren(node)).map(traverse2);
283
+ };
284
+ (!composed ? [...container.children] : getComposedChildren(container)).map(traverse2);
259
285
  }
260
- if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
261
- return [...node.shadowRoot.children];
262
- }
263
- return [...node.children];
264
- }
265
- function focusElement(element) {
266
- "focus" in element && typeof element.focus === "function" && element.focus();
267
- }
268
- function getActiveElement() {
269
- let current = document.activeElement;
270
- while (current?.shadowRoot?.activeElement) {
271
- current = current.shadowRoot.activeElement;
272
- }
273
- return current;
274
- }
275
- function getTabIndex(element) {
276
- return "tabIndex" in element ? Number(element.tabIndex) : 0;
286
+ return normalizeRadioGroup(sortByTabIndex(!filter ? candidates : candidates.filter(filter)));
277
287
  }
278
- function isDisabled(element) {
279
- return "disabled" in element && !!element.disabled;
280
- }
281
- function isFormControl(element) {
282
- return ["BUTTON", "INPUT", "SELECT", "TEXTAREA"].includes(element.tagName);
283
- }
284
- function isInert(element) {
285
- return "inert" in element && !!element.inert;
288
+ function getNextFocusable(container = document.body, options = {}) {
289
+ return getRelativeFocusable(container, 1, options);
286
290
  }
287
- function isUngroupedRadio(element) {
288
- return element.type === "radio" && !!element.name;
291
+ function getPreviousFocusable(container = document.body, options = {}) {
292
+ return getRelativeFocusable(container, -1, options);
289
293
  }
290
- function resolveOptions(options) {
291
- let {
292
- anchor = getActiveElement(),
293
- composed = false,
294
- filter,
295
- include,
296
- skipNegativeTabIndexCheck = false,
297
- skipVisibilityCheck = false,
298
- wrap = false
299
- } = options;
300
- if (!(anchor instanceof Element)) {
301
- const active = getActiveElement();
302
- if (active instanceof Element) {
303
- console.warn("Invalid anchor element. Fallback: active element.");
304
- anchor = active;
305
- }
306
- }
307
- if (typeof composed !== "boolean") {
308
- console.warn("Invalid composed option. Fallback: false.");
309
- composed = false;
294
+ function isFocusable(element, options = {}) {
295
+ if (!(element instanceof Element)) {
296
+ console.warn("Invalid element");
297
+ return false;
310
298
  }
311
- if (filter !== void 0 && typeof filter !== "function") {
312
- console.warn(
313
- "Invalid filter function. Fallback: no filter function (undefined)."
314
- );
315
- filter = void 0;
299
+ const { skipNegativeTabIndexCheck, skipVisibilityCheck } = resolveOptions(options);
300
+ if (element.hasAttribute("hidden") || isInert(element)) {
301
+ return false;
316
302
  }
317
- if (include !== void 0 && typeof include !== "function") {
318
- console.warn(
319
- "Invalid include function. Fallback: no include function (undefined)."
320
- );
321
- include = void 0;
303
+ if (!skipNegativeTabIndexCheck && getTabIndex(element) < 0) {
304
+ return false;
322
305
  }
323
- if (typeof skipNegativeTabIndexCheck !== "boolean") {
324
- console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
325
- skipNegativeTabIndexCheck = false;
306
+ if (!element.matches(
307
+ !skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
308
+ )) {
309
+ return false;
326
310
  }
327
- if (typeof skipVisibilityCheck !== "boolean") {
328
- console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
329
- skipVisibilityCheck = false;
311
+ if (isDisabledDeep(element)) {
312
+ return false;
330
313
  }
331
- if (typeof wrap !== "boolean") {
332
- console.warn("Invalid wrap option. Fallback: false.");
333
- wrap = false;
314
+ if (!skipVisibilityCheck && !element.checkVisibility({
315
+ contentVisibilityAuto: true,
316
+ opacityProperty: true,
317
+ visibilityProperty: true
318
+ })) {
319
+ return false;
334
320
  }
335
- return {
336
- anchor,
337
- composed,
338
- filter,
339
- include,
340
- skipNegativeTabIndexCheck,
341
- skipVisibilityCheck,
342
- wrap
343
- };
321
+ return true;
344
322
  }
345
323
 
346
- // src/index.ts
324
+ // src/constants.ts
347
325
  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;";
326
+
327
+ // src/options.ts
328
+ function resolveOptions2(options) {
329
+ let { noInlineStyle = false } = options;
330
+ if (typeof noInlineStyle !== "boolean") {
331
+ console.warn("Invalid noInlineStyle option. Fallback: false.");
332
+ noInlineStyle = false;
333
+ }
334
+ return { noInlineStyle };
335
+ }
336
+
337
+ // src/index.ts
348
338
  function createPortal(host, container = document.body, options = {}) {
349
339
  if (!(host instanceof Element)) {
350
340
  console.warn("Invalid host element");
@@ -380,7 +370,7 @@ var Portal = class {
380
370
  container = document.body;
381
371
  }
382
372
  this.#container = container;
383
- this.#settings = this.#resolveOptions(options);
373
+ this.#settings = resolveOptions2(options);
384
374
  this.#entranceSentinel = this.#createSentinel();
385
375
  this.#exitSentinel = this.#createSentinel();
386
376
  this.#initialize();
@@ -409,17 +399,15 @@ var Portal = class {
409
399
  for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
410
400
  sentinel.addEventListener("focus", this.#onFocus, { signal });
411
401
  }
412
- this.#host.addEventListener("keydown", this.#onKeyDown, {
413
- signal
414
- });
402
+ this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
415
403
  this.#host.setAttribute("data-portaled", "");
416
404
  }
417
405
  #onFocus = (event) => {
418
- const { currentTarget: sentinel, relatedTarget: previous } = event;
406
+ const { currentTarget: target, relatedTarget: previous } = event;
419
407
  if (!(previous instanceof Element)) {
420
408
  return;
421
409
  }
422
- if (sentinel === this.#entranceSentinel) {
410
+ if (target === this.#entranceSentinel) {
423
411
  if (this.#host.contains(previous)) {
424
412
  this.#moveFocus("previous");
425
413
  return;
@@ -506,14 +494,6 @@ var Portal = class {
506
494
  const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
507
495
  focusable && focusElement(focusable);
508
496
  }
509
- #resolveOptions(options) {
510
- let { noInlineStyle = false } = options;
511
- if (typeof noInlineStyle !== "boolean") {
512
- console.warn("Invalid noInlineStyle option. Fallback: false.");
513
- noInlineStyle = false;
514
- }
515
- return { noInlineStyle };
516
- }
517
497
  };
518
498
 
519
499
  exports.createPortal = createPortal;
@@ -27,87 +27,114 @@ function saveAttributes(element_or_elements, name_or_names) {
27
27
  }
28
28
 
29
29
  // node_modules/power-focusable/dist/index.js
30
+ function containsComposed(container, element) {
31
+ let current = element;
32
+ while (current) {
33
+ if (current === container) {
34
+ return true;
35
+ }
36
+ current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
37
+ }
38
+ return false;
39
+ }
40
+ function getComposedChildren(node) {
41
+ if (node instanceof ShadowRoot) {
42
+ return [...node.children];
43
+ }
44
+ if (!(node instanceof Element)) {
45
+ return [];
46
+ }
47
+ if (node instanceof HTMLSlotElement) {
48
+ const assigned = node.assignedElements({ flatten: true });
49
+ if (assigned.length) {
50
+ return assigned;
51
+ }
52
+ }
53
+ if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
54
+ return [...node.shadowRoot.children];
55
+ }
56
+ return [...node.children];
57
+ }
30
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"])';
31
59
  var FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX = FOCUSABLE_SELECTOR.replace(
32
60
  /(,\s*)?\[tabindex="-1"\]/g,
33
61
  ""
34
62
  );
35
- function getFocusables(container = document.body, options = {}) {
36
- if (!(container instanceof Element)) {
37
- console.warn("Invalid container element. Fallback: <body> element.");
38
- container = document.body;
63
+ function focusElement(element) {
64
+ "focus" in element && typeof element.focus === "function" && element.focus();
65
+ }
66
+ function getActiveElement() {
67
+ let current = document.activeElement;
68
+ while (current?.shadowRoot?.activeElement) {
69
+ current = current.shadowRoot.activeElement;
39
70
  }
40
- const {
41
- composed,
71
+ return current;
72
+ }
73
+ function getTabIndex(element) {
74
+ return "tabIndex" in element ? Number(element.tabIndex) : -1;
75
+ }
76
+ function isDisabled(element) {
77
+ return "disabled" in element && !!element.disabled;
78
+ }
79
+ function isFormControl(element) {
80
+ return ["BUTTON", "INPUT", "SELECT", "TEXTAREA"].includes(element.tagName);
81
+ }
82
+ function isInert(element) {
83
+ return "inert" in element && !!element.inert;
84
+ }
85
+ function isUngroupedRadio(input) {
86
+ return input.type === "radio" && !!input.name;
87
+ }
88
+ function resolveOptions(options) {
89
+ let {
90
+ anchor = getActiveElement(),
91
+ composed = false,
42
92
  filter,
43
93
  include,
44
- skipNegativeTabIndexCheck,
45
- skipVisibilityCheck
46
- } = resolveOptions(options);
47
- const candidates = [];
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);
94
+ skipNegativeTabIndexCheck = false,
95
+ skipVisibilityCheck = false,
96
+ wrap = false
97
+ } = options;
98
+ if (!(anchor instanceof Element)) {
99
+ const active = getActiveElement();
100
+ if (active instanceof Element) {
101
+ console.warn("Invalid anchor element. Fallback: active element.");
102
+ anchor = active;
56
103
  }
57
- } else {
58
- let traverse2 = function(node) {
59
- if (isFocusable(node, {
60
- skipNegativeTabIndexCheck,
61
- skipVisibilityCheck
62
- }) || include?.(node)) {
63
- candidates.push(node);
64
- }
65
- (!composed ? [...node.children] : getComposedChildren(node)).map(
66
- traverse2
67
- );
68
- };
69
- (!composed ? [...container.children] : getComposedChildren(container)).map(
70
- traverse2
71
- );
72
104
  }
73
- return normalizeRadioGroup(
74
- sortByTabIndex(!filter ? candidates : candidates.filter(filter))
75
- );
76
- }
77
- function getNextFocusable(container = document.body, options = {}) {
78
- return getRelativeFocusable(container, 1, options);
79
- }
80
- function getPreviousFocusable(container = document.body, options = {}) {
81
- return getRelativeFocusable(container, -1, options);
82
- }
83
- function isFocusable(element, options = {}) {
84
- if (!(element instanceof Element)) {
85
- console.warn("Invalid element");
86
- return false;
105
+ if (typeof composed !== "boolean") {
106
+ console.warn("Invalid composed option. Fallback: false.");
107
+ composed = false;
87
108
  }
88
- const { skipNegativeTabIndexCheck, skipVisibilityCheck } = resolveOptions(options);
89
- if (element.hasAttribute("hidden") || isInert(element)) {
90
- return false;
109
+ if (filter !== void 0 && typeof filter !== "function") {
110
+ console.warn("Invalid filter function. Fallback: no filter function (undefined).");
111
+ filter = void 0;
91
112
  }
92
- if (!skipNegativeTabIndexCheck && getTabIndex(element) < 0) {
93
- return false;
113
+ if (include !== void 0 && typeof include !== "function") {
114
+ console.warn("Invalid include function. Fallback: no include function (undefined).");
115
+ include = void 0;
94
116
  }
95
- if (!element.matches(
96
- !skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
97
- )) {
98
- return false;
117
+ if (typeof skipNegativeTabIndexCheck !== "boolean") {
118
+ console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
119
+ skipNegativeTabIndexCheck = false;
99
120
  }
100
- if (isDisabledDeep(element)) {
101
- return false;
121
+ if (typeof skipVisibilityCheck !== "boolean") {
122
+ console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
123
+ skipVisibilityCheck = false;
102
124
  }
103
- if (!skipVisibilityCheck && !element.checkVisibility({
104
- contentVisibilityAuto: true,
105
- opacityProperty: true,
106
- visibilityProperty: true
107
- })) {
108
- return false;
125
+ if (typeof wrap !== "boolean") {
126
+ console.warn("Invalid wrap option. Fallback: false.");
127
+ wrap = false;
109
128
  }
110
- return true;
129
+ return {
130
+ anchor,
131
+ composed,
132
+ filter,
133
+ include,
134
+ skipNegativeTabIndexCheck,
135
+ skipVisibilityCheck,
136
+ wrap
137
+ };
111
138
  }
112
139
  function getRelativeFocusable(container, offset, options) {
113
140
  if (!(container instanceof Element)) {
@@ -232,117 +259,80 @@ function sortByTabIndex(elements) {
232
259
  }
233
260
  return ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b)).concat(natural);
234
261
  }
235
- function containsComposed(container, element) {
236
- let current = element;
237
- while (current) {
238
- if (current === container) {
239
- return true;
240
- }
241
- current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
242
- }
243
- return false;
244
- }
245
- function getComposedChildren(node) {
246
- if (node instanceof ShadowRoot) {
247
- return [...node.children];
248
- }
249
- if (!(node instanceof Element)) {
250
- return [];
262
+ function getFocusables(container = document.body, options = {}) {
263
+ if (!(container instanceof Element)) {
264
+ console.warn("Invalid container element. Fallback: <body> element.");
265
+ container = document.body;
251
266
  }
252
- if (node instanceof HTMLSlotElement) {
253
- const assigned = node.assignedElements({ flatten: true });
254
- if (assigned.length) {
255
- return assigned;
267
+ const { composed, filter, include, skipNegativeTabIndexCheck, skipVisibilityCheck } = resolveOptions(options);
268
+ const candidates = [];
269
+ if (!composed && !include) {
270
+ for (const match of container.querySelectorAll(
271
+ !skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
272
+ )) {
273
+ match && isFocusable(match, { skipNegativeTabIndexCheck, skipVisibilityCheck }) && candidates.push(match);
256
274
  }
275
+ } else {
276
+ let traverse2 = function(node) {
277
+ if (isFocusable(node, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(node)) {
278
+ candidates.push(node);
279
+ }
280
+ (!composed ? [...node.children] : getComposedChildren(node)).map(traverse2);
281
+ };
282
+ (!composed ? [...container.children] : getComposedChildren(container)).map(traverse2);
257
283
  }
258
- if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
259
- return [...node.shadowRoot.children];
260
- }
261
- return [...node.children];
262
- }
263
- function focusElement(element) {
264
- "focus" in element && typeof element.focus === "function" && element.focus();
265
- }
266
- function getActiveElement() {
267
- let current = document.activeElement;
268
- while (current?.shadowRoot?.activeElement) {
269
- current = current.shadowRoot.activeElement;
270
- }
271
- return current;
272
- }
273
- function getTabIndex(element) {
274
- return "tabIndex" in element ? Number(element.tabIndex) : 0;
284
+ return normalizeRadioGroup(sortByTabIndex(!filter ? candidates : candidates.filter(filter)));
275
285
  }
276
- function isDisabled(element) {
277
- return "disabled" in element && !!element.disabled;
278
- }
279
- function isFormControl(element) {
280
- return ["BUTTON", "INPUT", "SELECT", "TEXTAREA"].includes(element.tagName);
281
- }
282
- function isInert(element) {
283
- return "inert" in element && !!element.inert;
286
+ function getNextFocusable(container = document.body, options = {}) {
287
+ return getRelativeFocusable(container, 1, options);
284
288
  }
285
- function isUngroupedRadio(element) {
286
- return element.type === "radio" && !!element.name;
289
+ function getPreviousFocusable(container = document.body, options = {}) {
290
+ return getRelativeFocusable(container, -1, options);
287
291
  }
288
- function resolveOptions(options) {
289
- let {
290
- anchor = getActiveElement(),
291
- composed = false,
292
- filter,
293
- include,
294
- skipNegativeTabIndexCheck = false,
295
- skipVisibilityCheck = false,
296
- wrap = false
297
- } = options;
298
- if (!(anchor instanceof Element)) {
299
- const active = getActiveElement();
300
- if (active instanceof Element) {
301
- console.warn("Invalid anchor element. Fallback: active element.");
302
- anchor = active;
303
- }
304
- }
305
- if (typeof composed !== "boolean") {
306
- console.warn("Invalid composed option. Fallback: false.");
307
- composed = false;
292
+ function isFocusable(element, options = {}) {
293
+ if (!(element instanceof Element)) {
294
+ console.warn("Invalid element");
295
+ return false;
308
296
  }
309
- if (filter !== void 0 && typeof filter !== "function") {
310
- console.warn(
311
- "Invalid filter function. Fallback: no filter function (undefined)."
312
- );
313
- filter = void 0;
297
+ const { skipNegativeTabIndexCheck, skipVisibilityCheck } = resolveOptions(options);
298
+ if (element.hasAttribute("hidden") || isInert(element)) {
299
+ return false;
314
300
  }
315
- if (include !== void 0 && typeof include !== "function") {
316
- console.warn(
317
- "Invalid include function. Fallback: no include function (undefined)."
318
- );
319
- include = void 0;
301
+ if (!skipNegativeTabIndexCheck && getTabIndex(element) < 0) {
302
+ return false;
320
303
  }
321
- if (typeof skipNegativeTabIndexCheck !== "boolean") {
322
- console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
323
- skipNegativeTabIndexCheck = false;
304
+ if (!element.matches(
305
+ !skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR : FOCUSABLE_SELECTOR_WITH_NEGATIVE_TABINDEX
306
+ )) {
307
+ return false;
324
308
  }
325
- if (typeof skipVisibilityCheck !== "boolean") {
326
- console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
327
- skipVisibilityCheck = false;
309
+ if (isDisabledDeep(element)) {
310
+ return false;
328
311
  }
329
- if (typeof wrap !== "boolean") {
330
- console.warn("Invalid wrap option. Fallback: false.");
331
- wrap = false;
312
+ if (!skipVisibilityCheck && !element.checkVisibility({
313
+ contentVisibilityAuto: true,
314
+ opacityProperty: true,
315
+ visibilityProperty: true
316
+ })) {
317
+ return false;
332
318
  }
333
- return {
334
- anchor,
335
- composed,
336
- filter,
337
- include,
338
- skipNegativeTabIndexCheck,
339
- skipVisibilityCheck,
340
- wrap
341
- };
319
+ return true;
342
320
  }
343
321
 
344
- // src/index.ts
322
+ // src/constants.ts
345
323
  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;";
324
+
325
+ // src/options.ts
326
+ function resolveOptions2(options) {
327
+ let { noInlineStyle = false } = options;
328
+ if (typeof noInlineStyle !== "boolean") {
329
+ console.warn("Invalid noInlineStyle option. Fallback: false.");
330
+ noInlineStyle = false;
331
+ }
332
+ return { noInlineStyle };
333
+ }
334
+
335
+ // src/index.ts
346
336
  function createPortal(host, container = document.body, options = {}) {
347
337
  if (!(host instanceof Element)) {
348
338
  console.warn("Invalid host element");
@@ -378,7 +368,7 @@ var Portal = class {
378
368
  container = document.body;
379
369
  }
380
370
  this.#container = container;
381
- this.#settings = this.#resolveOptions(options);
371
+ this.#settings = resolveOptions2(options);
382
372
  this.#entranceSentinel = this.#createSentinel();
383
373
  this.#exitSentinel = this.#createSentinel();
384
374
  this.#initialize();
@@ -407,17 +397,15 @@ var Portal = class {
407
397
  for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
408
398
  sentinel.addEventListener("focus", this.#onFocus, { signal });
409
399
  }
410
- this.#host.addEventListener("keydown", this.#onKeyDown, {
411
- signal
412
- });
400
+ this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
413
401
  this.#host.setAttribute("data-portaled", "");
414
402
  }
415
403
  #onFocus = (event) => {
416
- const { currentTarget: sentinel, relatedTarget: previous } = event;
404
+ const { currentTarget: target, relatedTarget: previous } = event;
417
405
  if (!(previous instanceof Element)) {
418
406
  return;
419
407
  }
420
- if (sentinel === this.#entranceSentinel) {
408
+ if (target === this.#entranceSentinel) {
421
409
  if (this.#host.contains(previous)) {
422
410
  this.#moveFocus("previous");
423
411
  return;
@@ -504,14 +492,6 @@ var Portal = class {
504
492
  const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
505
493
  focusable && focusElement(focusable);
506
494
  }
507
- #resolveOptions(options) {
508
- let { noInlineStyle = false } = options;
509
- if (typeof noInlineStyle !== "boolean") {
510
- console.warn("Invalid noInlineStyle option. Fallback: false.");
511
- noInlineStyle = false;
512
- }
513
- return { noInlineStyle };
514
- }
515
495
  };
516
496
 
517
497
  export { createPortal };
package/dist/index.cjs CHANGED
@@ -4,7 +4,21 @@ var attributeUtils = require('@y14e/attribute-utils');
4
4
  var powerFocusable = require('power-focusable');
5
5
 
6
6
  // src/index.ts
7
+
8
+ // src/constants.ts
7
9
  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;";
10
+
11
+ // src/options.ts
12
+ function resolveOptions(options) {
13
+ let { noInlineStyle = false } = options;
14
+ if (typeof noInlineStyle !== "boolean") {
15
+ console.warn("Invalid noInlineStyle option. Fallback: false.");
16
+ noInlineStyle = false;
17
+ }
18
+ return { noInlineStyle };
19
+ }
20
+
21
+ // src/index.ts
8
22
  function createPortal(host, container = document.body, options = {}) {
9
23
  if (!(host instanceof Element)) {
10
24
  console.warn("Invalid host element");
@@ -40,7 +54,7 @@ var Portal = class {
40
54
  container = document.body;
41
55
  }
42
56
  this.#container = container;
43
- this.#settings = this.#resolveOptions(options);
57
+ this.#settings = resolveOptions(options);
44
58
  this.#entranceSentinel = this.#createSentinel();
45
59
  this.#exitSentinel = this.#createSentinel();
46
60
  this.#initialize();
@@ -69,17 +83,15 @@ var Portal = class {
69
83
  for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
70
84
  sentinel.addEventListener("focus", this.#onFocus, { signal });
71
85
  }
72
- this.#host.addEventListener("keydown", this.#onKeyDown, {
73
- signal
74
- });
86
+ this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
75
87
  this.#host.setAttribute("data-portaled", "");
76
88
  }
77
89
  #onFocus = (event) => {
78
- const { currentTarget: sentinel, relatedTarget: previous } = event;
90
+ const { currentTarget: target, relatedTarget: previous } = event;
79
91
  if (!(previous instanceof Element)) {
80
92
  return;
81
93
  }
82
- if (sentinel === this.#entranceSentinel) {
94
+ if (target === this.#entranceSentinel) {
83
95
  if (this.#host.contains(previous)) {
84
96
  this.#moveFocus("previous");
85
97
  return;
@@ -166,14 +178,6 @@ var Portal = class {
166
178
  const focusable = direction === "previous" ? powerFocusable.getPreviousFocusable(document.body, options) : powerFocusable.getNextFocusable(document.body, options);
167
179
  focusable && powerFocusable.focusElement(focusable);
168
180
  }
169
- #resolveOptions(options) {
170
- let { noInlineStyle = false } = options;
171
- if (typeof noInlineStyle !== "boolean") {
172
- console.warn("Invalid noInlineStyle option. Fallback: false.");
173
- noInlineStyle = false;
174
- }
175
- return { noInlineStyle };
176
- }
177
181
  };
178
182
 
179
183
  exports.createPortal = createPortal;
package/dist/index.d.cts CHANGED
@@ -1,6 +1,7 @@
1
1
  interface PortalOptions {
2
2
  noInlineStyle: boolean;
3
3
  }
4
+
4
5
  declare function createPortal(host: Element, container?: HTMLElement, options?: Partial<PortalOptions>): () => void;
5
6
 
6
7
  export { type PortalOptions, createPortal };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  interface PortalOptions {
2
2
  noInlineStyle: boolean;
3
3
  }
4
+
4
5
  declare function createPortal(host: Element, container?: HTMLElement, options?: Partial<PortalOptions>): () => void;
5
6
 
6
7
  export { type PortalOptions, createPortal };
package/dist/index.js CHANGED
@@ -2,7 +2,21 @@ import { restoreAttributes, saveAttributes } from '@y14e/attribute-utils';
2
2
  import { containsComposed, focusElement, getActiveElement, getFocusables, getPreviousFocusable, getNextFocusable } from 'power-focusable';
3
3
 
4
4
  // src/index.ts
5
+
6
+ // src/constants.ts
5
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;";
8
+
9
+ // src/options.ts
10
+ function resolveOptions(options) {
11
+ let { noInlineStyle = false } = options;
12
+ if (typeof noInlineStyle !== "boolean") {
13
+ console.warn("Invalid noInlineStyle option. Fallback: false.");
14
+ noInlineStyle = false;
15
+ }
16
+ return { noInlineStyle };
17
+ }
18
+
19
+ // src/index.ts
6
20
  function createPortal(host, container = document.body, options = {}) {
7
21
  if (!(host instanceof Element)) {
8
22
  console.warn("Invalid host element");
@@ -38,7 +52,7 @@ var Portal = class {
38
52
  container = document.body;
39
53
  }
40
54
  this.#container = container;
41
- this.#settings = this.#resolveOptions(options);
55
+ this.#settings = resolveOptions(options);
42
56
  this.#entranceSentinel = this.#createSentinel();
43
57
  this.#exitSentinel = this.#createSentinel();
44
58
  this.#initialize();
@@ -67,17 +81,15 @@ var Portal = class {
67
81
  for (const sentinel of [this.#entranceSentinel, this.#exitSentinel]) {
68
82
  sentinel.addEventListener("focus", this.#onFocus, { signal });
69
83
  }
70
- this.#host.addEventListener("keydown", this.#onKeyDown, {
71
- signal
72
- });
84
+ this.#host.addEventListener("keydown", this.#onKeyDown, { signal });
73
85
  this.#host.setAttribute("data-portaled", "");
74
86
  }
75
87
  #onFocus = (event) => {
76
- const { currentTarget: sentinel, relatedTarget: previous } = event;
88
+ const { currentTarget: target, relatedTarget: previous } = event;
77
89
  if (!(previous instanceof Element)) {
78
90
  return;
79
91
  }
80
- if (sentinel === this.#entranceSentinel) {
92
+ if (target === this.#entranceSentinel) {
81
93
  if (this.#host.contains(previous)) {
82
94
  this.#moveFocus("previous");
83
95
  return;
@@ -164,14 +176,6 @@ var Portal = class {
164
176
  const focusable = direction === "previous" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
165
177
  focusable && focusElement(focusable);
166
178
  }
167
- #resolveOptions(options) {
168
- let { noInlineStyle = false } = options;
169
- if (typeof noInlineStyle !== "boolean") {
170
- console.warn("Invalid noInlineStyle option. Fallback: false.");
171
- noInlineStyle = false;
172
- }
173
- return { noInlineStyle };
174
- }
175
179
  };
176
180
 
177
181
  export { createPortal };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@y14e/portal",
3
- "version": "1.4.4",
3
+ "version": "1.4.5",
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.15",
62
- "power-focusable": "^4.4.10"
61
+ "@y14e/attribute-utils": "^2.0.18",
62
+ "power-focusable": "^4.5.6"
63
63
  }
64
64
  }