Skip to content

Commit

Permalink
Merge pull request storybookjs#15369 from apapadopoulou/ap/sidebar-to…
Browse files Browse the repository at this point in the history
…ggle

UI: Display menu icon on the toolbar when the sidebar is collapsed
  • Loading branch information
shilman authored Jun 30, 2021
2 parents cc0550c + 730ff9f commit c74836d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/ui/src/components/preview/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as S from './utils/components';
import { PreviewProps } from './utils/types';
import { copyTool } from './tools/copy';
import { ejectTool } from './tools/eject';
import { menuTool } from './tools/menu';

export const getTools = (getFn: API['getElements']) => Object.values(getFn<Addon>(types.TOOL));

Expand Down Expand Up @@ -193,7 +194,11 @@ export function filterTools(
path: State['path'];
}
) {
const toolsLeft = [tabs.filter((p) => !p.hidden).length >= 1 && createTabsTool(tabs), ...tools];
const toolsLeft = [
menuTool,
tabs.filter((p) => !p.hidden).length >= 1 && createTabsTool(tabs),
...tools,
];
const toolsRight = [...toolsExtra];

const filter = (item: Partial<Addon>) =>
Expand Down
36 changes: 36 additions & 0 deletions lib/ui/src/components/preview/tools/menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { IconButton, Icons, Separator } from '@storybook/components';
import { Consumer, Combo } from '@storybook/api';
import { Addon } from '@storybook/addons';

const menuMapper = ({ api, state }: Combo) => ({
isVisible: state.layout.showNav,
toggle: api.toggleNav,
});

export const menuTool: Addon = {
title: 'menu',
id: 'menu',
match: ({ viewMode }) => viewMode === 'story',
render: () => (
<>
<Consumer filter={menuMapper}>
{({ isVisible, toggle }) =>
!isVisible && (
<>
<IconButton
aria-label="Show sidebar"
key="menu"
onClick={toggle as any}
title="Show sidebar"
>
<Icons icon="menu" />
</IconButton>
<Separator />
</>
)
}
</Consumer>
</>
),
};
8 changes: 6 additions & 2 deletions lib/ui/src/components/sidebar/Menu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Fragment, FunctionComponent } from 'react';

import { WithTooltip, TooltipLinkList, Icons } from '@storybook/components';
import { styled } from '@storybook/theming';
import { MenuItemIcon, SidebarMenu, MenuButton, SidebarMenuList } from './Menu';
import { MenuItemIcon, SidebarMenu, ToolbarMenu, MenuButton, SidebarMenuList } from './Menu';
import { useMenu } from '../../containers/menu';

export default {
Expand Down Expand Up @@ -31,6 +31,8 @@ export const Items = () => <TooltipLinkList links={fakemenu} />;

export const Real = () => <SidebarMenu menu={fakemenu} isHighlighted />;

export const Toolbar = () => <ToolbarMenu menu={fakemenu} />;

const DoubleThemeRenderingHack = styled.div({
'#root > [data-side="left"] > &': {
textAlign: 'right',
Expand All @@ -40,7 +42,7 @@ const DoubleThemeRenderingHack = styled.div({
const ExpandedMenu: FunctionComponent<{ menu: any }> = ({ menu }) => (
<DoubleThemeRenderingHack>
<WithTooltip
placement="top"
placement="bottom"
trigger="click"
closeOnClick
startOpen
Expand All @@ -65,6 +67,7 @@ export const Expanded = () => {
false,
false,
false,
false,
false
);
return <ExpandedMenu menu={menu} />;
Expand All @@ -82,6 +85,7 @@ export const ExpandedWithoutReleaseNotes = () => {
false,
false,
false,
false,
false
);
return <ExpandedMenu menu={menu} />;
Expand Down
19 changes: 18 additions & 1 deletion lib/ui/src/components/sidebar/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { FunctionComponent, useMemo, ComponentProps } from 'react';

import { styled } from '@storybook/theming';
import { WithTooltip, TooltipLinkList, Button, Icons } from '@storybook/components';
import { WithTooltip, TooltipLinkList, Button, Icons, IconButton } from '@storybook/components';

export type MenuList = ComponentProps<typeof TooltipLinkList>['links'];

Expand Down Expand Up @@ -113,3 +113,20 @@ export const SidebarMenu: FunctionComponent<{
</WithTooltip>
);
};

export const ToolbarMenu: FunctionComponent<{
menu: MenuList;
}> = ({ menu }) => {
return (
<WithTooltip
placement="bottom"
trigger="click"
closeOnClick
tooltip={({ onHide }) => <SidebarMenuList onHide={onHide} menu={menu} />}
>
<IconButton title="Shortcuts" aria-label="Shortcuts">
<Icons icon="menu" />
</IconButton>
</WithTooltip>
);
};

0 comments on commit c74836d

Please sign in to comment.