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

Toolbars: Add dynamicTitle option #17789

Merged
merged 3 commits into from Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions addons/toolbars/src/components/ToolbarMenuList.tsx
Expand Up @@ -3,7 +3,7 @@ import { useGlobals } from '@storybook/api';
import { WithTooltip, TooltipLinkList } from '@storybook/components';
import { ToolbarMenuButton } from './ToolbarMenuButton';
import { withKeyboardCycle, WithKeyboardCycleProps } from '../hoc/withKeyboardCycle';
import { getSelectedIcon } from '../utils/get-selected-icon';
import { getSelectedIcon, getSelectedTitle } from '../utils/get-selected';
import { ToolbarMenuProps } from '../types';
import { ToolbarMenuListItem } from './ToolbarMenuListItem';

Expand All @@ -22,7 +22,7 @@ export const ToolbarMenuList: FC<ToolbarMenuListProps> = withKeyboardCycle(
id,
name,
description,
toolbar: { icon: _icon, items, title: _title, showName, preventDynamicIcon },
toolbar: { icon: _icon, items, title: _title, showName, preventDynamicIcon, dynamicTitle },
}) => {
const [globals, updateGlobals] = useGlobals();

Expand All @@ -40,6 +40,10 @@ export const ToolbarMenuList: FC<ToolbarMenuListProps> = withKeyboardCycle(
title = name;
}

if (dynamicTitle) {
title = getSelectedTitle({ currentValue, items }) || title;
}

const handleItemClick = useCallback(
(value: string) => {
updateGlobals({ [id]: value });
Expand Down
2 changes: 2 additions & 0 deletions addons/toolbars/src/types.ts
Expand Up @@ -33,6 +33,8 @@ export interface NormalizedToolbarConfig {
shortcuts?: ToolbarShortcuts;
/** @deprecated "name" no longer dual purposes as title - use "title" if a title is wanted */
showName?: boolean;
/** Change title based on selected value */
dynamicTitle?: boolean;
}

export type NormalizedToolbarArgType = ArgType & {
Expand Down
13 changes: 0 additions & 13 deletions addons/toolbars/src/utils/get-selected-icon.ts

This file was deleted.

21 changes: 21 additions & 0 deletions addons/toolbars/src/utils/get-selected.ts
@@ -0,0 +1,21 @@
import { ToolbarItem } from '../types';

interface GetSelectedItemProps {
currentValue: string | null;
items: ToolbarItem[];
}

export const getSelectedItem = ({ currentValue, items }: GetSelectedItemProps) => {
const selectedItem = currentValue != null && items.find((item) => item.value === currentValue);
return selectedItem;
};

export const getSelectedIcon = ({ currentValue, items }: GetSelectedItemProps) => {
const selectedItem = getSelectedItem({ currentValue, items });
return selectedItem?.icon;
};

export const getSelectedTitle = ({ currentValue, items }: GetSelectedItemProps) => {
const selectedItem = getSelectedItem({ currentValue, items });
return selectedItem?.title;
};
Expand Up @@ -12,6 +12,8 @@ export const globalTypes = {
items: ['light', 'dark'],
// Property that specifies if the name of the item will be displayed
showName: true,
// Change title based on selected value
dynamicTitle: true,
},
},
};
Expand Down
16 changes: 16 additions & 0 deletions examples/cra-ts-essentials/.storybook/preview.tsx
Expand Up @@ -26,6 +26,7 @@ export const globalTypes = {
description: 'Internationalization locale',
defaultValue: 'en',
toolbar: {
dynamicTitle: true,
icon: 'globe',
items: [
{ value: 'en', right: '🇺🇸', title: 'English' },
Expand All @@ -35,4 +36,19 @@ export const globalTypes = {
],
},
},
theme: {
name: 'Theme',
description: 'Global theme for components',
toolbar: {
dynamicTitle: true,
icon: 'circlehollow',
title: 'Theme',
items: [
{ value: 'light', icon: 'circlehollow', title: 'Light' },
{ value: 'dark', icon: 'circle', title: 'Dark' },
{ value: 'side-by-side', icon: 'sidebar', title: 'Side by side' },
{ value: 'stacked', icon: 'bottombar', title: 'Stacked' },
],
},
},
};