Skip to content

Commit

Permalink
Fix(web-react): Simplify useScrollControl to not scroll or set top of…
Browse files Browse the repository at this point in the history
…fset #DS-1124
  • Loading branch information
crishpeen committed Jan 11, 2024
1 parent e9277ea commit 0df3c47
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 23 deletions.
16 changes: 0 additions & 16 deletions packages/web-react/src/hooks/__tests__/useScrollControl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,12 @@ import { MutableRefObject } from 'react';
import { useScrollControl } from '../useScrollControl';

describe('useScrollControl', () => {
let originalScrollTo: typeof window.scrollTo;

const createMockRef = (isOpen: boolean) => ({
current: {
open: isOpen,
},
});

beforeEach(() => {
originalScrollTo = window.scrollTo;
window.scrollTo = jest.fn();
});

afterEach(() => {
window.scrollTo = originalScrollTo;
});

it('should disable scroll when isOpen is true', () => {
const mockRef = createMockRef(true) as MutableRefObject<HTMLDialogElement | null>;

Expand All @@ -28,15 +17,12 @@ describe('useScrollControl', () => {
});

expect(document.body.classList.contains('is-scrolling-disabled')).toBe(true);
expect(document.body.style.top).toMatch(/-\d+px/);
expect(document.body.style.paddingRight).toMatch(/\d+px/);
});

it('should enable scroll when isOpen is false', () => {
const mockRef = createMockRef(false) as MutableRefObject<HTMLDialogElement | null>;

const scrollToSpy = jest.spyOn(window, 'scrollTo');

act(() => {
renderHook(() => useScrollControl(mockRef, true));
});
Expand All @@ -45,8 +31,6 @@ describe('useScrollControl', () => {
renderHook(() => useScrollControl(mockRef, false));
});

expect(scrollToSpy).toHaveBeenCalledWith(0, 0);
expect(document.body.classList.contains('is-scrolling-disabled')).toBe(false);
expect(document.body.style.top).toBe('');
});
});
9 changes: 2 additions & 7 deletions packages/web-react/src/hooks/useScrollControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,23 @@ const CLASSNAME_SCROLLING_DISABLED = 'is-scrolling-disabled';
const disableScroll = () => {
const { body } = document;
const scrollBarWidth = window.innerWidth - body.clientWidth;
const offsetY = window.scrollY;

body.style.paddingRight = `${scrollBarWidth}px`;
body.style.top = `-${offsetY}px`;
body.classList.add(CLASSNAME_SCROLLING_DISABLED);
};

const enableScroll = (offsetY: number) => {
const enableScroll = () => {
const { body } = document;
body.style.paddingRight = '';
body.style.top = '';
body.classList.remove(CLASSNAME_SCROLLING_DISABLED);
window.scrollTo(0, -offsetY);
};

export const useScrollControl = (ref: MutableRefObject<HTMLDialogElement | null>, isOpen: boolean) => {
useEffect(() => {
if (isOpen) {
disableScroll();
} else if (ref.current && !ref.current.open) {
const offsetY = parseFloat(document.body.style.top || '0');
enableScroll(offsetY);
enableScroll();
}
}, [isOpen, ref]);
};

0 comments on commit 0df3c47

Please sign in to comment.