Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/press-trigger-toggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[feat] Tooltip (and `useTooltip`) take `pressTrigger` (type `TooltipPressTrigger`): `none` (the default, unchanged) or `toggle`. With `toggle` on a button trigger, a click, a tap, Enter, or Space pins the tooltip open and the same press closes it, while hover and focus still preview it. A pinned tooltip stays until Escape, a second press, a press outside, or focus leaving, and it takes Escape before the layer beneath it. The press stops at its trigger, so a clickable row, card, or label around it does nothing, and the trigger's own handlers still run. With `anchorRef`, Tooltip renders nothing around the trigger to stop it: the anchor's own `onClick` runs, Astryx clickable rows, cards, and containers ignore the press, and a plain `onClick` element around the anchor receives it unless the anchor's `onClick` stops it. The trigger reports `aria-expanded` and `aria-controls`, and the tooltip's text is announced once when it pins, inside a modal too (`component:Tooltip` FR9 and FR10). A trigger that is not a button gets a development warning and keeps tap-only opening; beside `pressTrigger="toggle"`, `touchTrigger` is ignored.

@vjeux
151 changes: 151 additions & 0 deletions apps/storybook/stories/TooltipPressToggleA11y.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
* @file TooltipPressToggleA11y.stories.tsx
* @input Uses Tooltip, Button, ClickableCard and Layout from core
* @output The fixtures the press-toggle proofs drive in a real browser: a
* button whose only job is its tooltip, the same trigger inside a clickable
* card, an anchored trigger with its own click handler inside a clickable
* card, and a trigger whose tooltip opens on no focus.
* @position The reproduction path for the `pressTrigger="toggle"` cases in
* `packages/core/src/Layer/__tests__/TouchTrigger.a11y.chromium.spec.ts`
* (`component:Tooltip` FR9, FR10; `docs/specs/AST-009/spec.md` FR30: a
* browser claim is checked against a checked-in story).
*
* SYNC: The spec navigates to these story ids by name. Nothing at compile
* time catches a renamed export — the Chromium spec does, when it finds no
* story.
*/

import {useRef, useState} from 'react';
import type {Meta, StoryObj} from '@storybook/react';
import {Button} from '@astryxdesign/core/Button';
import {ClickableCard} from '@astryxdesign/core/ClickableCard';
import {VStack} from '@astryxdesign/core/Layout';
import {Tooltip} from '@astryxdesign/core/Tooltip';

const meta: Meta = {
title: 'a11y/Tooltip press toggle',
// Driven by the press-toggle spec, not photographed: the states under test
// are pins and closes across presses, which no single frame shows.
tags: ['no-visual'],
parameters: {
docs: {
description: {
component:
'A button whose only job is its tooltip takes `pressTrigger="toggle"`: a click, a tap, Enter, or Space pins the tooltip, and the same press closes it. Inside a clickable card, the press stops at the trigger.',
},
},
},
};

export default meta;

type Story = StoryObj;

/** A press pins the tooltip open; the same press, Escape, or a press outside closes it. */
export const ButtonToggle: Story = {
render: () => (
<div style={{padding: 100}}>
<Tooltip
content="Rows are ranked by how many places import the icon"
placement="above"
pressTrigger="toggle">
<Button label="What is this?" variant="secondary">
?
</Button>
</Tooltip>
</div>
),
};

function InAClickableCardRender() {
const [opened, setOpened] = useState(0);
return (
<div style={{padding: 100, maxWidth: 360}}>
<VStack gap={2}>
<ClickableCard label="Icon usage" onClick={() => setOpened(n => n + 1)}>
<VStack gap={2}>
Icon usage
<Tooltip
content="Rows are ranked by how many places import the icon"
placement="above"
pressTrigger="toggle">
<Button label="What is this?" variant="secondary">
?
</Button>
</Tooltip>
</VStack>
</ClickableCard>
<span>Card opened {opened} times</span>
</VStack>
</div>
);
}

/** A press on the trigger pins its tooltip and stops there: the card does not open. */
export const InAClickableCard: Story = {
render: () => <InAClickableCardRender />,
};

function AnchoredActionInACardRender() {
const [opened, setOpened] = useState(0);
const [pressed, setPressed] = useState(0);
const anchorRef = useRef<HTMLButtonElement>(null);
return (
<div style={{padding: 100, maxWidth: 360}}>
<VStack gap={2}>
<ClickableCard label="Icon usage" onClick={() => setOpened(n => n + 1)}>
<VStack gap={2}>
Icon usage
<Button
ref={anchorRef}
label="What is this?"
variant="secondary"
onClick={() => setPressed(n => n + 1)}>
?
</Button>
<Tooltip
anchorRef={anchorRef}
content="Rows are ranked by how many places import the icon"
placement="above"
pressTrigger="toggle"
/>
</VStack>
</ClickableCard>
<span>Card opened {opened} times</span>
<span>Button pressed {pressed} times</span>
</VStack>
</div>
);
}

/**
* An anchored trigger with its own click handler, in a clickable card: the
* pinning press runs the trigger's own handler, and the card ignores it.
*/
export const AnchoredActionInACard: Story = {
render: () => <AnchoredActionInACardRender />,
};

/** A tooltip that opens on no focus: focus moving to another control still closes a pin. */
export const FocusTriggerNever: Story = {
render: () => (
<div style={{padding: 100}}>
<VStack gap={2}>
<Tooltip
content="Rows are ranked by how many places import the icon"
placement="above"
pressTrigger="toggle"
focusTrigger="never">
<Button label="What is this?" variant="secondary">
?
</Button>
</Tooltip>
<Button label="Next" variant="secondary">
Next
</Button>
</VStack>
</div>
),
};
Loading
Loading