diff --git a/frontend/src/v2/lib/overlays/RDialog/RDialog.vue b/frontend/src/v2/lib/overlays/RDialog/RDialog.vue index 43bd6f657..964e93110 100644 --- a/frontend/src/v2/lib/overlays/RDialog/RDialog.vue +++ b/frontend/src/v2/lib/overlays/RDialog/RDialog.vue @@ -17,6 +17,7 @@ // REmptyState / RProgressCircular / RSpinner as needed. import { computed, nextTick, onBeforeUnmount, ref, useSlots, watch } from "vue"; import RIcon from "@/v2/lib/primitives/RIcon/RIcon.vue"; +import { createBodyScrollLock, overlayCount } from "../bodyScrollLock"; import { type EscapableEntry, popEscapable, @@ -67,10 +68,12 @@ const panelRef = ref(null); // Element that had focus before the dialog opened — focus returns here // when the dialog closes so keyboard users don't lose their place. let previouslyFocused: HTMLElement | null = null; -// Guards this instance so lock/unlock are idempotent -let holdsLock = false; -// Stack depth at the moment this dialog opened (number of OTHER dialogs +// Shared, reference-counted body scroll lock (see bodyScrollLock.ts). +const { lock: lockBodyScroll, unlock: unlockBodyScroll } = + createBodyScrollLock(); + +// Stack depth at the moment this dialog opened (number of overlays // already open). Drives a per-instance z-index bump so a dialog opened // from inside another dialog always paints above its parent surface, // regardless of Teleport mount order. Reset to 0 on close. @@ -105,45 +108,15 @@ const stackEntry: EscapableEntry = { }, }; -// Body scroll lock, reference-counted so nested dialogs unlock -// correctly when the outer one is still open. -function lockBodyScroll() { - if (holdsLock) return; - holdsLock = true; - const cur = Number(document.body.dataset.rDialogOpenCount ?? "0") + 1; - document.body.dataset.rDialogOpenCount = String(cur); - if (cur === 1) { - // Remember whatever overflow was already in effect (e.g. a gallery - // view that locks the body for its whole lifetime) so we restore it - // on unlock instead of clobbering it to "". - document.body.dataset.rDialogPrevOverflow = document.body.style.overflow; - document.body.style.overflow = "hidden"; - } -} -function unlockBodyScroll() { - if (!holdsLock) return; - holdsLock = false; - const cur = Math.max( - 0, - Number(document.body.dataset.rDialogOpenCount ?? "0") - 1, - ); - document.body.dataset.rDialogOpenCount = String(cur); - if (cur === 0) { - document.body.style.overflow = - document.body.dataset.rDialogPrevOverflow ?? ""; - delete document.body.dataset.rDialogPrevOverflow; - } -} - watch( () => props.modelValue, (open) => { if (open) { previouslyFocused = document.activeElement as HTMLElement | null; - // Snapshot the open-dialog count BEFORE incrementing it via + // Snapshot the open-overlay count BEFORE incrementing it via // lockBodyScroll(), so the first dialog reads depth 0 and any // subsequent dialog sees the previous ones and stacks on top. - stackDepth.value = Number(document.body.dataset.rDialogOpenCount ?? "0"); + stackDepth.value = overlayCount(); lockBodyScroll(); pushEscapable(stackEntry); // Defer to the next tick so the panel is mounted before we diff --git a/frontend/src/v2/lib/overlays/RDrawer/RDrawer.vue b/frontend/src/v2/lib/overlays/RDrawer/RDrawer.vue index 2e10ec066..c1842ddf3 100644 --- a/frontend/src/v2/lib/overlays/RDrawer/RDrawer.vue +++ b/frontend/src/v2/lib/overlays/RDrawer/RDrawer.vue @@ -24,6 +24,7 @@ import { popEscapable, pushEscapable, } from "../RDialog/escapeStack.js"; +import { createBodyScrollLock } from "../bodyScrollLock"; defineOptions({ inheritAttrs: false }); @@ -66,8 +67,11 @@ const panelRef = ref(null); // Element that had focus before the drawer opened — focus returns here // when the drawer closes so keyboard users don't lose their place. let previouslyFocused: HTMLElement | null = null; -// Guards this instance so lock/unlock are idempotent -let holdsLock = false; + +// Shared, reference-counted body scroll lock (see bodyScrollLock.ts) so +// stacking (a Dialog open over a Drawer) unlocks in the right order. +const { lock: lockBodyScroll, unlock: unlockBodyScroll } = + createBodyScrollLock(); function closeDrawer() { emit("update:modelValue", false); @@ -79,37 +83,6 @@ function onScrimClick() { closeDrawer(); } -// Body scroll lock, same reference-count pattern RDialog uses so -// stacking (a Dialog open over a Drawer) unlocks in the right order. -function lockBodyScroll() { - if (holdsLock) return; - holdsLock = true; - const cur = Number(document.body.dataset.rOverlayOpenCount ?? "0") + 1; - document.body.dataset.rOverlayOpenCount = String(cur); - if (cur === 1) { - // Remember whatever overflow was already in effect — a host view may - // lock the body for its whole lifetime (e.g. the gallery shell, whose - // only scrollbar is the virtualizer's). Restoring this on unlock - // instead of forcing "" keeps that lock intact after the drawer closes. - document.body.dataset.rOverlayPrevOverflow = document.body.style.overflow; - document.body.style.overflow = "hidden"; - } -} -function unlockBodyScroll() { - if (!holdsLock) return; - holdsLock = false; - const cur = Math.max( - 0, - Number(document.body.dataset.rOverlayOpenCount ?? "0") - 1, - ); - document.body.dataset.rOverlayOpenCount = String(cur); - if (cur === 0) { - document.body.style.overflow = - document.body.dataset.rOverlayPrevOverflow ?? ""; - delete document.body.dataset.rOverlayPrevOverflow; - } -} - // Shared escape-stack entry — register on open so a single global // listener handles Esc across menus, dialogs, drawers, and so // `useGamepad`'s B-back closes the topmost overlay first. `persistent` diff --git a/frontend/src/v2/lib/overlays/bodyScrollLock.spec.ts b/frontend/src/v2/lib/overlays/bodyScrollLock.spec.ts new file mode 100644 index 000000000..32dfde96c --- /dev/null +++ b/frontend/src/v2/lib/overlays/bodyScrollLock.spec.ts @@ -0,0 +1,62 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { createBodyScrollLock, overlayCount } from "./bodyScrollLock"; + +afterEach(() => { + document.body.style.overflow = ""; + delete document.body.dataset.rOverlayOpenCount; + delete document.body.dataset.rOverlayPrevOverflow; +}); + +describe("createBodyScrollLock", () => { + it("locks the body on the first overlay and restores on the last", () => { + const { lock, unlock } = createBodyScrollLock(); + lock(); + expect(document.body.style.overflow).toBe("hidden"); + expect(overlayCount()).toBe(1); + unlock(); + expect(document.body.style.overflow).toBe(""); + expect(overlayCount()).toBe(0); + }); + + it("preserves an overflow the host set for its own lifetime", () => { + document.body.style.overflow = "clip"; + const { lock, unlock } = createBodyScrollLock(); + lock(); + expect(document.body.style.overflow).toBe("hidden"); + unlock(); + expect(document.body.style.overflow).toBe("clip"); + }); + + // The bug this module fixes: independent overlays (e.g. a dialog over a + // drawer) must share one count so the body only unlocks once the LAST + // one closes, regardless of close order. + it("keeps the lock while any overlay is still open, in any close order", () => { + const dialog = createBodyScrollLock(); + const drawer = createBodyScrollLock(); + + dialog.lock(); + drawer.lock(); + expect(overlayCount()).toBe(2); + expect(document.body.style.overflow).toBe("hidden"); + + // Close the first-opened overlay first — the body must STAY locked. + dialog.unlock(); + expect(overlayCount()).toBe(1); + expect(document.body.style.overflow).toBe("hidden"); + + drawer.unlock(); + expect(overlayCount()).toBe(0); + expect(document.body.style.overflow).toBe(""); + }); + + it("is idempotent so an unmount safety-net never double-counts", () => { + const { lock, unlock } = createBodyScrollLock(); + lock(); + lock(); // duplicate open + expect(overlayCount()).toBe(1); + unlock(); + unlock(); // watcher close + onBeforeUnmount + expect(overlayCount()).toBe(0); + expect(document.body.style.overflow).toBe(""); + }); +}); diff --git a/frontend/src/v2/lib/overlays/bodyScrollLock.ts b/frontend/src/v2/lib/overlays/bodyScrollLock.ts new file mode 100644 index 000000000..1408d9092 --- /dev/null +++ b/frontend/src/v2/lib/overlays/bodyScrollLock.ts @@ -0,0 +1,55 @@ +// Shared, reference-counted body scroll lock for v2 overlays. +// +// Every overlay that covers the page (RDialog, RDrawer, ...) hides the +// body scrollbar while open. They MUST share a single counter and a +// single saved-overflow value: with per-type counters, a dialog stacked +// over a drawer (or vice versa) would restore `body` overflow off its +// own count and clobber the other's lock — leaving the page scrollable +// while an overlay is still open, or permanently locked after all close. +// +// The first overlay to open snapshots whatever overflow was already in +// effect (e.g. a gallery view that locks the body for its whole +// lifetime) and forces "hidden"; the last one to close restores that +// snapshot instead of clobbering it to "". State lives on +// `document.body.dataset` so it survives across the many independent +// component instances that share the count. + +const COUNT_KEY = "rOverlayOpenCount"; +const PREV_OVERFLOW_KEY = "rOverlayPrevOverflow"; + +/** Number of overlays currently holding the lock. */ +export function overlayCount(): number { + return Number(document.body.dataset[COUNT_KEY] ?? "0"); +} + +// Each caller gets its own idempotent lock/unlock pair: `holdsLock` +// guards against double-counting so an unmount safety net can always +// call unlock() even when the close path already ran (and vice versa). +export function createBodyScrollLock() { + let holdsLock = false; + + function lock(): void { + if (holdsLock) return; + holdsLock = true; + const cur = overlayCount() + 1; + document.body.dataset[COUNT_KEY] = String(cur); + if (cur === 1) { + document.body.dataset[PREV_OVERFLOW_KEY] = document.body.style.overflow; + document.body.style.overflow = "hidden"; + } + } + + function unlock(): void { + if (!holdsLock) return; + holdsLock = false; + const cur = Math.max(0, overlayCount() - 1); + document.body.dataset[COUNT_KEY] = String(cur); + if (cur === 0) { + document.body.style.overflow = + document.body.dataset[PREV_OVERFLOW_KEY] ?? ""; + delete document.body.dataset[PREV_OVERFLOW_KEY]; + } + } + + return { lock, unlock }; +}