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

fix(useDisableBodyScroll): avoid affecting body's height #1279

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/__tests__/hooks-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const INITIAL_BODY_STYLES = 'background: red;';
const DISABLED_BODY_STYLES =
INITIAL_BODY_STYLES +
' ' +
'overflow: hidden; overflow-y: scroll; position: fixed; top: 0px; left: 0px; right: 0px; bottom: 0px;';
'overflow: hidden; overflow-y: scroll; position: fixed; top: 0px; left: 0px; right: 0px;';

const DisableScroll = () => {
useDisableBodyScroll(true);
Expand Down
1 change: 0 additions & 1 deletion src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export const useDisableBodyScroll = (disable: boolean): void => {
`top: ${-bodyScrollTop}px;`,
'left: 0px;',
'right: 0px;',
'bottom: 0px;',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the bottom: 0px;, because:

  • If the body's height is smaller than the screen, there is no scroll, so it shouldn't be an issue.
  • If there is scroll, the hook already fixes the top position and scrolls to the corresponding vertical position. Setting bottom to 0px doesn't change anything.

The issue is that if we also set bottom to 0, children of the body that have height:100% will be affected by this hook (because when top and bottom values are set, the height of the container becomes well defined, and it will trigger changes in the child).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried different scenarios, and everything seems to work fine:

  • Body that has no scroll
  • Body that has scroll
    • Verified case when scroll position is at the top, middle and bottom of the body
  • Verified that resizing the window while blocking the scroll doesn't affect anything, even if the body didn't have scroll before the resize and I make the window very small.

'overscroll-behavior-y: contain;', // disable overscroll
].join('');
}
Expand Down
Loading