From faed670d3ae9d49aa6257b50a901651231a815fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivo=20Ilic=CC=81?= Date: Sun, 21 Jan 2024 15:49:45 -0500 Subject: [PATCH] Updated the theme switcher to use toggle button when there are only two themes --- code/addons/themes/src/theme-switcher.tsx | 78 +++++++++++++++-------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/code/addons/themes/src/theme-switcher.tsx b/code/addons/themes/src/theme-switcher.tsx index 7e366fe8359e..ef27f0d25769 100644 --- a/code/addons/themes/src/theme-switcher.tsx +++ b/code/addons/themes/src/theme-switcher.tsx @@ -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( @@ -52,33 +52,55 @@ export const ThemeSwitcher = () => { return themeName && <>{`${themeName} theme`}; }, [themeOverride, themeDefault, selected]); - return hasMultipleThemes(themesList) ? ( - - { - return ( - ({ - 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 ( + { + updateGlobals({ theme: alternateTheme }); }} > - - - {label && {label}} - - - - ) : null; + + {label && {label}} + + ); + } + + if (hasMultipleThemes(themesList)) { + return ( + + { + return ( + ({ + id: theme, + title: theme, + active: selected === theme, + onClick: () => { + updateGlobals({ theme }); + onHide(); + }, + }))} + /> + ); + }} + > + + + {label && {label}} + + + + ); + } + + return null; };