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

refactor: add FocusTrapController for trapping focus within a node #3140

Merged
merged 4 commits into from
Dec 8, 2021

Conversation

vursen
Copy link
Contributor

@vursen vursen commented Dec 1, 2021

Description

The PR introduces a controller for trapping focus within a DOM node that is basically an extraction of the respective logic from the overlay component.

The controller supports two methods:

  • .trapFocus(node) – activates a focus trap for a DOM node and sets focus on the first focusable element inside the node if focus is not inside already.
  • .releaseFocus() – deactivates the focus trap

Todo List:

Depends on #3141

Part of #3134

Type of change

  • Internal

Checklist

  • I have read the contribution guide: https://vaadin.com/docs-beta/latest/guide/contributing/overview/
  • I have added a description following the guideline.
  • The issue is created in the corresponding repository and I have referenced it.
  • I have added tests to ensure my change is effective and works as intended.
  • New and existing tests are passing locally with my change.
  • I have performed self-review and corrected misspellings.

return element.matches(':not([disabled])');
}
// Elements that can be focused even if they have [disabled] attribute.
return element.matches('a[href], area[href], iframe, [tabindex], [contentEditable]');
Copy link
Contributor

@knoobie knoobie Dec 1, 2021

Choose a reason for hiding this comment

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

Should there be more checks regarding accessibility? Like aria-hidden? See e.g. tabindex inside aria-hidden https://www.w3.org/WAI/standards-guidelines/act/rules/aria-hidden-no-focusable-content-6cfa84/ for more details with positive and negative test examples

Edit: there is a highly advanced a11y lib for all kind of utilities if you wanna get some inspiration https://github.com/medialize/ally.js/tree/master/src/is

Copy link
Contributor Author

@vursen vursen Dec 2, 2021

Choose a reason for hiding this comment

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

Thank you for the references. 👍

About this particular PR, I would prefer to keep the implementation in its original shape as much as possible because the main idea here is more about decomposing and moving functions from the overlay to the component-base package.

Although, regarding the aria-hidden attribute, does it need special care? According to the first link you shared, the attribute doesn't make the element not focusable but rather prevents its announcement so there is no need to check it here, isn't it?

BTW, I extracted these changes into a separate PR, let's continue the discussion there if you want.

Copy link
Contributor

Choose a reason for hiding this comment

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

You are right, don't mind me 😀 the first reference is targeting application developer to explain that it's not good to add focusable elements inside aria-hidden because the user has no idea where his focus is once he tabbed into it. So all good!

The second reference was just something that could be interesting for you if you wanna refactor your implementation later on - nothing to be done immediately. 👍

@vursen vursen force-pushed the feat/focus-trap-controller branch 8 times, most recently from 176635b to 272c579 Compare December 7, 2021 14:00
@vursen vursen changed the title refactor: add FocusTrapController and make overlay use it refactor: add FocusTrapController for trapping focus within a node Dec 7, 2021
@vursen vursen marked this pull request as ready for review December 7, 2021 14:28
@sonarcloud
Copy link

sonarcloud bot commented Dec 8, 2021

SonarCloud Quality Gate failed.    Quality Gate failed

Bug C 1 Bug
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@vaadin-bot
Copy link
Collaborator

This ticket/PR has been released with platform 23.0.0.alpha1 and is also targeting the upcoming stable 23.0.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants