Skip to content

Commit 444a13d

Browse files
authored
fix: apply scrollbar-gutter only when scrollbar is present to prevent layout shift (#8303)
1 parent ca2d897 commit 444a13d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

packages/@react-aria/overlays/src/usePreventScroll.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ export function usePreventScroll(options: PreventScrollOptions = {}): void {
7070
// For most browsers, all we need to do is set `overflow: hidden` on the root element, and
7171
// add some padding to prevent the page from shifting when the scrollbar is hidden.
7272
function preventScrollStandard() {
73+
let scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
7374
return chain(
74-
// Use scrollbar-gutter when supported because it also works for fixed positioned elements.
75-
'scrollbarGutter' in document.documentElement.style
76-
? setStyle(document.documentElement, 'scrollbarGutter', 'stable')
77-
: setStyle(document.documentElement, 'paddingRight', `${window.innerWidth - document.documentElement.clientWidth}px`),
75+
scrollbarWidth > 0 &&
76+
// Use scrollbar-gutter when supported because it also works for fixed positioned elements.
77+
('scrollbarGutter' in document.documentElement.style
78+
? setStyle(document.documentElement, 'scrollbarGutter', 'stable')
79+
: setStyle(document.documentElement, 'paddingRight', `${scrollbarWidth}px`)),
7880
setStyle(document.documentElement, 'overflow', 'hidden')
7981
);
8082
}

0 commit comments

Comments
 (0)