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

[Popover] Disable focus management when opened via hover #855

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion packages/react/src/Popover/Popup/PopoverPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const PopoverPopup = React.forwardRef(function PopoverPopup(
descriptionId,
popupRef,
mounted,
openReason,
} = usePopoverRootContext();
const positioner = usePopoverPositionerContext();

Expand Down Expand Up @@ -91,7 +92,7 @@ const PopoverPopup = React.forwardRef(function PopoverPopup(
<FloatingFocusManager
context={positioner.positionerContext}
modal={false}
disabled={!mounted}
disabled={!mounted || openReason === 'hover'}
initialFocus={resolvedInitialFocus}
returnFocus={finalFocus}
>
Expand Down
27 changes: 26 additions & 1 deletion packages/react/src/Popover/Root/PopoverRoot.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { Popover } from '@base-ui-components/react/Popover';
import { fireEvent, flushMicrotasks, screen, waitFor } from '@mui/internal-test-utils';
import { act, fireEvent, flushMicrotasks, screen, waitFor } from '@mui/internal-test-utils';
import userEvent from '@testing-library/user-event';
import { expect } from 'chai';
import { spy } from 'sinon';
Expand Down Expand Up @@ -333,4 +333,29 @@ describe('<Popover.Root />', () => {
{ timeout: 1500 },
);
});

it('does not move focus to the popover when opened with hover', async () => {
await render(
<Popover.Root openOnHover delay={0}>
<Popover.Trigger>Toggle</Popover.Trigger>
<Popover.Positioner>
<Popover.Popup>
<Popover.Close>Close</Popover.Close>
</Popover.Popup>
</Popover.Positioner>
</Popover.Root>,
);

const toggle = screen.getByRole('button', { name: 'Toggle' });

act(() => toggle.focus());

await user.hover(toggle);
await flushMicrotasks();

const close = screen.getByRole('button', { name: 'Close' });

expect(close).not.to.equal(null);
expect(close).not.to.toHaveFocus();
});
});
12 changes: 5 additions & 7 deletions packages/react/src/Popover/Root/usePopoverRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function usePopoverRoot(params: usePopoverRoot.Parameters): usePopoverRoo
open: externalOpen,
onOpenChange: onOpenChangeProp = () => {},
defaultOpen = false,
keepMounted = false,
delay,
closeDelay,
openOnHover = false,
Expand Down Expand Up @@ -69,22 +68,21 @@ export function usePopoverRoot(params: usePopoverRoot.Parameters): usePopoverRoo
(nextOpen: boolean, event?: Event, reason?: OpenChangeReason) => {
onOpenChange(nextOpen, event, reason);
setOpenUnwrapped(nextOpen);
if (!keepMounted && !nextOpen) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need to check how setMounted(false) with this keepMounted check operates now for the other components with the hidden prop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like keepMounted was never passed to the hooks, hence it worked like keepMounted=false. I've removed the prop for Popover but it should be done for all components.


if (!nextOpen) {
if (animated) {
runOnceAnimationsFinish(() => {
if (!openRef.current) {
setMounted(false);
setOpenReason(null);
}
});
} else {
setMounted(false);
setOpenReason(null);
}
}

if (nextOpen) {
setOpenReason(reason ?? null);
} else {
setOpenReason(null);
setOpenReason(reason ?? null);
}
},
);
Expand Down
Loading