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

VisuallyHidden component causes unnecessary scroll in overflow containers #3406

Open
cham0287 opened this issue Mar 7, 2025 · 3 comments · May be fixed by #3407
Open

VisuallyHidden component causes unnecessary scroll in overflow containers #3406

cham0287 opened this issue Mar 7, 2025 · 3 comments · May be fixed by #3407

Comments

@cham0287
Copy link

cham0287 commented Mar 7, 2025

Bug report

Current Behavior

When using the VisuallyHidden component inside containers with overflow: hidden or overflow-y: scroll, the component extends beyond its parent container boundaries, causing unwanted scrollbars to appear or content to overflow. This creates unexpected scrolling behavior in parent components.

Expected behavior

The VisuallyHidden component should remain within its parent container boundaries and not trigger any scrolling behavior in parent components, while still maintaining its accessibility functionality.

Reproducible example

CodeSandbox Example

import { VisuallyHidden } from "@radix-ui/react-visually-hidden";

export default function App() {
  return (
    <div
      style={{ height: "200px", overflow: "scroll", border: "1px solid red" }}
    >
      {Array.from({ length: 200 }, (_, index) => (
        <p key={index}>test</p>
      ))}
      <VisuallyHidden />
    </div>
  );
}

Suggested solution

The issue can likely be fixed by adding explicit positioning (top: 0 and left: 0) to the VisuallyHidden component's inline styles. This would properly constrain the element within its parent container.

<span
  style={{
    position: 'absolute',
    border: 0,
    width: 1,
    height: 1,
    padding: 0,
    margin: -1,
    overflow: 'hidden',
    clip: 'rect(0, 0, 0, 0)',
    whiteSpace: 'nowrap',
    wordWrap: 'normal',
    top: 0, // Add this
    left: 0, // Add this
  }}
>
  {children}
</span>

Additional context

I initially discovered this issue when using the Form component and noticed unexpected scrollbars. After investigating further through Select component, I traced the problem back to the VisuallyHidden component.
This suggests that the issue may affect all components that use VisuallyHidden internally, including Form, Select, and others.

To debug the issue, I modified the VisuallyHidden component by removing the clip rect, adding background color, and increasing its width and height. This revealed that the component was positioning itself at the bottom of the container without respecting the height limitations of the overflow container, which is what triggered the unwanted scrollbars. Adding explicit top: 0 and left: 0 positioning properties constrains the element properly within its parent boundaries.

Your environment

Software Name(s) Version
Radix Package(s) @radix-ui/react-visually-hidden 1.1.2
React n/a 18.2.0
Browser Chrome Latest
Assistive tech N/A N/A
Node n/a N/A
npm/yarn npm N/A
Operating System CodeSandbox Environment N/A
TypeScript n/a 4.4.4
React Scripts n/a 5.0.1
@cham0287
Copy link
Author

cham0287 commented Mar 7, 2025

I've created a PR to fix this issue: #3407

@viky2022
Copy link

I encountered the same situation in the shadcn UI based on the radix UI, where the select component in a div box with the overflow x-auto attribute resulted in a white border that caused the entire page to display a scrollbar. How can I solve this problem? Thank you very much

@cham0287
Copy link
Author

I encountered the same situation in the shadcn UI based on the radix UI, where the select component in a div box with the overflow x-auto attribute resulted in a white border that caused the entire page to display a scrollbar. How can I solve this problem? Thank you very much

Although it doesn't seem like a good solution, but as a temporary workaround, you can easily solve this by setting position relative on one of the parent elements that contains the Select component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants