-
Notifications
You must be signed in to change notification settings - Fork 12
fix: Dismiss chart tooltip on ESC key press #167
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,23 +110,28 @@ export function InternalCoreChart({ | |
|
|
||
| // AWSUI-61678 | ||
| // Add global Escape key listener to dismiss hover tooltips for WCAG Content on Hover/Focus compliance | ||
| // Only enabled when keyboard navigation is disabled; when enabled, navigation handles Escape | ||
| // This listener handles Escape when using mouse hover. When keyboard navigation is active (application | ||
| // element is focused), the navigation handler deals with Escape, so we skip to avoid conflicts. | ||
| useEffect(() => { | ||
| if (!context.keyboardNavigationEnabled) { | ||
| const handleKeyDown = (event: KeyboardEvent) => { | ||
| if (event.keyCode === KeyCode.escape && context.tooltipEnabled) { | ||
| const tooltipState = api.tooltipStore.get(); | ||
| if (tooltipState.visible && !tooltipState.pinned) { | ||
| api.hideTooltip(); | ||
| } | ||
| const handleKeyDown = (event: KeyboardEvent) => { | ||
| // Skip if keyboard navigation is handling this (event originated from application element) | ||
| const target = event.target as Element; | ||
| if (context.keyboardNavigationEnabled && target?.closest('[role="application"]')) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do wen need to check for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevents double-handling. The navigation handler in chart-extra-navigation.ts already handles Escape for keyboard nav, this check skips the global handler when navigation is active. |
||
| return; | ||
| } | ||
|
|
||
| if (event.keyCode === KeyCode.escape && context.tooltipEnabled) { | ||
| const tooltipState = api.tooltipStore.get(); | ||
| if (tooltipState.visible && !tooltipState.pinned) { | ||
| api.hideTooltip(); | ||
| } | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener("keydown", handleKeyDown); | ||
| return () => { | ||
| document.removeEventListener("keydown", handleKeyDown); | ||
| }; | ||
| } | ||
| document.addEventListener("keydown", handleKeyDown); | ||
| return () => { | ||
| document.removeEventListener("keydown", handleKeyDown); | ||
| }; | ||
| }, [api, context.tooltipEnabled, context.keyboardNavigationEnabled]); | ||
|
|
||
| // Render fallback using the same root and container props as for the chart to ensure consistent | ||
|
|
||
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.
Does this break other event handlers? E.g. if I am currently in the keyboard navigation, and I have an event listener on the parent element, does it still get triggered?
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.
Yes, intentionally. Standard
role="application"behavior, captures all keys to prevent parent handlers (page scrolling, modal closing, etc.) from interfering with chart navigation.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.
What if this role is not set? Or can't that happen?
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.
This should not happen in practice because, the
role="application"is hardcoded in theChartApplicationcomponent (it's not configurable). The application element is only rendered whenkeyboardNavigation={true}.But if hypothetically it wasn't set: the global Escape handler would run during keyboard navigation, causing tooltip to dismiss when user meant to exit navigation.