diff --git a/packages/lab/src/NavigationStack/NavigationStack.test.tsx b/packages/lab/src/NavigationStack/NavigationStack.test.tsx index eae6a99239ee8..9ce74114120ad 100644 --- a/packages/lab/src/NavigationStack/NavigationStack.test.tsx +++ b/packages/lab/src/NavigationStack/NavigationStack.test.tsx @@ -14,7 +14,7 @@ */ import {describe, expect, it} from 'vitest'; -import {useState, type ReactNode} from 'react'; +import {useRef, useState, type ReactNode} from 'react'; import {act, fireEvent, render, screen, waitFor} from '@testing-library/react'; import {List, ListItem} from '@astryxdesign/core/List'; import { @@ -400,6 +400,58 @@ describe('NavigationStack', () => { ); }); + it('returns focus to the control push names in sourceRef', async () => { + function SourceRefRoot() { + const nav = useNavigationStack(); + const opener = useRef(null); + return ( + <> + + + + ); + } + function SourceRefHarness() { + const [path, setPath] = useState([]); + return ( + + + + } + path={path} + onPathChange={setPath} + getKey={entry => entry} + renderScreen={entry => ( + +

{entry} content

+
+ )} + /> + ); + } + render(); + fireEvent.click(screen.getByRole('button', {name: 'Push'})); + await screen.findByRole('heading', {name: 'General', level: 2}); + + fireEvent.click(backButton('Settings') as HTMLElement); + await waitFor( + () => expect(screen.queryByText('General content')).toBeNull(), + {timeout: 3000}, + ); + await waitFor(() => + expect(screen.getByRole('button', {name: 'Open later'})).toHaveFocus(), + ); + }); + it('throws when useNavigationStack is used outside a stack', () => { function Orphan() { useNavigationStack(); diff --git a/packages/lab/src/NavigationStack/NavigationStack.tsx b/packages/lab/src/NavigationStack/NavigationStack.tsx index ca03fb6881e4f..2ac3abe85fc00 100644 --- a/packages/lab/src/NavigationStack/NavigationStack.tsx +++ b/packages/lab/src/NavigationStack/NavigationStack.tsx @@ -32,6 +32,7 @@ import { useState, type ReactElement, type ReactNode, + type RefObject, type SyntheticEvent, } from 'react'; import * as stylex from '@stylexjs/stylex'; @@ -123,9 +124,10 @@ export interface NavigationStackProps { export interface NavigationStackApi { /** * Pushes an entry. The control that started it (the click or key press - * being handled, or `options.source`) gets focus back when its screen pops. + * being handled, or the element `options.sourceRef` holds) gets focus back + * when its screen pops. */ - push: (entry: T, options?: {source?: Element | null}) => void; + push: (entry: T, options?: {sourceRef?: RefObject}) => void; /** Pops the top screen; nothing at the root. */ pop: () => void; /** Pops back to the root. */ @@ -1590,8 +1592,8 @@ export function NavigationStack({ push: (entry, options) => { const last = lastActivationRef.current; const source = - options?.source !== undefined - ? options.source + options?.sourceRef !== undefined + ? options.sourceRef.current : last != null && performance.now() - last.at < 1000 ? last.target : null;