willmcpo / body-scroll-lock

Body scroll locking that just works with everything 😏
MIT License
4.04k stars 339 forks source link

CSS variable and body class for scrollbar gap #197

Open o11an opened 3 years ago

o11an commented 3 years ago

I've seen a few people already made other approaches to solve this, the problem being that other elements than the body might need the padding for the scrollbar gap as well, to prevent the otherwise visual glitch.

My solution to this, which in my opinion gives the user a more versatile way of adjusting for the gap where needed and without complicating things, would be to add a body class and a css variable to the root. These can then be used in any elements CSS needing the extra padding like so (assuming it's SCSS):

.body-scroll-lock-active & {
    padding-right: var(--scrollbar-gap);
}