Skip to content

Commit

Permalink
Merge pull request #25682 from ivoilic/feature/addon-themes/two-theme…
Browse files Browse the repository at this point in the history
…-toggle

UI: Updated the theme switcher to use toggle button for two themes
  • Loading branch information
kasperpeulen authored Feb 23, 2024
2 parents 2a8d0b9 + 6e29f59 commit 6bfc7e9
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 6bfc7e9

Please sign in to comment.