Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(web-react): Add useScrollControl hook and block scrolling when Dialog open #DS-1052 #1140

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/web-react/src/components/Dialog/useDialog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, MutableRefObject } from 'react';
import { useScrollControl } from '../../hooks';

export const useDialog = (ref: MutableRefObject<HTMLDialogElement | null>, isOpen: boolean) => {
useEffect(() => {
Expand All @@ -13,6 +14,8 @@ export const useDialog = (ref: MutableRefObject<HTMLDialogElement | null>, isOpe
}
}, [isOpen, ref]);

useScrollControl(ref, isOpen);

const openDialog = () => {
if (ref?.current) {
ref.current.showModal();
Expand Down
38 changes: 38 additions & 0 deletions packages/web-react/src/hooks/__tests__/useScrollControl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { renderHook, act } from '@testing-library/react-hooks';
import { MutableRefObject } from 'react';
import { useScrollControl } from '../useScrollControl';

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

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

act(() => {
renderHook(() => useScrollControl(mockRef, true));
});

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>;

act(() => {
renderHook(() => useScrollControl(mockRef, true));
});

act(() => {
renderHook(() => useScrollControl(mockRef, false));
});

expect(document.body.classList.contains('is-scrolling-disabled')).toBe(false);
expect(document.body.style.top).toBe('');
});
});
1 change: 1 addition & 0 deletions packages/web-react/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export * from './useDragAndDrop';
export * from './useIcon';
export * from './useIsomorphicLayoutEffect';
export * from './useLastActiveFocus';
export * from './useScrollControl';
export * from './useToggle';
32 changes: 32 additions & 0 deletions packages/web-react/src/hooks/useScrollControl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useEffect, MutableRefObject } from 'react';

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 { 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);
}
}, [isOpen, ref]);
};