Skip to content

Commit

Permalink
Updated the theme switcher to use toggle button when there are only t…
Browse files Browse the repository at this point in the history
…wo themes
  • Loading branch information
ivoilic committed Jan 21, 2024
1 parent 424ad8f commit faed670
Showing 1 changed file with 50 additions and 28 deletions.
78 changes: 50 additions & 28 deletions code/addons/themes/src/theme-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {

const IconButtonLabel = styled.div(({ theme }) => ({
fontSize: theme.typography.size.s2 - 1,
marginLeft: 10,
}));

const hasMultipleThemes = (themesList: ThemeAddonState['themesList']) => themesList.length > 1;
const hasTwoThemes = (themesList: ThemeAddonState['themesList']) => themesList.length === 2;

export const ThemeSwitcher = () => {
const { themeOverride } = useParameter<ThemeParameters>(
Expand Down Expand Up @@ -52,33 +52,55 @@ export const ThemeSwitcher = () => {
return themeName && <>{`${themeName} theme`}</>;
}, [themeOverride, themeDefault, selected]);

return hasMultipleThemes(themesList) ? (
<Fragment>
<WithTooltip
placement="top"
trigger="click"
closeOnOutsideClick
tooltip={({ onHide }) => {
return (
<TooltipLinkList
links={themesList.map((theme) => ({
id: theme,
title: theme,
active: selected === theme,
onClick: () => {
updateGlobals({ theme });
onHide();
},
}))}
/>
);
if (hasTwoThemes(themesList)) {
const currentTheme = selected || themeDefault;
const alternateTheme = themesList.find((theme) => theme !== currentTheme);
return (
<IconButton
key={THEME_SWITCHER_ID}
active={!themeOverride}
title="Theme"
onClick={() => {
updateGlobals({ theme: alternateTheme });
}}
>
<IconButton key={THEME_SWITCHER_ID} active={!themeOverride} title="Theme">
<PaintBrushIcon />
{label && <IconButtonLabel>{label}</IconButtonLabel>}
</IconButton>
</WithTooltip>
</Fragment>
) : null;
<PaintBrushIcon />
{label && <IconButtonLabel>{label}</IconButtonLabel>}
</IconButton>
);
}

if (hasMultipleThemes(themesList)) {
return (
<Fragment>
<WithTooltip
placement="top"
trigger="click"
closeOnOutsideClick
tooltip={({ onHide }) => {
return (
<TooltipLinkList
links={themesList.map((theme) => ({
id: theme,
title: theme,
active: selected === theme,
onClick: () => {
updateGlobals({ theme });
onHide();
},
}))}
/>
);
}}
>
<IconButton key={THEME_SWITCHER_ID} active={!themeOverride} title="Theme">
<PaintBrushIcon />
{label && <IconButtonLabel>{label}</IconButtonLabel>}
</IconButton>
</WithTooltip>
</Fragment>
);
}

return null;
};

0 comments on commit faed670

Please sign in to comment.