fix(presentation): describe the tooltip trigger and close it with Escape (Q-6.8-11)
Tooltip injects `aria-describedby` into its focusable child (merged with any existing value) instead of a non-focusable wrapper span, and Escape hides the tooltip until the next hover or focus (WCAG 1.4.13). The listener is on the document so a hovered, unfocused trigger closes too. Verified with tests/component/tooltip.test.tsx (RED: 3 of 3 failed before the change) and the lane gates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
38db97a564
commit
21233e8d9b
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
cloneElement,
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
@@ -104,17 +105,42 @@ export function Popover({ triggerLabel, children }: PopoverProps) {
|
||||
|
||||
export type TooltipProps = Readonly<{
|
||||
content: string;
|
||||
children: React.ReactElement;
|
||||
children: React.ReactElement<{ "aria-describedby"?: string | undefined }>;
|
||||
}>;
|
||||
|
||||
/**
|
||||
* Describes its focusable child (WCAG 1.4.13): the child carries
|
||||
* `aria-describedby`, and Escape hides the tooltip until the next hover or
|
||||
* focus.
|
||||
*/
|
||||
export function Tooltip({ content, children }: TooltipProps) {
|
||||
const tooltipId = useId();
|
||||
const [dismissed, setDismissed] = useState(false);
|
||||
useEffect(() => {
|
||||
if (dismissed) return;
|
||||
// Document level: a hovered trigger does not have keyboard focus.
|
||||
function dismissOnEscape(event: KeyboardEvent) {
|
||||
if (event.key === "Escape") setDismissed(true);
|
||||
}
|
||||
document.addEventListener("keydown", dismissOnEscape);
|
||||
return () => document.removeEventListener("keydown", dismissOnEscape);
|
||||
}, [dismissed]);
|
||||
const describedBy = [children.props["aria-describedby"], tooltipId]
|
||||
.filter(Boolean)
|
||||
.join(" ");
|
||||
return (
|
||||
<span className="ui-tooltip">
|
||||
<span aria-describedby={tooltipId} className="ui-tooltip__trigger">
|
||||
{children}
|
||||
</span>
|
||||
<span className="ui-tooltip__content" id={tooltipId} role="tooltip">
|
||||
<span
|
||||
className="ui-tooltip"
|
||||
onFocus={() => setDismissed(false)}
|
||||
onMouseEnter={() => setDismissed(false)}
|
||||
>
|
||||
{cloneElement(children, { "aria-describedby": describedBy })}
|
||||
<span
|
||||
className="ui-tooltip__content"
|
||||
hidden={dismissed}
|
||||
id={tooltipId}
|
||||
role="tooltip"
|
||||
>
|
||||
{content}
|
||||
</span>
|
||||
</span>
|
||||
|
||||
Reference in New Issue
Block a user