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 249d639
Showing 1 changed file with 6 additions and 4 deletions.
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 249d639

Please sign in to comment.