Skip to content

Commit

Permalink
only account for active instances of useScrollLock hook
Browse files Browse the repository at this point in the history
  • Loading branch information
sirineJ committed Mar 5, 2025
1 parent 957991b commit 3c13a02
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/circuit-ui/hooks/useScrollLock/useScrollLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useCallback, useEffect, useRef } from 'react';

let instanceCount = 0;

export const useScrollLock = (isLocked: boolean): void => {
export const useScrollLock = (isActive: boolean): void => {
const scrollValue = useRef<string>();

const restoreScroll = useCallback(() => {
Expand All @@ -31,18 +31,20 @@ export const useScrollLock = (isLocked: boolean): void => {
}, []);

useEffect(() => {
instanceCount += 1;
if (isActive) {
instanceCount += 1;
}

return () => {
instanceCount -= 1;
if (instanceCount === 0) {
if (instanceCount <= 1) {
restoreScroll();
}
};
}, [restoreScroll]);
}, [restoreScroll, isActive]);

useEffect(() => {
if (isLocked) {
if (isActive) {
scrollValue.current = `${window.scrollY}px`;
const scrollY = scrollValue.current;
const { body } = document;
Expand All @@ -56,5 +58,5 @@ export const useScrollLock = (isLocked: boolean): void => {
} else if (instanceCount === 1) {
restoreScroll();
}
}, [isLocked, restoreScroll]);
}, [isActive, restoreScroll]);
};

0 comments on commit 3c13a02

Please sign in to comment.