Skip to content

Commit

Permalink
improve scroll lock
Browse files Browse the repository at this point in the history
  • Loading branch information
sirineJ committed Dec 19, 2024
1 parent b13d78d commit f55271a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
1 change: 0 additions & 1 deletion packages/circuit-ui/components/Modal/Modal.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export default {
component: Modal,
tags: ['status:stable'],
parameters: {
layout: 'padded',
chromatic: {
modes: {
mobile: modes.smallMobile,
Expand Down
10 changes: 6 additions & 4 deletions packages/circuit-ui/hooks/useScrollLock/useScrollLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@ export const useScrollLock = (isLocked: boolean): void => {
const scrollY =
document.documentElement.style.getPropertyValue('--scroll-y');
const { body } = document;
const bodyWidth = body.offsetWidth;
// when position is set to fixed, the body element is taken out of
// the normal document flow and this may cause it to change size.
// To prevent this, we set the width of the body to its current width.
body.style.position = 'fixed';
body.style.left = '0';
body.style.right = '0';
body.style.width = `${bodyWidth}px`;
body.style.top = `-${scrollY}`;
} else {
// restore scroll to page
const { body } = document;
const scrollY = body.style.top;
body.style.position = '';
body.style.top = '';
body.style.left = '';
body.style.right = '';
body.style.width = '';
window.scrollTo(0, Number.parseInt(scrollY || '0', 10) * -1);
}
}, [isLocked]);
Expand Down

0 comments on commit f55271a

Please sign in to comment.