react-spectrum: React Aria's usePreventScroll hook causes whitespace shown on desktops from padding-right on html element
🐛 Bug Report
When the usePreventScroll hook is used on a non-mobile device (with a scrollbar), a strip of whitespace shows up. This happens when a element with a 100% width and height is added to the DOM.
The problem is caused by the padding-right that’s added to the html element.
🤔 Expected Behavior
The whitespace should not be visible on the right side of the page.
😯 Current Behavior
The padding-right shows up at the right side of the page.
💁 Possible Solution
The body element styled should get overflow: hidden assigned. Also possible to give the body element the same background color as the fullscreen container, which is not optimal.
🔦 Context
See the right side of the page, used in a new Next.js application.

💻 Code Sample
🌍 Your Environment
| Software | Version(s) |
|---|---|
| react-aria | 3.1.0 |
| Browser | Google Chrome |
| Operating System | Windows 10 64bit |
🧢 Your Company/Team
🕷 Tracking Issue (optional)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 5
- Comments: 17 (5 by maintainers)
Any updates on this issue? It seems to still be a current thing
react-spectrumstill making progress? the best solution has already been provided above with compensation, but it’s been a yearI looked at the source code of the hook and came to the conclusion that, in principle, it is enough for the user that the value be placed in the style variable, then user can manually create
.compensate-for-scrollbarclassName with css var reference in the value.Here is the modified code
usePreventScroll - refactored
Somewhere in your global styles
Then
compensate-for-scrollbarclassName.compensate-for-scrollbarclassName together withstickyclassName (in case of controlled component)That fix for me problems related with jumping fixed/sticky elements. Regarding the issue of changing the placement of styles I don’t have specific ideas why you are doing this on
htmland not on thebody, but for my cases this is enough.I think the best way to solve this issue is following steps
htmlelement add custom classNamecompensate-for-scrollbarbodyelement when isActive, AND generate styles for this classNameWhen is not active - nothing.
width: 100%useleft: 0; right:0to work correctly with padding compensation.This technique is well known and used in multiple popular libraries like
fancybox.At this moment I don’t have any solution to do the same thing with
usePreventScrollas I described to achieve the same result. I know this hook as a whole aria library is not related to styling, but I think this is a good step towards users.@pascalduez we’ll need to find some other way than a callback for usePreventScroll, that’d potentially require listening to every dialog etc that makes use of usePreventScroll, in a large shared code base, this could be untenable. We don’t have another idea at this time, so all ideas are welcome.