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

[Storybook] Create Stories for EuiSideNav #7174

Merged
merged 9 commits into from
Sep 22, 2023
5 changes: 5 additions & 0 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ appendIconComponentCache(iconCache);
import { EuiProvider } from '../src/components/provider';
import { writingModeStyles } from './writing_mode.styles';

import { MINIMAL_VIEWPORTS } from '@storybook/addon-viewport';

// Import light theme for components still using Sass styling
// TODO: Remove this import and the `yarn compile-scss &&` command
// once all EUI components are converted to Emotion
Expand Down Expand Up @@ -93,6 +95,9 @@ const preview: Preview = {
date: /Date$/,
},
},
viewport: {
viewports: MINIMAL_VIEWPORTS,
},
},
// Due to CommonProps, these props appear on almost every Story, but generally
// aren't super useful to test - let's disable them by default and (if needed)
Expand Down
255 changes: 255 additions & 0 deletions src/components/side_nav/side_nav.stories.tsx
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} />,
Copy link
Contributor

@cee-chen cee-chen Sep 13, 2023

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:

  1. Non-stateful playground with items set via args.items, not via render, that allows developers to configure the item array/object via storybook controls. Using the items in the current stateful component as a static array would be fine, although I would not have stateful isSelected behavior. You can have 1 item with a static isSelected: true property just to show the visual behavior of it, but it doesn't need to be controlled via state.

  2. Story with stateful isOpenOnMobile / toggleOpenOnMobile. Note that the control for toggleOpenOnMobile should be set to { control: false } so that it can't be override in the control panel. I would only show those 2 props and mobileBreakpoints and mobileTitle 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)

  3. Story with custom renderItem callback

};

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
/>
);
};