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

[ScrollArea] Check for :hover on mount #862

Merged
merged 6 commits into from
Nov 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,6 @@ describe('<ScrollArea.Scrollbar />', () => {
},
}));

it('adds [data-hovering] attribute when viewport is hovered', async function test(t = {}) {
// Fails to pass in browser CI, but works locally
if (!/jsdom/.test(window.navigator.userAgent)) {
// @ts-expect-error to support mocha and vitest
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
this?.skip?.() || t?.skip();
}

await render(
<ScrollArea.Root style={{ width: 200, height: 200 }} data-testid="root">
<ScrollArea.Viewport style={{ width: '100%', height: '100%' }}>
<div style={{ width: 1000, height: 1000 }} />
</ScrollArea.Viewport>
<ScrollArea.Scrollbar orientation="vertical" data-testid="vertical" keepMounted />
<ScrollArea.Scrollbar orientation="horizontal" data-testid="horizontal" keepMounted />
<ScrollArea.Corner />
</ScrollArea.Root>,
);

const verticalScrollbar = screen.getByTestId('vertical');
const horizontalScrollbar = screen.getByTestId('horizontal');

expect(verticalScrollbar).not.to.have.attribute('data-hovering');
expect(horizontalScrollbar).not.to.have.attribute('data-hovering');

fireEvent.pointerEnter(screen.getByTestId('root'), { pointerType: 'mouse' });

expect(verticalScrollbar).to.have.attribute('data-hovering', '');
expect(horizontalScrollbar).to.have.attribute('data-hovering', '');

fireEvent.pointerLeave(screen.getByTestId('root'), { pointerType: 'mouse' });

expect(verticalScrollbar).not.to.have.attribute('data-hovering');
expect(horizontalScrollbar).not.to.have.attribute('data-hovering');
});

it('adds [data-scrolling] attribute when viewport is scrolled', async () => {
await render(
<ScrollArea.Root style={{ width: 200, height: 200 }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function useScrollAreaViewport(params: useScrollAreaViewport.Parameters)
setHiddenState,
hiddenState,
handleScroll,
setHovering,
} = useScrollAreaRootContext();

const contentWrapperRef = React.useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -149,6 +150,14 @@ export function useScrollAreaViewport(params: useScrollAreaViewport.Parameters)
computeThumb();
}, [computeThumb, hiddenState, dir]);

useEnhancedEffect(() => {
// `onMouseEnter` doesn't fire upon load, so we need to check if the viewport is already
// being hovered.
if (viewportRef.current?.matches(':hover')) {
setHovering(true);
}
}, [viewportRef, setHovering]);

React.useEffect(() => {
if (
!contentWrapperRef.current ||
Expand Down