-
Notifications
You must be signed in to change notification settings - Fork 12
[WB-2147] Popover: Dismiss popover when tabbing out (blur) #2896
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
base: sb-popover-enable-snap
Are you sure you want to change the base?
Conversation
…behavior to close/dismiss the popover once the focus moves outside of it.
🦋 Changeset detectedLatest commit: 0e53eba The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Size Change: +11 B (+0.01%) Total Size: 109 kB
ℹ️ View Unchanged
|
npm Snapshot: Published🎉 Good news!! We've packaged up the latest commit from this PR (812aefe) and published all packages with changesets to npm. You can install the packages in ./dev/tools/deploy_wonder_blocks.js --tag="PR2896"Packages can also be installed manually by running: pnpm add @khanacademy/wonder-blocks-<package-name>@PR2896 |
A new build was pushed to Chromatic! 🚀https://5e1bf4b385e3fb0020b7073c-txblpvpzqf.chromatic.com/ Chromatic results:
|
GeraldRequired Reviewers
Don't want to be involved in this pull request? Comment |
beaesguerra
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
| }); | ||
| }); | ||
|
|
||
| // TODO(FEI-5533): Key press events aren't working correctly with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for enabling this to work again!
| it("should close the popover when the user tabs before the trigger element", async () => { | ||
| // Arrange | ||
| render( | ||
| <Popover | ||
| dismissEnabled={true} | ||
| placement="top" | ||
| content={ | ||
| <PopoverContent | ||
| title="Title" | ||
| content="content" | ||
| closeButtonVisible={true} | ||
| /> | ||
| } | ||
| > | ||
| {({open}: any) => ( | ||
| <button data-anchor onClick={open}> | ||
| Open default popover | ||
| </button> | ||
| )} | ||
| </Popover>, | ||
| ); | ||
|
|
||
| // open the popover | ||
| await userEvent.click( | ||
| await screen.findByRole("button", { | ||
| name: "Open default popover", | ||
| }), | ||
| ); | ||
|
|
||
| // Act | ||
| // Focus on the reference element | ||
| await userEvent.tab({shift: true}); | ||
| // Focus on the previous element before the popover (triggers | ||
| // closing the popover) | ||
| await userEvent.tab({shift: true}); | ||
|
|
||
| // Assert | ||
| await waitFor(() => { | ||
| expect(screen.queryByText("Title")).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| it("should close the popover when the user tabs after the last focusable element inside the popover", async () => { | ||
| // Arrange | ||
| render( | ||
| <div> | ||
| <Popover | ||
| dismissEnabled={true} | ||
| placement="top" | ||
| content={ | ||
| <PopoverContent | ||
| title="Title" | ||
| content="content" | ||
| closeButtonVisible={true} | ||
| /> | ||
| } | ||
| > | ||
| <Button>Open default popover</Button> | ||
| </Popover> | ||
| <Button>Next button outside</Button> | ||
| </div>, | ||
| ); | ||
|
|
||
| // open the popover | ||
| // open the popover by focusing on the trigger element | ||
| await userEvent.click( | ||
| await screen.findByRole("button", { | ||
| name: "Open default popover", | ||
| }), | ||
| ); | ||
|
|
||
| // Focus on the last focusable element inside the popover (dismiss button) | ||
| await userEvent.tab(); | ||
|
|
||
| // Focus on the next element after the popover | ||
| await userEvent.tab(); | ||
|
|
||
| // Assert | ||
| expect(screen.queryByText("Title")).not.toBeInTheDocument(); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have tests for when dismissEnabled={false}, the popover isn't closed on focus out?
| } | ||
|
|
||
| // The user is tabbing out of the popover. | ||
| if (e.key === "Tab" && !e.shiftKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use the keys.tab from wonder-blocks-core!
| } | ||
|
|
||
| // The user is tabbing before the trigger element. | ||
| if (e.key === "Tab" && e.shiftKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same suggestion about using the keys constant!
Summary:
This PR changes the
dismissEnabledbehavior to close/dismiss the popover oncethe focus moves outside of it.
Issue: https://khanacademy.atlassian.net/browse/WB-2147
Test plan:
Navigate to the
DismissEnabledstory and test the following scenarios:/?path=/docs/packages-popover-popover--docs#dismiss-enabledScreen.Recording.2025-12-09.at.2.47.31.PM.mov