Skip to content

Commit

Permalink
feat: focusNavigation API
Browse files Browse the repository at this point in the history
  • Loading branch information
georgylobko committed Jan 24, 2025
1 parent 63e9618 commit 0cf8021
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
17 changes: 12 additions & 5 deletions pages/page-layout/default.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function WithDrawers() {
const { urlParams, setUrlParams } = useContext(AppContext as DemoContext);
const [isToolsOpen, setIsToolsOpen] = useState(false);
const [isNavigationOpen, setIsNavigationOpen] = useState(true);
const appLayoutRef = useRef<PageLayoutProps.Ref>(null);
const pageLayoutRef = useRef<PageLayoutProps.Ref>(null);

const drawersProps: Pick<PageLayoutProps, 'activeDrawerId' | 'onDrawerChange' | 'drawers'> | null = {
activeDrawerId: activeDrawerId,
Expand All @@ -49,7 +49,7 @@ export default function WithDrawers() {
<PageLayout
ariaLabels={{ ...appLayoutLabels, ...drawerLabels }}
breadcrumbs={<Breadcrumbs />}
ref={appLayoutRef}
ref={pageLayoutRef}
content={
<ContentLayout
disableOverlap={true}
Expand All @@ -65,7 +65,7 @@ export default function WithDrawers() {
onFollow={() => {
setHelpPathSlug('header');
setIsToolsOpen(true);
appLayoutRef.current?.focusToolsClose();
pageLayoutRef.current?.focusToolsClose();
}}
>
Info
Expand All @@ -76,12 +76,19 @@ export default function WithDrawers() {
</Header>

<SpaceBetween size="xs">
<Button onClick={() => setIsNavigationOpen(current => !current)}>Toggle navigation</Button>
<Button
onClick={() => {
setIsNavigationOpen(current => !current);
pageLayoutRef.current?.focusNavigation();
}}
>
Toggle navigation
</Button>

<Button
onClick={() => {
setActiveDrawerId('pro-help');
appLayoutRef.current?.focusActiveDrawer();
pageLayoutRef.current?.focusActiveDrawer();
}}
>
Open a drawer without trigger
Expand Down
3 changes: 2 additions & 1 deletion src/app-layout/visual-refresh-toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ const AppLayoutVisualRefreshToolbar = React.forwardRef<AppLayoutProps.Ref, AppLa

const globalDrawersFocusControl = useMultipleFocusControl(true, activeGlobalDrawersIds);
const drawersFocusControl = useFocusControl(!!activeDrawer?.id, true, activeDrawer?.id);
const navigationFocusControl = useFocusControl(navigationOpen);
const navigationFocusControl = useFocusControl(navigationOpen, navigationTriggerHide);
const splitPanelFocusControl = useSplitPanelFocusControl([splitPanelPreferences, splitPanelOpen]);

const onNavigationToggle = useStableCallback((open: boolean) => {
Expand All @@ -235,6 +235,7 @@ const AppLayoutVisualRefreshToolbar = React.forwardRef<AppLayoutProps.Ref, AppLa
focusToolsClose: () => drawersFocusControl.setFocus(true),
focusActiveDrawer: () => drawersFocusControl.setFocus(true),
focusSplitPanel: () => splitPanelFocusControl.refs.slider.current?.focus(),
focusNavigation: () => navigationFocusControl.setFocus(true),
}));

const resolvedStickyNotifications = !!stickyNotifications && !isMobile;
Expand Down
11 changes: 10 additions & 1 deletion src/page-layout/__tests__/page-layout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@ describe('PageLayout', () => {
test('triggerless navigation', () => {
const PageLayoutWrapper = () => {
const [isNavigationOpen, setIsNavigationOpen] = useState(false);
const pageLayoutRef = useRef<PageLayoutProps.Ref>(null);

return (
<PageLayout
ref={pageLayoutRef}
navigationTriggerHide={true}
navigationOpen={isNavigationOpen}
onNavigationChange={event => setIsNavigationOpen(event.detail.open)}
navigation={<>Mock Navigation</>}
content={
<div>
Content
<button data-testid="toggle-navigation" onClick={() => setIsNavigationOpen(current => !current)}>
<button
data-testid="toggle-navigation"
onClick={() => {
setIsNavigationOpen(current => !current);
pageLayoutRef.current?.focusNavigation();
}}
>
Toggle navigation
</button>
</div>
Expand All @@ -43,6 +51,7 @@ describe('PageLayout', () => {
wrapper.find(`[data-testid="toggle-navigation"]`)!.click();

expect(wrapper.findOpenNavigationPanel()).toBeTruthy();
expect(wrapper.findNavigationClose()!.getElement()).toHaveFocus();
});

test('triggerless drawers', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/page-layout/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ export interface PageLayoutProps extends Omit<AppLayoutProps, 'disableBodyScroll
export namespace PageLayoutProps {
export type AnalyticsMetadata = AppLayoutProps.AnalyticsMetadata;
export type ContentType = AppLayoutProps.ContentType;
export type Ref = AppLayoutProps.Ref;
export interface Ref extends AppLayoutProps.Ref {
/**
* Focuses the navigation. Use this to focus the navigation after opening it programmatically.
*/
focusNavigation(): void;
}
export type Drawer = AppLayoutProps.Drawer;
export type DrawerAriaLabels = AppLayoutProps.DrawerAriaLabels;
export type Labels = AppLayoutProps.Labels;
Expand Down

0 comments on commit 0cf8021

Please sign in to comment.