react-component / react-component/portal

isBodyOverflowing returns false when overflow-y: scroll is set on body

Open
#25 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
27
Forks
26
PR merge metrics
No merged PRs in 30d

Description

Hi team,

I'm encountering an issue with the isBodyOverflowing method in the context of a web app where the scrollbar should always be visible — even if the content does not actually overflow.

To achieve this consistent scrollbar behavior, I set:

body {
  overflow-y: scroll;
}

However, isBodyOverflowing still returns false in this case, since the body technically isn't overflowing. This results in a visual jump ("modal shift") when opening a dialog from a non-scrollable page — the scrollbar is already present, but no padding is applied because the method believes there's no overflow.

This is problematic in cases where the app switches frequently between scrollable and non-scrollable pages, and a consistent layout is desired.

Suggestions:
  • Could isBodyOverflowing consider overflow-y: scroll as a special case and return true if a scrollbar is always visible?
  • Alternatively, would it be possible to expose a way to override the internal state, so developers can control this behavior manually?

I'm using Ant Design, and this behavior leads to inconsistent UI when opening modals. A way to opt into always applying padding in this case would be very helpful.

Thanks!

Related method: portal/src/util.ts

export function isBodyOverflowing() {
  return (
    document.body.scrollHeight >
      (window.innerHeight || document.documentElement.clientHeight) &&
    window.innerWidth > document.body.offsetWidth
  );
}

Suggestion

export function isBodyOverflowing() {
  if (window.getComputedStyle(document.body).overflowY === 'scroll') return true;

  return (
    document.body.scrollHeight >
      (window.innerHeight || document.documentElement.clientHeight) &&
    window.innerWidth > document.body.offsetWidth
  );
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in portal/src/util.ts at isBodyOverflowing and verify how its result affects modal scrollbar padding. Check the behavior when document.body has overflow-y: scroll, then add coverage for the intended always-visible-scrollbar case. Done means the modal layout no longer shifts when the scrollbar is already present.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.