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

add focus-trap to popover #5670

Merged
merged 5 commits into from
May 11, 2021
Merged

add focus-trap to popover #5670

merged 5 commits into from
May 11, 2021

Conversation

gwyneplaine
Copy link
Contributor

Our popovers aren't keyboard friendly.
Previously we circumvented this in the admin-ui filter popover by autofocusing the select component.

This is a more robust fix, which adds focus-trap to our popover component.
I've opted for focus-trap over focus-trap-react here, as it:

  • gives us more granular control
  • doesn't activate the focus-trap automatically on mount
  • doesn't pull in an unnecessary react-wrapper

Porting this over to use focus-trap-react later is arbitrary.

Before:

focus-trap-befopre.mov

After:

focus-trap-after.mov

@vercel
Copy link

vercel bot commented May 11, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/keystonejs/keystone-next-docs/3rkp8DfaYdx4p3g3T2PfQ27U4S1d
✅ Preview: https://keystone-next-docs-git-fix-dialog-focus-keystonejs.vercel.app

@changeset-bot
Copy link

changeset-bot bot commented May 11, 2021

🦋 Changeset detected

Latest commit: 27f3171

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@keystone-ui/popover Minor
@keystone-next/admin-ui Patch

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

@vercel vercel bot temporarily deployed to Preview May 11, 2021 03:25 Inactive
@@ -194,6 +196,24 @@ type DialogProps = {
export const PopoverDialog = forwardRef<HTMLDivElement, DialogProps>(
({ isVisible, children, arrow, ...props }, consumerRef) => {
const { elevation, radii, shadow, colors } = useTheme();
const focusTrapRef = useRef<HTMLDivElement | null>(null);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

popper's provided ref is a function, and not a refObject, we create a refObject here for us to attach our focustrap to

<div data-popper-arrow ref={arrow.ref} className="tooltipArrow" {...arrow.props} />

{children}
<div ref={focusTrapRef}>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wrapping div to attach focus-trap to


useEffect(() => {
if (focusTrap?.current) {
if (isVisible) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

hook into isVisible to activate / deactivate the focustrap

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 11, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 27f3171:

Sandbox Source
@keystone-next/example-sandbox Configuration

@vercel vercel bot temporarily deployed to Preview May 11, 2021 03:29 Inactive
@gwyneplaine gwyneplaine requested a review from a team May 11, 2021 03:29
@vercel vercel bot temporarily deployed to Preview May 11, 2021 03:50 Inactive
@vercel vercel bot temporarily deployed to Preview May 11, 2021 05:51 Inactive
Copy link
Member

@molomby molomby left a comment

Choose a reason for hiding this comment

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

LGTM

@gwyneplaine gwyneplaine merged commit 669f0d8 into master May 11, 2021
@gwyneplaine gwyneplaine deleted the fix/dialog-focus branch May 11, 2021 06:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants