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

Example story for navigator in modal #58933

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
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
157 changes: 155 additions & 2 deletions packages/components/src/modal/stories/index.story.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* External dependencies
*/
import type { CSSProperties } from 'react';
import type { StoryFn, Meta } from '@storybook/react';

/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { starEmpty, starFilled } from '@wordpress/icons';
import { useId, useState } from '@wordpress/element';
import { chevronLeft, close, starEmpty, starFilled } from '@wordpress/icons';

/**
* Internal dependencies
Expand All @@ -16,6 +17,13 @@ import Button from '../../button';
import InputControl from '../../input-control';
import Modal from '../';
import type { ModalProps } from '../types';
import {
NavigatorProvider,
NavigatorScreen,
NavigatorButton,
NavigatorToParentButton,
useNavigator,
} from '../../navigator';

const meta: Meta< typeof Modal > = {
component: Modal,
Expand Down Expand Up @@ -123,3 +131,148 @@ WithHeaderActions.args = {
WithHeaderActions.parameters = {
...Default.parameters,
};

export const WithNavigator: StoryFn< typeof Modal > = () => {
const fonts = new Map( [
[ 'a', { name: 'Font A', variants: [ 'Italic 100', 'Regular 400' ] } ],
[ 'b', { name: 'Font B', variants: [ 'Regular 100', 'Regular 400' ] } ],
[ 'c', { name: 'Font C', variants: [ 'Regular 100', 'Bold 100' ] } ],
] );

const [ isOpen, setOpen ] = useState( false );
const openModal = () => setOpen( true );
const closeModal = () => setOpen( false );
const modalHeaderId = useId();
const headerStyle: CSSProperties = { display: 'flex' };
const titleStyle: CSSProperties = { margin: 0 };
const listStyle: CSSProperties = {
listStyle: 'none',
margin: '1em 0',
padding: 0,
};
const itemStyle: CSSProperties = {
display: 'flex',
padding: '6px 12px',
fontSize: 14,
alignItems: 'stretch',
boxSizing: 'border-box',
justifyContent: 'space-between',
};

function FontNavigator() {
return (
<NavigatorProvider initialPath="/">
<NavigatorScreen path="/">
<FontList />
</NavigatorScreen>

<NavigatorScreen path="/:font">
<FontPage />
</NavigatorScreen>
</NavigatorProvider>
);
}

function FontList() {
return (
<div>
<header style={ headerStyle }>
<h1 id={ modalHeaderId } style={ titleStyle }>
Font Manager
</h1>
</header>
<ol style={ listStyle }>
{ Array.from( fonts.entries(), ( [ id, { name } ] ) => (
<li>
<NavigatorButton
path={ `/${ id }` }
key={ id }
style={ itemStyle }
>
{ name }
</NavigatorButton>
</li>
) ) }
</ol>
</div>
);
}

function FontPage() {
const {
params: { font: id },
} = useNavigator();
const font = fonts.get( id.toString() );

if ( ! font ) {
return (
<div>
<header style={ headerStyle }>
<NavigatorToParentButton
icon={ chevronLeft }
label="Back"
/>
<h1 id={ modalHeaderId } style={ titleStyle }>
Not found
</h1>
</header>
</div>
);
}

const { name, variants } = font;

return (
<div>
<header style={ headerStyle }>
<NavigatorToParentButton
icon={ chevronLeft }
label="Back"
/>
<h1 id={ modalHeaderId } style={ titleStyle }>
{ name }
</h1>
</header>
<ul style={ listStyle }>
{ variants.map( ( variant, index ) => (
<li key={ index }>
{ /* eslint-disable-next-line jsx-a11y/label-has-associated-control */ }
<label style={ itemStyle }>
{ variant }
<input type="checkbox" />
</label>
</li>
) ) }
</ul>
</div>
);
}

return (
<>
<Button variant="secondary" onClick={ openModal }>
Open Font Manager
</Button>
{ isOpen && (
<Modal
__experimentalHideHeader={ true }
aria-labelledby={ modalHeaderId }
size="large"
onRequestClose={ closeModal }
>
<FontNavigator />
<Button
icon={ close }
label="Close"
onClick={ closeModal }
style={ {
position: 'absolute',
right: 32,
top: 32,
} }
/>
</Modal>
) }
</>
);
};
Loading