Admin: themaknop met drie standen (licht, donker, automatisch), toont de huidige stand, volgt de systeemvoorkeur live
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import i18n from '@/i18n/config';
|
||||
import { ThemeToggle } from './ThemeToggle';
|
||||
|
||||
const media = (dark: boolean) =>
|
||||
vi.spyOn(window, 'matchMedia').mockImplementation(
|
||||
() => ({ matches: dark, addEventListener: () => undefined, removeEventListener: () => undefined }) as unknown as MediaQueryList,
|
||||
);
|
||||
|
||||
beforeEach(async () => {
|
||||
localStorage.clear();
|
||||
document.documentElement.className = '';
|
||||
vi.restoreAllMocks();
|
||||
await i18n.changeLanguage('nl');
|
||||
});
|
||||
|
||||
describe('ThemeToggle', () => {
|
||||
it.each([
|
||||
['light', 'Thema: licht'],
|
||||
['dark', 'Thema: donker'],
|
||||
])('shows the CURRENT %s theme in its name (not the action)', (mode, name) => {
|
||||
localStorage.setItem('cms-theme', mode);
|
||||
media(false);
|
||||
|
||||
render(<ThemeToggle />);
|
||||
|
||||
const button = screen.getByTestId('theme-toggle');
|
||||
expect(button).toHaveAccessibleName(name);
|
||||
expect(button).toHaveAttribute('data-mode', mode);
|
||||
expect(button).toHaveAttribute('title', name);
|
||||
});
|
||||
|
||||
it('says in words what automatic currently is', () => {
|
||||
media(true);
|
||||
|
||||
render(<ThemeToggle />);
|
||||
|
||||
expect(screen.getByTestId('theme-toggle')).toHaveAccessibleName('Thema: automatisch (nu donker)');
|
||||
expect(screen.getByTestId('theme-toggle')).toHaveAttribute('data-mode', 'auto');
|
||||
});
|
||||
|
||||
it('opens a menu with three options, the current one marked, and chooses with the keyboard', async () => {
|
||||
media(false);
|
||||
localStorage.setItem('cms-theme', 'light');
|
||||
render(<ThemeToggle />);
|
||||
|
||||
screen.getByTestId('theme-toggle').focus();
|
||||
await userEvent.keyboard('{Enter}');
|
||||
|
||||
expect(await screen.findByTestId('theme-option-light')).toHaveAttribute('aria-current', 'true');
|
||||
expect(screen.getByTestId('theme-option-dark')).not.toHaveAttribute('aria-current');
|
||||
expect(screen.getByTestId('theme-option-auto')).toHaveTextContent('Automatisch');
|
||||
|
||||
await userEvent.click(screen.getByTestId('theme-option-dark'));
|
||||
|
||||
expect(localStorage.getItem('cms-theme')).toBe('dark');
|
||||
expect(document.documentElement.classList.contains('dark')).toBe(true);
|
||||
expect(screen.getByTestId('theme-toggle')).toHaveAccessibleName('Thema: donker');
|
||||
});
|
||||
|
||||
it('works on the first click: automatic to dark flips what is shown at once', async () => {
|
||||
media(false);
|
||||
render(<ThemeToggle />);
|
||||
|
||||
await userEvent.click(screen.getByTestId('theme-toggle'));
|
||||
await userEvent.click(await screen.findByTestId('theme-option-dark'));
|
||||
|
||||
expect(document.documentElement.classList.contains('dark')).toBe(true);
|
||||
expect(document.documentElement.classList.contains('light')).toBe(false);
|
||||
});
|
||||
|
||||
it('has English texts too', async () => {
|
||||
await i18n.changeLanguage('en');
|
||||
media(true);
|
||||
|
||||
render(<ThemeToggle />);
|
||||
|
||||
expect(screen.getByTestId('theme-toggle')).toHaveAccessibleName('Theme: automatic (now dark)');
|
||||
});
|
||||
});
|
||||
@@ -1,20 +1,66 @@
|
||||
import { Sun, Moon } from 'lucide-react';
|
||||
import { Monitor, Moon, Sun } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useTheme } from '@/hooks/useTheme';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuCheck,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuLabel,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu';
|
||||
import { useTheme, type ThemeMode } from '@/hooks/useTheme';
|
||||
|
||||
const MODES: { mode: ThemeMode; icon: typeof Sun }[] = [
|
||||
{ mode: 'light', icon: Sun },
|
||||
{ mode: 'dark', icon: Moon },
|
||||
{ mode: 'auto', icon: Monitor },
|
||||
];
|
||||
|
||||
/**
|
||||
* Three states: light, dark, automatic (follow the system). The button shows the CURRENT choice (sun = light, moon =
|
||||
* dark, monitor = automatic), its name says it in words ("Theme: automatic (now dark)"), and a small menu with the three
|
||||
* options (the current one marked) changes it.
|
||||
*/
|
||||
export function ThemeToggle() {
|
||||
const { t } = useTranslation();
|
||||
const { isDark, toggleTheme } = useTheme();
|
||||
const { mode, theme, setMode } = useTheme();
|
||||
const Icon = MODES.find((m) => m.mode === mode)!.icon;
|
||||
const label =
|
||||
mode === 'auto'
|
||||
? t('theme.labelAuto', { now: t(`theme.mode.${theme}`).toLowerCase() })
|
||||
: t('theme.label', { mode: t(`theme.mode.${mode}`).toLowerCase() });
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={toggleTheme}
|
||||
aria-label={isDark ? t('theme.switchToLight') : t('theme.switchToDark')}
|
||||
data-testid="theme-toggle"
|
||||
className="flex items-center justify-center rounded-md p-2 text-muted-foreground hover:bg-accent hover:text-accent-foreground"
|
||||
>
|
||||
{isDark ? <Sun className="size-4" /> : <Moon className="size-4" />}
|
||||
</button>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
aria-label={label}
|
||||
title={label}
|
||||
data-testid="theme-toggle"
|
||||
data-mode={mode}
|
||||
className="flex items-center justify-center rounded-md p-2 text-muted-foreground hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
||||
>
|
||||
<Icon className="size-4" />
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start">
|
||||
<DropdownMenuLabel>{t('theme.title')}</DropdownMenuLabel>
|
||||
<DropdownMenuSeparator />
|
||||
{MODES.map(({ mode: option, icon: OptionIcon }) => (
|
||||
<DropdownMenuItem
|
||||
key={option}
|
||||
onSelect={() => setMode(option)}
|
||||
aria-current={mode === option ? 'true' : undefined}
|
||||
data-testid={`theme-option-${option}`}
|
||||
>
|
||||
<OptionIcon className="size-4" />
|
||||
{t(`theme.mode.${option}`)}
|
||||
<DropdownMenuCheck checked={mode === option} />
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user