8000 DT-131 - improve keyboard navigation for modals by rossedfort · Pull Request #966 · temporalio/ui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

DT-131 - improve keyboard navigation for modals #966

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

Merged
merged 5 commits into from
Dec 2, 2022
Merged

Conversation

rossedfort
Copy link
Contributor

What was changed

  • trap focus within modals by cycling to the previous element if the first/last focusable element is focused
  • works for forward and backward navigation, i.e. tab and shift+tab
Screen.Recording.2022-11-29.at.2.27.01.PM.mov

Why?

Checklist

  1. Closes

  2. How was this tested:

  1. Any docs updates needed?

@vercel
Copy link
vercel bot commented Nov 29, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
holocene ✅ Ready (Inspect) Visit Preview Dec 2, 2022 at 4:13PM (UTC)
ui ✅ Ready (Inspect) Visit Preview Dec 2, 2022 at 4:13PM (UTC)

@cypress
Copy link
cypress bot commented Nov 29, 2022



Test summary

88 0 0 0


Run details

Project Temporal UI
Status Passed
Commit 555e807870 ℹ️
Started Dec 2, 2022 4:14 PM
Ended Dec 2, 2022 4:19 PM
Duration 05:40 💡
OS Linux Ubuntu - 20.04
Browser Chrome 107

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

- add some support for screen readers
- focus "close" button when modal opens
<div
class="body"
class:large
aria-modal="true"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
aria-modal="true"
tabindex="-1"
bind:this={modal} // add a modal (HTMLDivElement) variable
role="dialog"
aria-label={ariaLabel} // add an ariaLabel prop

event.preventDefault();
}
} else if (document.activeElement === confirmButton.buttonElement) {
closeButton.focus();
Copy link
Contributor

Choose a reason for hiding this comment

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

There's an edge case here where if loading is true the focus can escape the modal.

What do you think about a more generic check for a first and last focusable element? e.g.

    const focusable = modalElement.querySelectorAll('button');
    const firstFocusable = focusable[0];
    const lastFocusable = focusable[focusable.length - 1];
    if (event.key === 'Tab') {
      if (event.shiftKey) {
        if (document.activeElement === firstFocusable) {
          lastFocusable.focus();
          event.preventDefault();
        }
      } else if (document.activeElement === lastFocusable) {
        firstFocusable.focus();
        event.preventDefault();
      }
    }

This means we could also remove the focus export on the Button component.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah nice catch. Yeah I had initially thought of a more generic solution like that, but I didn't love the idea of using a querySelectorAll to get all the focusable elements, I thought using bind with only the elements we wanted focusable was safer. That said, I don't know why we wouldn't want any button in a modal to not be focusable, so I think I like this solution better 👍

Copy link
Contributor
@laurakwhit laurakwhit left a comment

Choose a reason for hiding this comment

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

One last little suggestion, but otherwise lgtm :shipit:

{#if !loading}
<div class="float-right p-6" on:click={cancelModal}>
<button
aria-label="Close Modal"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
aria-label="Close Modal"
aria-label={cancelText}

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
0