Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigator: add basic history support, enable advanced focus restoration #37223

Closed
wants to merge 17 commits into from
Closed
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
12 changes: 12 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,18 @@
"markdown_source": "../packages/components/src/navigation/README.md",
"parent": "components"
},
{
"title": "NavigatorBackLink",
"slug": "navigator-back-link",
"markdown_source": "../packages/components/src/navigator/navigator-back-link/README.md",
"parent": "components"
},
{
"title": "NavigatorLink",
"slug": "navigator-link",
"markdown_source": "../packages/components/src/navigator/navigator-link/README.md",
"parent": "components"
},
{
"title": "NavigatorProvider",
"slug": "navigator-provider",
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export { default as __experimentalNavigationMenu } from './navigation/menu';
export {
NavigatorProvider as __experimentalNavigatorProvider,
NavigatorScreen as __experimentalNavigatorScreen,
NavigatorLink as __experimentalNavigatorLink,
NavigatorBackLink as __experimentalNavigatorBackLink,
useNavigator as __experimentalUseNavigator,
} from './navigator';
export { default as Notice } from './notice';
Expand Down
6 changes: 5 additions & 1 deletion packages/components/src/navigator/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ import { createContext } from '@wordpress/element';
*/
import type { NavigatorContext as NavigatorContextType } from './types';

const initialContextValue: NavigatorContextType = [ {}, () => {} ];
const initialContextValue: NavigatorContextType = {
location: {},
push: () => {},
pop: () => {},
};
export const NavigatorContext = createContext( initialContextValue );
2 changes: 2 additions & 0 deletions packages/components/src/navigator/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { default as NavigatorProvider } from './navigator-provider';
export { default as NavigatorScreen } from './navigator-screen';
export { default as NavigatorLink } from './navigator-link';
export { default as NavigatorBackLink } from './navigator-back-link';
export { default as useNavigator } from './use-navigator';
19 changes: 19 additions & 0 deletions packages/components/src/navigator/navigator-back-link/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# `NavigatorBackLink`

<div class="callout callout-alert">
This feature is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes.
</div>

**TODO**

The `Navigator*` family of components is _not_ opinionated in terms of UI, and can be composed with any UI components to navigate between the nested screens.

## Usage

**TODO**

## Props

The component accepts the following props:

**TODO**
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import type { Ref } from 'react';

/**
* Internal dependencies
*/
import { contextConnect, WordPressComponentProps } from '../../ui/context';
import { View } from '../../view';
import { useNavigatorBackLink } from './hook';
import type { NavigatorBackLinkProps } from '../types';

function NavigatorBackLink(
props: WordPressComponentProps< NavigatorBackLinkProps, 'button' >,
forwardedRef: Ref< any >
) {
const navigatorBackLinkProps = useNavigatorBackLink( props );

return <View ref={ forwardedRef } { ...navigatorBackLinkProps } />;
}

/**
* TODO: add example
*/
const ConnectedNavigatorBackLink = contextConnect(
NavigatorBackLink,
'NavigatorBackLink'
);

export default ConnectedNavigatorBackLink;
37 changes: 37 additions & 0 deletions packages/components/src/navigator/navigator-back-link/hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* WordPress dependencies
*/
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
import { useContextSystem, WordPressComponentProps } from '../../ui/context';
import Button from '../../button';
import useNavigator from '../use-navigator';
import type { NavigatorBackLinkProps } from '../types';

export function useNavigatorBackLink(
props: WordPressComponentProps< NavigatorBackLinkProps, 'button' >
) {
const { onClick, as = Button, ...otherProps } = useContextSystem(
props,
'NavigatorBackLink'
);

const { pop } = useNavigator();
const handleClick: React.MouseEventHandler< HTMLElement > = useCallback(
( e ) => {
e.preventDefault();
pop();
onClick?.( e );
},
[ pop, onClick ]
);

return {
as,
onClick: handleClick,
...otherProps,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './component';
19 changes: 19 additions & 0 deletions packages/components/src/navigator/navigator-link/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# `NavigatorLink`

<div class="callout callout-alert">
This feature is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes.
</div>

**TODO**

The `Navigator*` family of components is _not_ opinionated in terms of UI, and can be composed with any UI components to navigate between the nested screens.

## Usage

**TODO**

## Props

The component accepts the following props:

**TODO**
29 changes: 29 additions & 0 deletions packages/components/src/navigator/navigator-link/component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import type { Ref } from 'react';

/**
* Internal dependencies
*/
import { contextConnect, WordPressComponentProps } from '../../ui/context';
import { View } from '../../view';
import { useNavigatorLink } from './hook';
import type { NavigatorLinkProps } from '../types';

function NavigatorLink(
props: WordPressComponentProps< NavigatorLinkProps, 'button' >,
forwardedRef: Ref< any >
) {
const navigatorLinkProps = useNavigatorLink( props );

return <View ref={ forwardedRef } { ...navigatorLinkProps } />;
}

/**
* TODO: add example
*/
const ConnectedNavigatorLink = contextConnect( NavigatorLink, 'NavigatorLink' );

export default ConnectedNavigatorLink;
50 changes: 50 additions & 0 deletions packages/components/src/navigator/navigator-link/hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* WordPress dependencies
*/
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
import { useContextSystem, WordPressComponentProps } from '../../ui/context';
import Button from '../../button';
import useNavigator from '../use-navigator';
import type { NavigatorLinkProps } from '../types';

const cssSelectorForAttribute = ( attrName: string, attrValue: string ) =>
`[${ attrName }="${ attrValue }"]`;

export function useNavigatorLink(
props: WordPressComponentProps< NavigatorLinkProps, 'button' >
) {
const {
path,
onClick,
as = Button,
attributeName = 'id',
...otherProps
} = useContextSystem( props, 'NavigatorLink' );

const { push } = useNavigator();
const handleClick: React.MouseEventHandler< HTMLElement > = useCallback(
( e ) => {
e.preventDefault();
push( path, {
focusTargetSelector: cssSelectorForAttribute(
attributeName,
path
),
} );
onClick?.( e );
},
[ push, onClick ]
);

return {
as,
onClick: handleClick,
path,
...otherProps,
[ attributeName ]: path,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './component';
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { css } from '@emotion/react';
/**
* WordPress dependencies
*/
import { useMemo, useState } from '@wordpress/element';
import { useMemo, useState, useCallback } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -21,7 +21,11 @@ import {
import { useCx } from '../../utils/hooks/use-cx';
import { View } from '../../view';
import { NavigatorContext } from '../context';
import type { NavigatorProviderProps, NavigatorPath } from '../types';
import type {
NavigatorProviderProps,
NavigatorLocation,
NavigatorContext as NavigatorContextType,
} from '../types';

function NavigatorProvider(
props: WordPressComponentProps< NavigatorProviderProps, 'div' >,
Expand All @@ -34,9 +38,59 @@ function NavigatorProvider(
...otherProps
} = useContextSystem( props, 'NavigatorProvider' );

const [ path, setPath ] = useState< NavigatorPath >( {
path: initialPath,
} );
const [ locationHistory, setLocationHistory ] = useState<
NavigatorLocation[]
>( [
{
path: initialPath,
isBack: false,
isInitial: true,
},
] );

const push: NavigatorContextType[ 'push' ] = useCallback(
( path, options ) => {
const { focusTargetSelector, ...restOptions } = options;

// The `focusTargetSelector` needs to be applied on the current
// location, while the remaining options need to be applied on the
// incoming location
setLocationHistory( [
...locationHistory.slice( 0, -1 ),
{
...locationHistory[ locationHistory.length - 1 ],
isBack: false,
focusTargetSelector,
},
{
...restOptions,
path,
isBack: false,
isInitial: false,
},
] );
},
[ locationHistory ]
);

const pop: NavigatorContextType[ 'pop' ] = useCallback( () => {
if ( locationHistory.length > 1 ) {
setLocationHistory( [
...locationHistory.slice( 0, -2 ),
// Force the `isBack` flag to `true` when navigating back.
{
...locationHistory[ locationHistory.length - 2 ],
isBack: true,
},
] );
}
}, [ locationHistory ] );

const navigatorContextValue: NavigatorContextType = {
location: locationHistory[ locationHistory.length - 1 ],
push,
pop,
};

const cx = useCx();
const classes = useMemo(
Expand All @@ -47,7 +101,7 @@ function NavigatorProvider(

return (
<View ref={ forwardedRef } className={ classes } { ...otherProps }>
<NavigatorContext.Provider value={ [ path, setPath ] }>
<NavigatorContext.Provider value={ navigatorContextValue }>
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
{ children }
</NavigatorContext.Provider>
</View>
Expand Down
Loading