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

UI: Display menu icon on the toolbar when the sidebar is collapsed #15369

Merged
merged 12 commits into from Jun 30, 2021
Merged
7 changes: 6 additions & 1 deletion lib/ui/src/components/preview/toolbar.tsx
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
@@ -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
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"
darleendenno marked this conversation as resolved.
Show resolved Hide resolved
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
@@ -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>
);
};