@hoangyell/gallery 1.0.6 → 1.0.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/Gallery.astro +52 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hoangyell/gallery",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "Astro image gallery with lightbox, pyramid multi-row layout, and accessible controls.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/Gallery.astro CHANGED
@@ -500,6 +500,13 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
500
500
  currentIndex: number;
501
501
  touchStartX: number;
502
502
  touchStartY: number;
503
+ lockedScrollY: number;
504
+ previousBodyOverflow: string;
505
+ previousBodyPosition: string;
506
+ previousBodyTop: string;
507
+ previousBodyWidth: string;
508
+ previousHtmlOverflow: string;
509
+ previousHtmlScrollBehavior: string;
503
510
 
504
511
  constructor(el: HTMLElement) {
505
512
  this.gallery = el;
@@ -514,6 +521,13 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
514
521
  this.currentIndex = -1;
515
522
  this.touchStartX = 0;
516
523
  this.touchStartY = 0;
524
+ this.lockedScrollY = -1;
525
+ this.previousBodyOverflow = "";
526
+ this.previousBodyPosition = "";
527
+ this.previousBodyTop = "";
528
+ this.previousBodyWidth = "";
529
+ this.previousHtmlOverflow = "";
530
+ this.previousHtmlScrollBehavior = "";
517
531
 
518
532
  this.bind();
519
533
  document.addEventListener("astro:before-swap", this.forceClose);
@@ -539,10 +553,10 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
539
553
 
540
554
  document.removeEventListener("keydown", this.handleKey);
541
555
  document.removeEventListener("gesturestart", this.preventZoom);
542
- document.body.style.overflow = "";
543
556
  const main = document.querySelector("main");
544
557
  if (main) main.removeAttribute("inert");
545
558
  }
559
+ this.unlockBackgroundScroll();
546
560
  document.removeEventListener("astro:before-swap", this.forceClose);
547
561
  };
548
562
 
@@ -561,7 +575,7 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
561
575
  this.preload(index + 1);
562
576
 
563
577
  document.body.appendChild(this.overlay);
564
- document.body.style.overflow = "hidden";
578
+ this.lockBackgroundScroll();
565
579
  const main = document.querySelector("main");
566
580
  if (main) main.setAttribute("inert", "");
567
581
  this.overlay.focus();
@@ -650,7 +664,7 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
650
664
  this.imgEl = null;
651
665
  this.counterEl = null;
652
666
 
653
- document.body.style.overflow = "";
667
+ this.unlockBackgroundScroll();
654
668
  const main = document.querySelector("main");
655
669
  if (main) main.removeAttribute("inert");
656
670
 
@@ -663,6 +677,41 @@ if (resolvedRows <= 1 || galleryItems.length <= 1) {
663
677
  }, 300);
664
678
  };
665
679
 
680
+ lockBackgroundScroll = () => {
681
+ if (this.lockedScrollY >= 0) return;
682
+
683
+ this.lockedScrollY = window.scrollY;
684
+ this.previousBodyOverflow = document.body.style.overflow;
685
+ this.previousBodyPosition = document.body.style.position;
686
+ this.previousBodyTop = document.body.style.top;
687
+ this.previousBodyWidth = document.body.style.width;
688
+ this.previousHtmlOverflow = document.documentElement.style.overflow;
689
+ this.previousHtmlScrollBehavior = document.documentElement.style.scrollBehavior;
690
+
691
+ document.documentElement.style.overflow = "hidden";
692
+ document.body.style.overflow = "hidden";
693
+ document.body.style.position = "fixed";
694
+ document.body.style.top = `-${this.lockedScrollY}px`;
695
+ document.body.style.width = "100%";
696
+ };
697
+
698
+ unlockBackgroundScroll = () => {
699
+ if (this.lockedScrollY < 0) return;
700
+
701
+ document.documentElement.style.overflow = this.previousHtmlOverflow;
702
+ document.body.style.overflow = this.previousBodyOverflow;
703
+ document.body.style.position = this.previousBodyPosition;
704
+ document.body.style.top = this.previousBodyTop;
705
+ document.body.style.width = this.previousBodyWidth;
706
+ document.documentElement.style.scrollBehavior = "auto";
707
+ window.scrollTo({ top: this.lockedScrollY, left: 0, behavior: "auto" });
708
+ requestAnimationFrame(() => {
709
+ document.documentElement.style.scrollBehavior =
710
+ this.previousHtmlScrollBehavior;
711
+ });
712
+ this.lockedScrollY = -1;
713
+ };
714
+
666
715
  navigate = (dir: number) => {
667
716
  const next = this.currentIndex + dir;
668
717
  if (next < 0 || next >= this.items.length) return;