Skip to content

Commit f88f5dc

Browse files
authored
fix(HoverCard): focusTrigger="auto" decides when focus arrives, through Tooltip's shared check (#9597)
* fix(HoverCard): focusTrigger="auto" decides when focus arrives, through Tooltip's shared check component:HoverCard Current-state impact ("A roving trigger may not open on focus"), architecture:interaction-modality INV4. useHoverCard read the tab order only when its listeners attached, so a roving trigger rendered with tabindex="-1" never opened on keyboard focus until a re-render, and a focusable trigger the check does not list (<summary>, an SVG link) never did. Tooltip already judges focus per event. That check moves to utils/triggerFocus.ts (isFocusableTrigger, isFocusForTrigger) and both hooks call it: focus listeners attach unless focusTrigger is "never", each focusin is judged when it arrives, and a focus that counted still hides on blur after the trigger left the tab order. Tooltip's behavior and tests are unchanged. Tests: roving, <summary> (red before), plain wrapper, and left-the-tab-order. Record: the Current-state bullet is dropped and the FR4-FR6 row names the cases. * fix(HoverCard, Tooltip): one shared focus gate, and the docs say auto judges focus when it arrives CodeBunny on #9597: - The counted-focus memory moves into utils/triggerFocus.ts as createTriggerFocusGate (focusIn, focusOut); useTooltip and useHoverCard each keep one gate and call it, with no local copy. - HoverCard's record drops FR4's gap sentence for the behavior, the Focus trigger row says it is judged when focus arrives, and the readiness count drops to seven; useHoverCard.doc.mjs, HoverCard.doc.mjs (both languages) and the focusTrigger JSDoc use Tooltip's wording. - A new case covers focus inside a focusable trigger that leaves the tab order, which needs the counted memory. * Merge main into lane4/hovercard-focus-trigger-auto * Merge commit '381c0d272a72a1a8ec810d3f711235094725f087' into lane4/hovercard-focus-trigger-auto # Conflicts: # docs/specs/components/HoverCard/HoverCard.spec.md * Merge commit 'ae97fa8fc03702d3e60c6e8a4279d9f8e89314bb' into lane4/hovercard-focus-trigger-auto # Conflicts: # docs/specs/components/HoverCard/HoverCard.spec.md
1 parent 275a3f8 commit f88f5dc

9 files changed

Lines changed: 463 additions & 172 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[fix] Under `focusTrigger="auto"`, a `HoverCard` now decides whether focus opens it when the focus arrives, as `Tooltip` does. A roving-tabindex trigger that was `tabindex="-1"` when the card attached, such as a toolbar member, opens on arrow-key focus, and a focusable trigger such as a `<summary>` or an SVG link opens on focus.
6+
7+
@cixzhang @duytbp

‎docs/specs/components/HoverCard/HoverCard.spec.md‎

Lines changed: 21 additions & 28 deletions
Large diffs are not rendered by default.

‎packages/core/src/HoverCard/HoverCard.doc.mjs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ export const docs = {
9898
{
9999
name: 'focusTrigger',
100100
type: "'auto' | 'always' | 'never'",
101-
description: 'Controls when focus events trigger the hover card.',
101+
description:
102+
'When focus should open the hover card. auto opens when the trigger itself takes focus, or focus lands inside a trigger that is focusable at that moment, so a roving-tabindex member opens when the arrow keys reach it.',
102103
default: "'auto'",
103104
},
104105
{
@@ -303,7 +304,8 @@ export const docsZh = {
303304
{
304305
name: 'focusTrigger',
305306
type: "'auto' | 'always' | 'never'",
306-
description: '控制焦点事件何时触发悬浮卡片。',
307+
description:
308+
'焦点何时打开悬浮卡片。auto 在触发元素自身获得焦点,或焦点落在当时可聚焦的触发元素内部时打开,因此方向键到达漫游 tabindex 成员时也会打开。',
307309
default: "'auto'",
308310
},
309311
{

‎packages/core/src/HoverCard/HoverCard.test.tsx‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,118 @@ describe('useHoverCard closing while the pointer is on the card', () => {
170170
});
171171
});
172172

173+
describe('useHoverCard — focusTrigger="auto" decides when focus arrives', () => {
174+
it('opens on focus of a trigger that was tabindex="-1" when it attached', async () => {
175+
const onShow = vi.fn();
176+
function RovingMember() {
177+
const card = useHoverCard({onShow, delay: 0});
178+
return (
179+
<div>
180+
{/* A roving-tabindex member that is not the tab stop when the
181+
card attaches. */}
182+
<button type="button" tabIndex={-1} ref={card.ref}>
183+
Filter
184+
</button>
185+
{card.renderHoverCard(<span>Card</span>)}
186+
</div>
187+
);
188+
}
189+
render(<RovingMember />);
190+
const member = screen.getByRole('button', {name: 'Filter'});
191+
// The roving hook moves the tab stop, then focus, as an arrow key does.
192+
member.setAttribute('tabindex', '0');
193+
fireEvent.focusIn(member);
194+
await waitFor(() => expect(onShow).toHaveBeenCalledTimes(1));
195+
});
196+
197+
it('opens on focus of a focusable trigger it does not list, such as a summary', async () => {
198+
const onShow = vi.fn();
199+
function SummaryTrigger() {
200+
const card = useHoverCard({onShow, delay: 0});
201+
return (
202+
<details>
203+
<summary ref={card.ref}>More</summary>
204+
{card.renderHoverCard(<span>Card</span>)}
205+
</details>
206+
);
207+
}
208+
render(<SummaryTrigger />);
209+
fireEvent.focusIn(screen.getByText('More'));
210+
await waitFor(() => expect(onShow).toHaveBeenCalledTimes(1));
211+
});
212+
213+
it('ignores focus inside a plain wrapper trigger', async () => {
214+
const onShow = vi.fn();
215+
function WrapperTrigger() {
216+
const card = useHoverCard({onShow, delay: 0});
217+
return (
218+
<div>
219+
<div ref={card.ref}>
220+
<input aria-label="Amount" />
221+
</div>
222+
{card.renderHoverCard(<span>Card</span>)}
223+
</div>
224+
);
225+
}
226+
render(<WrapperTrigger />);
227+
fireEvent.focusIn(screen.getByRole('textbox', {name: 'Amount'}));
228+
await act(async () => {
229+
await new Promise(resolve => setTimeout(resolve, 20));
230+
});
231+
expect(onShow).not.toHaveBeenCalled();
232+
});
233+
234+
it('hides on blur from inside a focusable trigger that left the tab order', async () => {
235+
const onShow = vi.fn();
236+
const onHide = vi.fn();
237+
function FocusableRow() {
238+
const card = useHoverCard({onShow, onHide, delay: 0, hideDelay: 0});
239+
return (
240+
<div>
241+
<div tabIndex={0} ref={card.ref} data-testid="row">
242+
<button type="button">Inside</button>
243+
</div>
244+
{card.renderHoverCard(<span>Card</span>)}
245+
</div>
246+
);
247+
}
248+
render(<FocusableRow />);
249+
const inside = screen.getByRole('button', {name: 'Inside'});
250+
// Focus inside a focusable trigger counts.
251+
fireEvent.focusIn(inside);
252+
await waitFor(() => expect(onShow).toHaveBeenCalledTimes(1));
253+
// The trigger leaves the tab order before focus moves on: the focus that
254+
// counted still hides the card.
255+
screen.getByTestId('row').setAttribute('tabindex', '-1');
256+
fireEvent.focusOut(inside);
257+
await waitFor(() => expect(onHide).toHaveBeenCalledTimes(1));
258+
});
259+
260+
it('hides on blur of a trigger that left the tab order while focused', async () => {
261+
const onShow = vi.fn();
262+
const onHide = vi.fn();
263+
function RovingMember() {
264+
const card = useHoverCard({onShow, onHide, delay: 0, hideDelay: 0});
265+
return (
266+
<div>
267+
<button type="button" ref={card.ref}>
268+
Filter
269+
</button>
270+
{card.renderHoverCard(<span>Card</span>)}
271+
</div>
272+
);
273+
}
274+
render(<RovingMember />);
275+
const member = screen.getByRole('button', {name: 'Filter'});
276+
fireEvent.focusIn(member);
277+
await waitFor(() => expect(onShow).toHaveBeenCalledTimes(1));
278+
// The roving hook takes the tab stop away before focus moves on.
279+
member.setAttribute('tabindex', '-1');
280+
fireEvent.focusOut(member);
281+
await waitFor(() => expect(onHide).toHaveBeenCalledTimes(1));
282+
});
283+
});
284+
173285
describe('HoverCard', () => {
174286
it('renders trigger element', () => {
175287
render(

‎packages/core/src/HoverCard/HoverCard.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@ export interface HoverCardProps extends Pick<
155155

156156
/**
157157
* When to trigger on focus:
158-
* - `auto`: Only if element is naturally focusable
159-
* - `always`: Always attach focus listeners
160-
* - `never`: Never attach focus listeners (for composite widgets)
158+
* - `auto`: focus on the trigger itself, or within a trigger that is
159+
* focusable when focus arrives
160+
* - `always`: focus on the trigger or anything inside it
161+
* - `never`: never on focus (for composite widgets)
161162
*
162163
* @default 'auto'
163164
*/

0 commit comments

Comments
 (0)