Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[useScrollLock] Layout shift occurs with certain layouts #603

Closed
atomiks opened this issue Sep 12, 2024 · 2 comments · Fixed by #604
Closed

[useScrollLock] Layout shift occurs with certain layouts #603

atomiks opened this issue Sep 12, 2024 · 2 comments · Fixed by #604
Labels
bug 🐛 Something doesn't work component: dialog This is the name of the generic UI component, not the React module!

Comments

@atomiks
Copy link
Contributor

atomiks commented Sep 12, 2024

The useScrollLock hook currently has issues:

  • position: fixed elements need to have their width or right properties compensated by the removed scrollbar width, otherwise they shift over to the right once scroll locking is activated on the body. Regular content is unaffected. To complicate matters more, in some systems, the <body> scrollbar appears on the left of the screen; this means the left property, if specified, would need to be compensated under certain conditions.
  • When overflow-y: scroll is applied to the <body>, the removed scrollbar padding compensation itself causes layout shift and needs to be ignored. There may be other layout settings where this is the case as well.

radix-ui/primitives#1925

The scrollbar-gutter: stable CSS property, when applied to <html>:

  • Is unsupported in Safari
  • Fully solves this issue in Firefox
  • Is bugged in Chrome, as initial shift still occurs, then works on subsequent locking until a resize occurs - making it unusable in practice.

Possible solutions

  1. @vladmoroz has a demo as an alternative that could "just work" and require no manual intervention: https://codesandbox.io/p/sandbox/remove-scroll-poc-forked-pqfyhf
  2. If the above doesn't work out - first, we need to remove the padding compensation under certain layouts. However, position: fixed will still need their layout adjusted for the removed scrollbar width. We need to add a CSS variable to the root such as --base-ui-removed-scrollbar-width that users can use to apply to their position: fixed elements: width: calc(100% - var(--base-ui-removed-scrollbar-width, 0px)) or right: var(--base-ui-removed-scrollbar-width, 0).

Search keywords: scroll-lock

@atomiks atomiks added the bug 🐛 Something doesn't work label Sep 12, 2024
@oliviertassinari
Copy link
Member

oliviertassinari commented Sep 14, 2024

I guess it's related to #79 and mui/material-ui#5750 (I imagine we can transfer this issue back to here).

One thing to consider is that we need to support overflow-y: scroll on the <html>. See for mui/material-ui#40749 why. It's broken right now on Base UI and its docs. Regression issue opened: #611.

@oliviertassinari oliviertassinari added the component: dialog This is the name of the generic UI component, not the React module! label Sep 14, 2024
Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: dialog This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants