@y14e/portal 1.0.2 → 1.0.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/dist/index.cjs CHANGED
@@ -269,14 +269,23 @@ function isUngroupedRadio(element) {
269
269
  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;`;
270
270
  function createPortal(host, container = document.body) {
271
271
  if (!(host instanceof Element)) {
272
- throw new Error("Invalid host element");
272
+ console.warn("Invalid host element");
273
+ return () => {
274
+ };
275
+ }
276
+ if (host.hasAttribute("data-portaled")) {
277
+ console.warn("Already portaled");
278
+ return () => {
279
+ };
273
280
  }
274
281
  if (!(container instanceof Element)) {
275
282
  console.warn("Invalid container element. Fallback: <body> element.");
276
283
  container = document.body;
277
284
  }
278
- if (!containsComposed2(container, host)) {
279
- throw new Error("Mismatch elements");
285
+ if (containsComposed2(host, container)) {
286
+ console.warn("Host element cannot contain the container element");
287
+ return () => {
288
+ };
280
289
  }
281
290
  const portal = new Portal(host, container);
282
291
  return () => portal.destroy();
@@ -317,7 +326,7 @@ var Portal = class {
317
326
  this.#exitSentinel.after(this.#host);
318
327
  this.#entranceSentinel.remove();
319
328
  this.#exitSentinel.remove();
320
- this.#host.removeAttribute("data-portal");
329
+ this.#host.removeAttribute("data-portaled");
321
330
  }
322
331
  #initialize() {
323
332
  this.#host.before(this.#entranceSentinel);
@@ -337,7 +346,7 @@ var Portal = class {
337
346
  capture: true,
338
347
  signal
339
348
  });
340
- this.#host.setAttribute("data-portal", "");
349
+ this.#host.setAttribute("data-portaled", "");
341
350
  }
342
351
  #onFocusIn = (event) => {
343
352
  const current = event.target;
@@ -347,14 +356,14 @@ var Portal = class {
347
356
  }
348
357
  if (current === this.#entranceSentinel) {
349
358
  if (this.#host.contains(before)) {
350
- this.#moveFocusOutside("previous");
359
+ this.#moveFocus("previous");
351
360
  } else {
352
361
  const first = this.#getFocusables()[0];
353
362
  first && focus(first);
354
363
  }
355
364
  } else if (current === this.#exitSentinel) {
356
365
  if (this.#host.contains(before)) {
357
- this.#moveFocusOutside("next");
366
+ this.#moveFocus("next");
358
367
  } else {
359
368
  const last = this.#getFocusables().at(-1);
360
369
  last && focus(last);
@@ -391,6 +400,7 @@ var Portal = class {
391
400
  #createSentinel() {
392
401
  const sentinel = document.createElement("span");
393
402
  sentinel.setAttribute("aria-hidden", "true");
403
+ sentinel.setAttribute("data-portal-sentinel", "");
394
404
  sentinel.setAttribute("tabindex", "0");
395
405
  sentinel.style.cssText += VISUALLY_HIDDEN_CSS;
396
406
  return sentinel;
@@ -401,7 +411,7 @@ var Portal = class {
401
411
  include: (element) => this.#tabIndexes.has(element)
402
412
  });
403
413
  }
404
- #moveFocusOutside(direction) {
414
+ #moveFocus(direction) {
405
415
  const options = {
406
416
  anchor: direction === "previous" ? this.#entranceSentinel : this.#exitSentinel,
407
417
  composed: true
@@ -435,7 +445,7 @@ function getActiveElement2() {
435
445
  * Lightweight DOM portal (teleport) utility with fully focus management.
436
446
  * Designed for accessible dialogs, menus, overlays, popovers.
437
447
  *
438
- * @version 1.0.2
448
+ * @version 1.0.4
439
449
  * @author Yusuke Kamiyamane
440
450
  * @license MIT
441
451
  * @copyright Copyright (c) Yusuke Kamiyamane
package/dist/index.d.cts CHANGED
@@ -3,7 +3,7 @@
3
3
  * Lightweight DOM portal (teleport) utility with fully focus management.
4
4
  * Designed for accessible dialogs, menus, overlays, popovers.
5
5
  *
6
- * @version 1.0.2
6
+ * @version 1.0.4
7
7
  * @author Yusuke Kamiyamane
8
8
  * @license MIT
9
9
  * @copyright Copyright (c) Yusuke Kamiyamane
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * Lightweight DOM portal (teleport) utility with fully focus management.
4
4
  * Designed for accessible dialogs, menus, overlays, popovers.
5
5
  *
6
- * @version 1.0.2
6
+ * @version 1.0.4
7
7
  * @author Yusuke Kamiyamane
8
8
  * @license MIT
9
9
  * @copyright Copyright (c) Yusuke Kamiyamane
package/dist/index.js CHANGED
@@ -267,14 +267,23 @@ function isUngroupedRadio(element) {
267
267
  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;`;
268
268
  function createPortal(host, container = document.body) {
269
269
  if (!(host instanceof Element)) {
270
- throw new Error("Invalid host element");
270
+ console.warn("Invalid host element");
271
+ return () => {
272
+ };
273
+ }
274
+ if (host.hasAttribute("data-portaled")) {
275
+ console.warn("Already portaled");
276
+ return () => {
277
+ };
271
278
  }
272
279
  if (!(container instanceof Element)) {
273
280
  console.warn("Invalid container element. Fallback: <body> element.");
274
281
  container = document.body;
275
282
  }
276
- if (!containsComposed2(container, host)) {
277
- throw new Error("Mismatch elements");
283
+ if (containsComposed2(host, container)) {
284
+ console.warn("Host element cannot contain the container element");
285
+ return () => {
286
+ };
278
287
  }
279
288
  const portal = new Portal(host, container);
280
289
  return () => portal.destroy();
@@ -315,7 +324,7 @@ var Portal = class {
315
324
  this.#exitSentinel.after(this.#host);
316
325
  this.#entranceSentinel.remove();
317
326
  this.#exitSentinel.remove();
318
- this.#host.removeAttribute("data-portal");
327
+ this.#host.removeAttribute("data-portaled");
319
328
  }
320
329
  #initialize() {
321
330
  this.#host.before(this.#entranceSentinel);
@@ -335,7 +344,7 @@ var Portal = class {
335
344
  capture: true,
336
345
  signal
337
346
  });
338
- this.#host.setAttribute("data-portal", "");
347
+ this.#host.setAttribute("data-portaled", "");
339
348
  }
340
349
  #onFocusIn = (event) => {
341
350
  const current = event.target;
@@ -345,14 +354,14 @@ var Portal = class {
345
354
  }
346
355
  if (current === this.#entranceSentinel) {
347
356
  if (this.#host.contains(before)) {
348
- this.#moveFocusOutside("previous");
357
+ this.#moveFocus("previous");
349
358
  } else {
350
359
  const first = this.#getFocusables()[0];
351
360
  first && focus(first);
352
361
  }
353
362
  } else if (current === this.#exitSentinel) {
354
363
  if (this.#host.contains(before)) {
355
- this.#moveFocusOutside("next");
364
+ this.#moveFocus("next");
356
365
  } else {
357
366
  const last = this.#getFocusables().at(-1);
358
367
  last && focus(last);
@@ -389,6 +398,7 @@ var Portal = class {
389
398
  #createSentinel() {
390
399
  const sentinel = document.createElement("span");
391
400
  sentinel.setAttribute("aria-hidden", "true");
401
+ sentinel.setAttribute("data-portal-sentinel", "");
392
402
  sentinel.setAttribute("tabindex", "0");
393
403
  sentinel.style.cssText += VISUALLY_HIDDEN_CSS;
394
404
  return sentinel;
@@ -399,7 +409,7 @@ var Portal = class {
399
409
  include: (element) => this.#tabIndexes.has(element)
400
410
  });
401
411
  }
402
- #moveFocusOutside(direction) {
412
+ #moveFocus(direction) {
403
413
  const options = {
404
414
  anchor: direction === "previous" ? this.#entranceSentinel : this.#exitSentinel,
405
415
  composed: true
@@ -433,7 +443,7 @@ function getActiveElement2() {
433
443
  * Lightweight DOM portal (teleport) utility with fully focus management.
434
444
  * Designed for accessible dialogs, menus, overlays, popovers.
435
445
  *
436
- * @version 1.0.2
446
+ * @version 1.0.4
437
447
  * @author Yusuke Kamiyamane
438
448
  * @license MIT
439
449
  * @copyright Copyright (c) Yusuke Kamiyamane
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@y14e/portal",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Lightweight DOM portal (teleport) utility with fully focus management",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",