base-ui icon indicating copy to clipboard operation
base-ui copied to clipboard

[scroll-area] has overflow data attributes applied on first render before measure

Open ShaneYu opened this issue 10 months ago • 5 comments

Bug report

Current behavior

When the scroll-area component is first rendered it applies the data- attributes for has overflow x and y prior to the measure where it can determine if it actually has overflow or not.

Anyone using these attributes to apply additional styling, like padding right, when there is overflow y (for example) see a UI layout shift that just shouldn't happen.

Expected behavior

On first render all data- attributes indicating there is overflow x or y should not be applied. These attributes are applied after the first measure and it actually knows that there is and overflow.

Reproducible example

For a easy and visual example, simply use a scroll-area where the content does not cause any overflow. Add a class of data-has-overflow-y:pr-4 to the scroll-area root element.

When loading the page it will apply the padding right due to the attribute being present on first render and then immediately remove the padding after the first message and it removes those overflow attributes.

In this Gif you can see the layout shift occurring, the scroll-area is wrapping the todo items in the body of the card.

Image

Base UI version

Currently using version 1.0..0 (latest at time of writing).

Which browser are you using?

Brave (Chromium based), Chrome and Edge.

Which OS are you using?

Microsoft Windows 11 Version 25H2 (OS Build 26200.7623)

Which assistive tech are you using (if applicable)?

Not applicable.

Additional context

The issue is that within the ScrollAreaRoot.tsx the "has overflow" state is derived from hiddenState where it's default state is set to { x: false, y: false, corner: false }. hasOverflowY is computed as !hiddenState.y.

So on the very first render, hiddenState.y === false => hasOverflowY === true, which means any the data-has-overflow-y state attribute will be present immediately - before anything has been measured.

I wonder if the easiest and most correct fix would be to initialise hiddenState to "hidden until proven otherwise" (i.e. assume no overflow until the viewport/content measurement logic updates it).

Using this one-line semantic change to default hidden state as true.

const DEFAULT_HIDDEN_STATE = { x: true, y: true, corner: true };

This should work as the first render, hasOverflowY = !true = false => no data-has-overflow-y yet. After the viewport/content observers run and call setHiddenState(...), the flags become accurate and the attribute appears only when it's actually overflowed.

Until it's measured, saying it's overflowed feel inaccurate and leads to issues like this one; hopefully this tweak doesn't have any knock effect and fixes all of the overflow attributes as they are all derived from that hiddenState. I also believe this will help to avoid hydration/SSR awkwardness where the server render would otherwise claim the overflow before the client can measure.

ShaneYu avatar Jan 22 '26 16:01 ShaneYu

Hello @ShaneYu, your suggestion makes sense, but I'm unable to replicate your problem (at least with minimal examples on MacOS and Windows VM). Could you share a minimal working example on a StackBlitz, CodeSandbox, or GitHub repo?

LukasTy avatar Jan 23 '26 15:01 LukasTy

Since the issue is missing key information and has been inactive for 7 days, it has been automatically closed. If you wish to see the issue reopened, please provide the missing information.

github-actions[bot] avatar Jan 30 '26 21:01 github-actions[bot]

Didn't get to read this within the 7 day period - this makes sense to me though (defaulting all hidden states to true)

atomiks avatar Jan 31 '26 20:01 atomiks

@LukasTy I was able to reproduce this bug here, reload the preview to see the padding and red bg being applied on mount:

https://stackblitz.com/edit/mnumhhk3?file=src%2FApp.tsx

CiscoFran10 avatar Feb 01 '26 06:02 CiscoFran10

This fix will be available in the next npm release of Base UI.

In the meantime, you can try it out on our Canary release channel:

npm i https://pkg.pr.new/@base-ui/react@4107

github-actions[bot] avatar Mar 04 '26 11:03 github-actions[bot]