diff --git a/src/components/theme-changer/index.tsx b/src/components/theme-changer/index.tsx index 6eb1d0f..cc97578 100644 --- a/src/components/theme-changer/index.tsx +++ b/src/components/theme-changer/index.tsx @@ -1,47 +1,32 @@ -import { RiDice4Line } from 'react-icons/ri'; import { SanitizedThemeConfig } from '../../interfaces/sanitized-config'; import { LOCAL_STORAGE_KEY_NAME } from '../../constants'; import { skeleton } from '../../utils'; -import { MouseEvent } from 'react'; /** - * Renders a theme changer component. - * - * @param {Object} props - The props object. - * @param {string} props.theme - The current theme. - * @param {function} props.setTheme - A function to set the theme. - * @param {boolean} props.loading - Whether the component is in a loading state. - * @param {SanitizedThemeConfig} props.themeConfig - The theme configuration object. - * @return {JSX.Element} The rendered theme changer component. + * Renders a light/dark theme toggle. */ const ThemeChanger = ({ theme, setTheme, loading, - themeConfig, }: { theme: string; setTheme: (theme: string) => void; loading: boolean; themeConfig: SanitizedThemeConfig; }) => { - const changeTheme = ( - e: MouseEvent, - selectedTheme: string, - ) => { - e.preventDefault(); - - document.querySelector('html')?.setAttribute('data-theme', selectedTheme); + const isDark = theme === 'dark'; + const applyTheme = (next: 'light' | 'dark') => { + document.querySelector('html')?.setAttribute('data-theme', next); typeof window !== 'undefined' && - localStorage.setItem(LOCAL_STORAGE_KEY_NAME, selectedTheme); - - setTheme(selectedTheme); + localStorage.setItem(LOCAL_STORAGE_KEY_NAME, next); + setTheme(next); }; return (
-
+
{loading ? ( @@ -54,65 +39,30 @@ const ThemeChanger = ({ Theme )}
- + {loading ? skeleton({ widthCls: 'w-16', heightCls: 'h-5' }) - : theme === 'light' - ? 'Light' - : theme === 'dark' - ? 'Dark' - : theme === themeConfig.defaultTheme - ? 'Default' - : theme} + : isDark + ? 'Dark' + : 'Light'}
{loading ? ( skeleton({ widthCls: 'w-12', - heightCls: 'h-10', - className: 'mr-6', + heightCls: 'h-8', }) ) : ( -
-
- -
-
- -
-
+ applyTheme(isDark ? 'light' : 'dark')} + aria-label={ + isDark ? 'Switch to light theme' : 'Switch to dark theme' + } + /> )}