Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): coordinate concurrent useScrollLock instances (#4788)

alex-js-ltd757185e160c8
diff --git a/.changeset/scroll-lock-out-of-order-cleanup.md b/.changeset/scroll-lock-out-of-order-cleanup.md
new file mode 100644
index 0000000000..46c1bef329
--- /dev/null
+++ b/.changeset/scroll-lock-out-of-order-cleanup.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `useScrollLock`: coordinate concurrent locks with a shared counter, so overlays closing out of order no longer unlock the body early or leave it stuck locked.
+
+@alex-js-ltd
diff --git a/packages/core/src/hooks/useScrollLock.test.ts b/packages/core/src/hooks/useScrollLock.test.ts
new file mode 100644
index 0000000000..8ea6bc2ff2
--- /dev/null
+++ b/packages/core/src/hooks/useScrollLock.test.ts
@@ -0,0 +1,72 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file useScrollLock.test.ts
+ * @input Uses vitest, @testing-library/react, useScrollLock hook
+ * @output Unit tests for single and concurrent useScrollLock instances
+ * @position Testing; validates useScrollLock.ts implementation
+ *
+ * SYNC: When useScrollLock.ts changes, update tests to match new behavior
+ */
+
+import {afterEach, describe, expect, it, vi} from 'vitest';
+import {cleanup, renderHook} from '@testing-library/react';
+import {useScrollLock} from './useScrollLock';
+
+describe('useScrollLock', () => {
+ afterEach(() => {
+ cleanup();
+ document.body.style.cssText = '';
+ vi.restoreAllMocks();
+ });
+
+ it('restores body styles and scroll position after a single lock is released', () => {
+ document.body.style.overflow = 'auto';
+ document.body.style.position = 'relative';
+
+ vi.spyOn(window, 'scrollX', 'get').mockReturnValue(120);
+ vi.spyOn(window, 'scrollY', 'get').mockReturnValue(480);
+ vi.spyOn(window, 'scrollTo').mockImplementation(() => {});
+
+ const lock = renderHook(() => useScrollLock(true));
+
+ expect(document.body.style.overflow).toBe('hidden');
+ expect(document.body.style.position).toBe('fixed');
+
+ lock.unmount();
+
+ expect(document.body.style.overflow).toBe('auto');
+ expect(document.body.style.position).toBe('relative');
+ expect(window.scrollTo).toHaveBeenCalledWith(120, 480);
+ });
+
+ it('stays locked while a second overlay is still open, even if the first closes out of order', () => {
+ vi.spyOn(window, 'scrollTo').mockImplementation(() => {});
+
+ const first = renderHook(() => useScrollLock(true));
+ const second = renderHook(() => useScrollLock(true));
+
+ first.unmount();
+
+ expect(document.body.style.position).toBe('fixed');
+ expect(document.body.style.overflow).toBe('hidden');
+
+ second.unmount();
+ });
+
+ it('fully restores the body once every overlay has closed, regardless of close order', () => {
+ vi.spyOn(window, 'scrollTo').mockImplementation(() => {});
+
+ const first = renderHook(() => useScrollLock(true));
+ const second = renderHook(() => useScrollLock(true));
+
+ first.unmount();
+ second.unmount();
+
+ expect(document.body.style.position).toBe('');
+ expect(document.body.style.overflow).toBe('');
+ expect(document.body.style.top).toBe('');
+ expect(document.body.style.left).toBe('');
+ expect(document.body.style.right).toBe('');
+ });
+});
diff --git a/packages/core/src/hooks/useScrollLock.ts b/packages/core/src/hooks/useScrollLock.ts
index a328657bc3..74b1711892 100644
--- a/packages/core/src/hooks/useScrollLock.ts
+++ b/packages/core/src/hooks/useScrollLock.ts
@@ -17,6 +17,19 @@
import {useEffect} from 'react';
+interface ScrollLockSnapshot {
+ scrollX: number;
+ scrollY: number;
+ overflow: string;
+ position: string;
+ top: string;
+ left: string;
+ right: string;
+}
+
+let lockCount = 0;
+let originalBodyState: ScrollLockSnapshot | null = null;
+
/**
* Locks body scroll when `isLocked` is true.
*
@@ -36,28 +49,47 @@ export function useScrollLock(isLocked: boolean): void {
return;
}
- const scrollX = window.scrollX;
- const scrollY = window.scrollY;
const {body} = document;
- const prevOverflow = body.style.overflow;
- const prevPosition = body.style.position;
- const prevTop = body.style.top;
- const prevLeft = body.style.left;
- const prevRight = body.style.right;
-
- body.style.overflow = 'hidden';
- body.style.position = 'fixed';
- body.style.top = `-${scrollY}px`;
- body.style.left = '0';
- body.style.right = '0';
+
+ if (lockCount === 0) {
+ const scrollX = window.scrollX;
+ const scrollY = window.scrollY;
+
+ originalBodyState = {
+ scrollX,
+ scrollY,
+ overflow: body.style.overflow,
+ position: body.style.position,
+ top: body.style.top,
+ left: body.style.left,
+ right: body.style.right,
+ };
+
+ body.style.overflow = 'hidden';
+ body.style.position = 'fixed';
+ body.style.top = `-${scrollY}px`;
+ body.style.left = '0';
+ body.style.right = '0';
+ }
+
+ lockCount += 1;
return () => {
- body.style.overflow = prevOverflow;
- body.style.position = prevPosition;
- body.style.top = prevTop;
- body.style.left = prevLeft;
- body.style.right = prevRight;
- window.scrollTo(scrollX, scrollY);
+ lockCount -= 1;
+
+ if (lockCount !== 0 || originalBodyState == null) {
+ return;
+ }
+
+ const state = originalBodyState;
+ originalBodyState = null;
+
+ body.style.overflow = state.overflow;
+ body.style.position = state.position;
+ body.style.top = state.top;
+ body.style.left = state.left;
+ body.style.right = state.right;
+ window.scrollTo(state.scrollX, state.scrollY);
};
}, [isLocked]);
}