Skip to content

Commit

Permalink
Components: refactor Navigator* folder structure, rename `Navigator…
Browse files Browse the repository at this point in the history
…` to `NavigatorProvider` (#35160)

* Rename `Navigator` component to `NavigatorRoot`, move code under `navigator-root` folder

* Move `NavigatorScreen` under `navigator-screen` folder, add README

* Rename `NavigatorRoot` to  `NavigatorProvider`
  • Loading branch information
ciampo authored Sep 28, 2021
1 parent 341473b commit d3e907b
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 46 deletions.
12 changes: 9 additions & 3 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1068,9 +1068,15 @@
"parent": "components"
},
{
"title": "Navigator",
"slug": "navigator",
"markdown_source": "../packages/components/src/navigator/README.md",
"title": "NavigatorProvider",
"slug": "navigator-provider",
"markdown_source": "../packages/components/src/navigator/navigator-provider/README.md",
"parent": "components"
},
{
"title": "NavigatorScreen",
"slug": "navigator-screen",
"markdown_source": "../packages/components/src/navigator/navigator-screen/README.md",
"parent": "components"
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export { default as __experimentalNavigationGroup } from './navigation/group';
export { default as __experimentalNavigationItem } from './navigation/item';
export { default as __experimentalNavigationMenu } from './navigation/menu';
export {
Navigator as __experimentalNavigator,
NavigatorProvider as __experimentalNavigatorProvider,
NavigatorScreen as __experimentalNavigatorScreen,
useNavigator as __experimentalUseNavigator,
} from './navigator';
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/navigator/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { default as Navigator } from './navigator';
export { default as NavigatorScreen } from './screen';
export { default as NavigatorProvider } from './navigator-provider';
export { default as NavigatorScreen } from './navigator-screen';
export { default as useNavigator } from './use-navigator';
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Navigator
# `NavigatorProvider`

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

The Navigator components allows rendering nested panels or menus (also called screens) and navigate between these different states. The Global Styles sidebar is an example of this.
The `NavigatorProvider` component allows rendering nested panels or menus (via the [`NavigatorScreen` component](/packages/components/src/navigator/navigator-screen/README.md)) and navigate between these different states (via the `useNavigator` hook). The Global Styles sidebar is an example of this.

The components is not opinionated in terms of UI, it lets compose any UI components to navigate between the nested screens.
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

```jsx
import {
__experimentalNavigator as Navigator,
__experimentalNavigatorProvider as NavigatorProvider,
__experimentalNavigatorScreen as NavigatorScreen,
__experimentalUseNavigator as useNavigator,
} from '@wordpress/components';
Expand All @@ -32,7 +32,7 @@ function NavigatorButton( {
}

const MyNavigation = () => (
<Navigator initialPath="/">
<NavigatorProvider initialPath="/">
<NavigatorScreen path="/">
<p>This is the home screen.</p>
<NavigatorButton isPrimary path="/child">
Expand All @@ -46,43 +46,31 @@ const MyNavigation = () => (
Go back
</NavigatorButton>
</NavigatorScreen>
</Navigator>
</NavigatorProvider>
);
```

## Navigator Props
## Props

`Navigator` supports the following props.
The component accepts the following props:

### `initialPath`

- Type: `string`
- Required: No
### `initialPath`: `string`

The initial active path.

## NavigatorScreen Props

`NavigatorScreen` supports the following props.

### `path`

- Type: `string`
- Required: Yes
- Required: No

The path of the current screen.

## The navigator object.
## The navigator object

You can retrieve a `navigator` instance by using the `useNavigator` hook.
The navigator offers the following methods:

### `push`
The hook offers the following methods:

- Type: `( path: string, options ) => void`
### `push`: `( path: string, options ) => void`

The `push` function allows you to navigate to a given path. The second argument can augment the navigation operations with different options.

The available options are:

- `isBack` (`boolean): A boolean flag indicating that we're moving back to a previous state.
- `isBack` (`boolean): A boolean flag indicating that we're moving back to a previous state. -->
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import { NavigatorContext } from './context';
import { NavigatorContext } from '../context';

function Navigator( { initialPath, children } ) {
function NavigatorProvider( { initialPath, children } ) {
const [ path, setPath ] = useState( { path: initialPath } );

return (
Expand All @@ -18,4 +18,4 @@ function Navigator( { initialPath, children } ) {
);
}

export default Navigator;
export default NavigatorProvider;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './component';
22 changes: 22 additions & 0 deletions packages/components/src/navigator/navigator-screen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# `NavigatorScreen`

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

The `NavigatorScreen` component represents a single view/screen/panel/menu and is supposed to be used in combination with [the `NavigatorProvider` component](/packages/components/src/navigator/navigator-provider/README.md).

## Usage

Refer to [the `NavigatorProvider` component](/packages/components/src/navigator/navigator-provider/README.md#usage) for a usage example.

## Props

The component accepts the following props:

### `path`

- Type: `string`
- Required: Yes

The path of the current screen.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { isRTL } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { NavigatorContext } from './context';
import { NavigatorContext } from '../context';

const animationEnterDelay = 0;
const animationEnterDuration = 0.14;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './component';
8 changes: 4 additions & 4 deletions packages/components/src/navigator/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Internal dependencies
*/
import Button from '../../button';
import { Navigator, NavigatorScreen, useNavigator } from '../';
import { NavigatorProvider, NavigatorScreen, useNavigator } from '../';

export default {
title: 'Components (Experimental)/Navigator',
component: Navigator,
component: NavigatorProvider,
};

function NavigatorButton( { path, isBack = false, ...props } ) {
Expand All @@ -20,7 +20,7 @@ function NavigatorButton( { path, isBack = false, ...props } ) {
}

const MyNavigation = () => (
<Navigator initialPath="/">
<NavigatorProvider initialPath="/">
<NavigatorScreen path="/">
<p>This is the home screen.</p>
<NavigatorButton isPrimary path="/child">
Expand All @@ -34,7 +34,7 @@ const MyNavigation = () => (
Go back
</NavigatorButton>
</NavigatorScreen>
</Navigator>
</NavigatorProvider>
);

export const _default = () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/navigator/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { render, screen, fireEvent } from '@testing-library/react';
/**
* Internal dependencies
*/
import { Navigator, NavigatorScreen, useNavigator } from '../';
import { NavigatorProvider, NavigatorScreen, useNavigator } from '../';

jest.mock( 'framer-motion', () => {
const actual = jest.requireActual( 'framer-motion' );
Expand Down Expand Up @@ -47,7 +47,7 @@ const MyNavigation = ( {
initialPath = PATHS.HOME,
onNavigatorButtonClick,
} ) => (
<Navigator initialPath={ initialPath }>
<NavigatorProvider initialPath={ initialPath }>
<NavigatorScreen path={ PATHS.HOME }>
<p>This is the home screen.</p>
<NavigatorButton
Expand Down Expand Up @@ -76,7 +76,7 @@ const MyNavigation = ( {
</NavigatorScreen>

{ /* A `NavigatorScreen` with `path={ PATHS.NOT_FOUND }` is purposefully not included */ }
</Navigator>
</NavigatorProvider>
);

const getNavigationScreenByText = ( text, { throwIfNotFound = true } = {} ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { map } from 'lodash';
*/
import {
Button,
__experimentalNavigator as Navigator,
__experimentalNavigatorProvider as NavigatorProvider,
__experimentalNavigatorScreen as NavigatorScreen,
__experimentalItemGroup as ItemGroup,
__experimentalItem as Item,
Expand Down Expand Up @@ -213,7 +213,7 @@ export default function GlobalStylesSidebar() {
</>
}
>
<Navigator initialPath="/">
<NavigatorProvider initialPath="/">
<NavigatorScreen path="/">
<StylePreview />

Expand Down Expand Up @@ -280,7 +280,7 @@ export default function GlobalStylesSidebar() {
setSetting={ setSetting }
/>
) ) }
</Navigator>
</NavigatorProvider>
</DefaultSidebar>
);
}

0 comments on commit d3e907b

Please sign in to comment.