-
Notifications
You must be signed in to change notification settings - Fork 72
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
Tooltip: Focus tooltipped item when tooltip is active, allow tooltip to be dismissed on Esc
key
#2380
base: main
Are you sure you want to change the base?
Tooltip: Focus tooltipped item when tooltip is active, allow tooltip to be dismissed on Esc
key
#2380
Conversation
…missed with escape
✅ Deploy Preview for moduswebcomponents ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
const target = this.element.firstElementChild as HTMLElement; | ||
const tabIndex = this.targetTabIndex !== '' && this.targetTabIndex != null ? (this.targetTabIndex as string) : '-1'; | ||
target.setAttribute('tabindex', tabIndex); | ||
target.focus(); |
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.
Focusing for two reasons:
- According to ARIA standards, focus stays on the triggering element while the tooltip is displayed.
- Keyboard interaction is done on focused item only.
show(): void { | ||
const target = this.element.firstElementChild as HTMLElement; | ||
const tabIndex = this.targetTabIndex !== '' && this.targetTabIndex != null ? (this.targetTabIndex as string) : '-1'; |
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.
Adding tabindex
to make item focusable. Feel free to suggest alternatives.
Esc
key
@austinoneil Is it possible that this could be simplied to just adding something like @Listen('keyup')
escapeKeyHandler(event: KeyboardEvent) {
if (event.code === 'Escape') {
this.hide();
}
} The idea being that This avoids Also I used FYI @coliff |
The issue with this is that I'll file an issue for replacing |
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.
@austinoneil I tested the implementation with multiple buttons with tooltips locally and have a few observations:
- I need to press the tab key twice to navigate between buttons with tooltips.
- Remove listener in cleanupPopper fn.
I understand the issue with using @Listen
since you have to specify the target as document, body, or window because the tooltip is not focusable. Since you are adding the listener to the specific focusable element, the problem of all tooltip instance listeners triggering should not occur. Therefore I believe we should be able to remove the tabIndex logic, can you try it?
Description
Focus tooltipped item when tooltip is active, allow tooltip to be dismissed on escape
References #2177
Type of change
How Has This Been Tested?
End to end tests, manual test using the following html:
Also tried without
tabindex
on first button.Checklist