import { useTheme } from 'next-themes'; import { FC } from 'react'; import { useIntl } from 'react-intl'; import Moon from '../../atoms/icons/moon'; import Sun from '../../atoms/icons/sun'; import RadioGroup, { type RadioGroupCallback, type RadioGroupCallbackProps, type RadioGroupOption, type RadioGroupProps, } from './radio-group'; export type ThemeToggleProps = Pick< RadioGroupProps, 'bodyClassName' | 'groupClassName' | 'legendClassName' | 'legendPosition' >; /** * ThemeToggle component * * Render a Toggle component to set theme. */ const ThemeToggle: FC = (props) => { const intl = useIntl(); const { resolvedTheme, setTheme } = useTheme(); const isDarkTheme = resolvedTheme === 'dark'; /** * Update the theme. * * @param {string} theme - A theme name. */ const updateTheme = (theme: string) => { setTheme(theme === 'light' ? 'light' : 'dark'); }; /** * Handle change events. * * @param {RadioGroupCallbackProps} props - An object with choices. */ const handleChange: RadioGroupCallback = ({ choices, updateChoice, }: RadioGroupCallbackProps) => { if (choices.new === choices.prev) { const newTheme = choices.new === 'light' ? 'dark' : 'light'; updateChoice(newTheme); updateTheme(newTheme); } else { updateTheme(choices.new); } }; const themeLabel = intl.formatMessage({ defaultMessage: 'Theme:', description: 'ThemeToggle: theme label', id: 'suXOBu', }); const lightThemeLabel = intl.formatMessage({ defaultMessage: 'Light theme', description: 'ThemeToggle: light theme label', id: 'Ygea7s', }); const darkThemeLabel = intl.formatMessage({ defaultMessage: 'Dark theme', description: 'ThemeToggle: dark theme label', id: '2QwvtS', }); const options: RadioGroupOption[] = [ { id: 'theme-light', label: ( <> {lightThemeLabel} ), name: 'theme', value: 'light', }, { id: 'theme-dark', label: ( <> {darkThemeLabel} ), name: 'theme', value: 'dark', }, ]; return ( ); }; export default ThemeToggle;