@y14e/portal 1.2.12 → 1.2.14

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
@@ -10,14 +10,14 @@ npm i @y14e/portal
10
10
 
11
11
  ```ts
12
12
  // npm
13
- import { createPortal } from '@y14e/portal@1.2.12';
13
+ import { createPortal } from '@y14e/portal@1.2.14';
14
14
 
15
15
  // CDNs
16
- import { createPortal } from 'https://esm.sh/@y14e/portal@1.2.12';
16
+ import { createPortal } from 'https://esm.sh/@y14e/portal@1.2.14';
17
17
  // or
18
- import { createPortal } from 'https://cdn.jsdelivr.net/npm/@y14e/portal@1.2.12/+esm';
18
+ import { createPortal } from 'https://cdn.jsdelivr.net/npm/@y14e/portal@1.2.14/+esm';
19
19
  // or
20
- import { createPortal } from 'https://esm.unpkg.com/@y14e/portal@1.2.12';
20
+ import { createPortal } from 'https://esm.unpkg.com/@y14e/portal@1.2.14';
21
21
  ```
22
22
 
23
23
  ## 📦 APIs
package/dist/index.cjs CHANGED
@@ -400,6 +400,7 @@ var Portal = class {
400
400
  #exitSentinel;
401
401
  #focusables = /* @__PURE__ */ new Set();
402
402
  #controller = null;
403
+ #timer;
403
404
  #isDestroyed = false;
404
405
  constructor(host, container) {
405
406
  this.#host = host;
@@ -415,6 +416,10 @@ var Portal = class {
415
416
  this.#isDestroyed = true;
416
417
  this.#controller?.abort();
417
418
  this.#controller = null;
419
+ if (this.#timer !== void 0) {
420
+ cancelAnimationFrame(this.#timer);
421
+ this.#timer = void 0;
422
+ }
418
423
  restoreAttributes([...this.#focusables]);
419
424
  this.#focusables.clear();
420
425
  this.#exitSentinel.after(this.#host);
@@ -452,7 +457,15 @@ var Portal = class {
452
457
  }
453
458
  this.#update();
454
459
  const first = [...this.#focusables][0];
455
- first && focusElement(first);
460
+ if (first) {
461
+ focusElement(first);
462
+ } else {
463
+ const next = getNextFocusable(document.body, {
464
+ anchor: this.#exitSentinel,
465
+ composed: true
466
+ });
467
+ next && focusElement(next);
468
+ }
456
469
  return;
457
470
  }
458
471
  if (current === this.#exitSentinel) {
@@ -462,7 +475,15 @@ var Portal = class {
462
475
  }
463
476
  this.#update();
464
477
  const last = [...this.#focusables].at(-1);
465
- last && focusElement(last);
478
+ if (last) {
479
+ focusElement(last);
480
+ } else {
481
+ const previous = getPreviousFocusable(document.body, {
482
+ anchor: this.#entranceSentinel,
483
+ composed: true
484
+ });
485
+ previous && focusElement(previous);
486
+ }
466
487
  return;
467
488
  }
468
489
  };
@@ -523,7 +544,8 @@ var Portal = class {
523
544
  return sentinel;
524
545
  }
525
546
  #focusSentinel(isPrevious) {
526
- requestAnimationFrame(
547
+ this.#timer && cancelAnimationFrame(this.#timer);
548
+ this.#timer = requestAnimationFrame(
527
549
  () => (isPrevious ? this.#entranceSentinel : this.#exitSentinel).focus()
528
550
  );
529
551
  }
@@ -567,7 +589,7 @@ function getActiveElement2() {
567
589
  * Lightweight DOM portal (teleport) utility with fully focus management.
568
590
  * Designed for accessible dialogs, menus, overlays, popovers.
569
591
  *
570
- * @version 1.2.12
592
+ * @version 1.2.14
571
593
  * @author Yusuke Kamiyamane
572
594
  * @license MIT
573
595
  * @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.2.12
6
+ * @version 1.2.14
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.2.12
6
+ * @version 1.2.14
7
7
  * @author Yusuke Kamiyamane
8
8
  * @license MIT
9
9
  * @copyright Copyright (c) Yusuke Kamiyamane
package/dist/index.js CHANGED
@@ -398,6 +398,7 @@ var Portal = class {
398
398
  #exitSentinel;
399
399
  #focusables = /* @__PURE__ */ new Set();
400
400
  #controller = null;
401
+ #timer;
401
402
  #isDestroyed = false;
402
403
  constructor(host, container) {
403
404
  this.#host = host;
@@ -413,6 +414,10 @@ var Portal = class {
413
414
  this.#isDestroyed = true;
414
415
  this.#controller?.abort();
415
416
  this.#controller = null;
417
+ if (this.#timer !== void 0) {
418
+ cancelAnimationFrame(this.#timer);
419
+ this.#timer = void 0;
420
+ }
416
421
  restoreAttributes([...this.#focusables]);
417
422
  this.#focusables.clear();
418
423
  this.#exitSentinel.after(this.#host);
@@ -450,7 +455,15 @@ var Portal = class {
450
455
  }
451
456
  this.#update();
452
457
  const first = [...this.#focusables][0];
453
- first && focusElement(first);
458
+ if (first) {
459
+ focusElement(first);
460
+ } else {
461
+ const next = getNextFocusable(document.body, {
462
+ anchor: this.#exitSentinel,
463
+ composed: true
464
+ });
465
+ next && focusElement(next);
466
+ }
454
467
  return;
455
468
  }
456
469
  if (current === this.#exitSentinel) {
@@ -460,7 +473,15 @@ var Portal = class {
460
473
  }
461
474
  this.#update();
462
475
  const last = [...this.#focusables].at(-1);
463
- last && focusElement(last);
476
+ if (last) {
477
+ focusElement(last);
478
+ } else {
479
+ const previous = getPreviousFocusable(document.body, {
480
+ anchor: this.#entranceSentinel,
481
+ composed: true
482
+ });
483
+ previous && focusElement(previous);
484
+ }
464
485
  return;
465
486
  }
466
487
  };
@@ -521,7 +542,8 @@ var Portal = class {
521
542
  return sentinel;
522
543
  }
523
544
  #focusSentinel(isPrevious) {
524
- requestAnimationFrame(
545
+ this.#timer && cancelAnimationFrame(this.#timer);
546
+ this.#timer = requestAnimationFrame(
525
547
  () => (isPrevious ? this.#entranceSentinel : this.#exitSentinel).focus()
526
548
  );
527
549
  }
@@ -565,7 +587,7 @@ function getActiveElement2() {
565
587
  * Lightweight DOM portal (teleport) utility with fully focus management.
566
588
  * Designed for accessible dialogs, menus, overlays, popovers.
567
589
  *
568
- * @version 1.2.12
590
+ * @version 1.2.14
569
591
  * @author Yusuke Kamiyamane
570
592
  * @license MIT
571
593
  * @copyright Copyright (c) Yusuke Kamiyamane
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@y14e/portal",
3
- "version": "1.2.12",
3
+ "version": "1.2.14",
4
4
  "description": "Lightweight DOM portal (teleport) utility with fully focus management",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",