Skip to content
Draft
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
54 changes: 53 additions & 1 deletion packages/lab/src/NavigationStack/NavigationStack.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -400,6 +400,58 @@ describe('NavigationStack', () => {
);
});

it('returns focus to the control push names in sourceRef', async () => {
function SourceRefRoot() {
const nav = useNavigationStack<string>();
const opener = useRef<HTMLButtonElement>(null);
return (
<>
<button ref={opener} type="button">
Open later
</button>
<button
type="button"
onClick={() => nav.push('General', {sourceRef: opener})}>
Push
</button>
</>
);
}
function SourceRefHarness() {
const [path, setPath] = useState<string[]>([]);
return (
<NavigationStack
label="Settings"
root={
<NavigationScreen title="Settings">
<SourceRefRoot />
</NavigationScreen>
}
path={path}
onPathChange={setPath}
getKey={entry => entry}
renderScreen={entry => (
<NavigationScreen title={entry}>
<p>{entry} content</p>
</NavigationScreen>
)}
/>
);
}
render(<SourceRefHarness />);
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();
Expand Down
10 changes: 6 additions & 4 deletions packages/lab/src/NavigationStack/NavigationStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
useState,
type ReactElement,
type ReactNode,
type RefObject,
type SyntheticEvent,
} from 'react';
import * as stylex from '@stylexjs/stylex';
Expand Down Expand Up @@ -123,9 +124,10 @@ export interface NavigationStackProps<T> {
export interface NavigationStackApi<T = unknown> {
/**
* 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<Element | null>}) => void;
/** Pops the top screen; nothing at the root. */
pop: () => void;
/** Pops back to the root. */
Expand Down Expand Up @@ -1590,8 +1592,8 @@ export function NavigationStack<T>({
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;
Expand Down