-
Notifications
You must be signed in to change notification settings - Fork 839
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
[Storybook] Create Stories for EuiSideNav
#7174
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
07ee9de
[Storybook] Create Storybook for EuiSideNav that includes a playgroun…
breehall 7433161
Merge branch 'main' into storybook/side-nav
cee-chen b42d650
[Storybook] Refactor stories and playground for EuiSideNav
breehall bbfea96
Update Storybook configuration to allow us to set default device size…
breehall b1f7c7e
Merge branch 'storybook/side-nav' of https://github.com/breehall/eui …
breehall 89df53e
[PR Feedback] Prefer Storybook decorator over inline styling on compo…
breehall bb55b25
[PR Feedback] Import order
breehall ea22f24
Merge remote-tracking branch 'upstream/main' into storybook/side-nav
breehall 23e6c3b
[PR Feedback] Code clean up and hone in on story focus
breehall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React, { useState } from 'react'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { | ||
EuiSideNav, | ||
EuiSideNavProps, | ||
EuiSideNavHeadingProps, | ||
} from './side_nav'; | ||
|
||
import { EuiIcon } from '../icon'; | ||
import { EuiText } from '../text'; | ||
|
||
const meta: Meta<EuiSideNavProps> = { | ||
title: 'EuiSideNav', | ||
component: EuiSideNav, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<EuiSideNavProps>; | ||
|
||
const componentDefaults: EuiSideNavProps = { | ||
mobileBreakpoints: ['xs', 's'], | ||
items: [], | ||
// Aria-label and mobileTitle do not have defaults; they are being set here as they are shared between examples | ||
'aria-label': 'Side navigation example', | ||
mobileTitle: 'Mobile navigation header', | ||
}; | ||
|
||
// Heading props shared across examples | ||
const _sharedHeadingProps: EuiSideNavHeadingProps = { | ||
element: 'h1', | ||
screenReaderOnly: false, | ||
}; | ||
|
||
export const Playground: Story = { | ||
args: { | ||
...componentDefaults, | ||
heading: 'Elastic', | ||
headingProps: _sharedHeadingProps, | ||
isOpenOnMobile: false, | ||
items: [ | ||
{ | ||
name: 'Kibana', | ||
id: 'kibana', | ||
icon: <EuiIcon type="logoKibana" />, | ||
items: [ | ||
{ | ||
name: 'Has nested children', | ||
id: 'normal_children', | ||
items: [ | ||
{ | ||
name: 'Child 1', | ||
id: 'child_1', | ||
items: [ | ||
{ | ||
name: 'Selected item', | ||
id: 'selected_item', | ||
onClick: () => {}, | ||
isSelected: true, | ||
items: [], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Has forceOpen: true', | ||
id: 'force_open', | ||
forceOpen: true, | ||
items: [ | ||
{ | ||
name: 'Child 3', | ||
id: 'child_3', | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Children only without link', | ||
id: 'children_only', | ||
onClick: undefined, | ||
items: [ | ||
{ | ||
name: 'Child 4', | ||
id: 'child_4', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
render: ({ ...args }) => ( | ||
<EuiSideNav | ||
{...args} | ||
css={{ width: '200px' }} // Required to view text truncation | ||
/> | ||
), | ||
}; | ||
|
||
export const MobileSideNav: Story = { | ||
args: { | ||
...componentDefaults, | ||
heading: 'Elastic', | ||
headingProps: _sharedHeadingProps, | ||
isOpenOnMobile: true, | ||
truncate: true, | ||
}, | ||
argTypes: { | ||
// This story demos the side nav on smaller screens; removing other props to prevent confusion | ||
'aria-label': { table: { disable: true } }, | ||
heading: { table: { disable: true } }, | ||
headingProps: { table: { disable: true } }, | ||
toggleOpenOnMobile: { table: { disable: true } }, | ||
isOpenOnMobile: { table: { disable: true } }, | ||
items: { table: { disable: true } }, | ||
renderItem: { table: { disable: true } }, | ||
truncate: { table: { disable: true } }, | ||
}, | ||
parameters: { | ||
viewport: { | ||
defaultViewport: 'mobile1', | ||
}, | ||
}, | ||
render: ({ ...args }) => <StatefulSideNav {...args} />, | ||
}; | ||
|
||
export const RenderItem: Story = { | ||
JasonStoltz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
args: { | ||
...componentDefaults, | ||
items: [ | ||
{ | ||
name: 'Kibana', | ||
id: 'kibana', | ||
renderItem: ({ children }) => ( | ||
<EuiText color="subdued">{children}</EuiText> | ||
), | ||
}, | ||
{ | ||
name: 'Observability', | ||
id: 'observability', | ||
}, | ||
{ | ||
name: 'Security', | ||
id: 'security', | ||
}, | ||
], | ||
renderItem: ({ children }) => <EuiText color="accent">{children}</EuiText>, | ||
heading: 'Navigation header', | ||
headingProps: _sharedHeadingProps, | ||
}, | ||
argTypes: { | ||
// This story demos the renderItem prop; removing other props to prevent confusion | ||
'aria-label': { table: { disable: true } }, | ||
heading: { table: { disable: true } }, | ||
headingProps: { table: { disable: true } }, | ||
toggleOpenOnMobile: { table: { disable: true } }, | ||
isOpenOnMobile: { table: { disable: true } }, | ||
mobileBreakpoints: { table: { disable: true } }, | ||
mobileTitle: { table: { disable: true } }, | ||
truncate: { table: { disable: true } }, | ||
}, | ||
render: ({ ...args }) => <EuiSideNav {...args} />, | ||
}; | ||
|
||
const StatefulSideNav = (props: Partial<EuiSideNavProps>) => { | ||
const [isSideNavOpenOnMobile, setIsSideNavOpenOnMobile] = useState( | ||
props.isOpenOnMobile | ||
); | ||
const [selectedItemName, setSelectedItem] = useState('Time stuff'); | ||
|
||
const toggleOpenOnMobile = () => { | ||
setIsSideNavOpenOnMobile(!isSideNavOpenOnMobile); | ||
}; | ||
|
||
const sideNav = [ | ||
{ | ||
name: 'Kibana', | ||
id: 'kibana', | ||
onClick: undefined, | ||
icon: <EuiIcon type="logoKibana" />, | ||
items: [ | ||
{ | ||
name: 'Has nested children', | ||
id: 'normal_children', | ||
isSelected: selectedItemName === 'Has nested children', | ||
onClick: () => setSelectedItem('Has nested children'), | ||
items: [ | ||
{ | ||
name: 'Child 1', | ||
id: 'child_1', | ||
isSelected: selectedItemName === 'Child 1', | ||
onClick: () => setSelectedItem('Child 1'), | ||
items: [ | ||
{ | ||
name: 'Child 2', | ||
id: 'child_2', | ||
isSelected: selectedItemName === 'Child 2', | ||
onClick: () => setSelectedItem('Child 2'), | ||
items: [], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Has forceOpen: true', | ||
id: 'force_open', | ||
isSelected: selectedItemName === 'Has forceOpen: true', | ||
onClick: () => setSelectedItem('Has forceOpen: true'), | ||
forceOpen: true, | ||
items: [ | ||
{ | ||
name: 'Child 3', | ||
id: 'child_3', | ||
isSelected: selectedItemName === 'Child 3', | ||
onClick: () => setSelectedItem('Child 3'), | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Children only without link', | ||
id: 'children_only', | ||
isSelected: selectedItemName === 'Children only without link', | ||
onClick: undefined, | ||
items: [ | ||
{ | ||
name: 'Child 4', | ||
id: 'child_4', | ||
isSelected: selectedItemName === 'Child 4', | ||
onClick: () => setSelectedItem('Child 4'), | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
return ( | ||
<EuiSideNav | ||
{...props} | ||
toggleOpenOnMobile={toggleOpenOnMobile} | ||
isOpenOnMobile={isSideNavOpenOnMobile} | ||
items={sideNav} | ||
css={{ width: '200px' }} // Required to view text truncation | ||
/> | ||
); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a super huge fan of the stateful side nav being the default playground story. I'd much rather have the following stories:
Non-stateful playground with items set via
args.items
, not viarender
, that allows developers to configure the item array/object via storybook controls. Using theitems
in the current stateful component as a static array would be fine, although I would not have statefulisSelected
behavior. You can have 1 item with a staticisSelected: true
property just to show the visual behavior of it, but it doesn't need to be controlled via state.Story with stateful
isOpenOnMobile
/toggleOpenOnMobile
. Note that the control fortoggleOpenOnMobile
should be set to{ control: false }
so that it can't be override in the control panel. I would only show those 2 props andmobileBreakpoints
andmobileTitle
on this story to keep the focus on mobile nav QA. I would also tweak the parameters of the storybook to start in small mobile view (https://storybook.js.org/docs/react/essentials/viewport)Story with custom
renderItem
callback