Merge pull request #17789 from cocco3/toolbar-dynamic-title

Toolbars: Add dynamicTitle option
This commit is contained in:
Michael Shilman 2022-04-28 10:30:45 +08:00 committed by GitHub
commit 1a6ea867d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 15 deletions

View File

@ -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';
@ -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();
@ -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 });

View File

@ -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 & {

View File

@ -1,13 +0,0 @@
import { ToolbarItem } from '../types';
interface GetSelectedIconProps {
currentValue: string | null;
items: ToolbarItem[];
}
export const getSelectedIcon = ({ currentValue, items }: GetSelectedIconProps) => {
const selectedItem = currentValue != null && items.find((item) => item.value === currentValue);
const selectedIcon = selectedItem && selectedItem.icon;
return selectedIcon;
};

View File

@ -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;
};

View File

@ -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,
},
},
};

View File

@ -26,6 +26,7 @@ export const globalTypes = {
description: 'Internationalization locale',
defaultValue: 'en',
toolbar: {
dynamicTitle: true,
icon: 'globe',
items: [
{ value: 'en', right: '🇺🇸', title: 'English' },
@ -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' },
],
},
},
};