diff --git a/frontend/src/components/layout/ThemeToggle.test.tsx b/frontend/src/components/layout/ThemeToggle.test.tsx new file mode 100644 index 0000000..1272d1d --- /dev/null +++ b/frontend/src/components/layout/ThemeToggle.test.tsx @@ -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(); + + 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(); + + 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(); + + 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(); + + 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(); + + expect(screen.getByTestId('theme-toggle')).toHaveAccessibleName('Theme: automatic (now dark)'); + }); +}); diff --git a/frontend/src/components/layout/ThemeToggle.tsx b/frontend/src/components/layout/ThemeToggle.tsx index 60041f8..7f8416f 100644 --- a/frontend/src/components/layout/ThemeToggle.tsx +++ b/frontend/src/components/layout/ThemeToggle.tsx @@ -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 ( - + + + + + + {t('theme.title')} + + {MODES.map(({ mode: option, icon: OptionIcon }) => ( + setMode(option)} + aria-current={mode === option ? 'true' : undefined} + data-testid={`theme-option-${option}`} + > + + {t(`theme.mode.${option}`)} + + + ))} + + ); } diff --git a/frontend/src/components/ui/checkbox.test.tsx b/frontend/src/components/ui/checkbox.test.tsx new file mode 100644 index 0000000..f934d29 --- /dev/null +++ b/frontend/src/components/ui/checkbox.test.tsx @@ -0,0 +1,105 @@ +import { describe, expect, it } from 'vitest'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { useForm } from 'react-hook-form'; +import { Checkbox, CheckboxField } from './checkbox'; +import { Radio, RadioField, RadioGroup } from './radio'; + +describe('Checkbox', () => { + it('is a real checkbox: Space toggles it and the label is clickable', async () => { + render(); + const box = screen.getByRole('checkbox', { name: 'Uitgelicht' }); + + box.focus(); + await userEvent.keyboard(' '); + expect(box).toBeChecked(); + + await userEvent.click(screen.getByText('Uitgelicht')); + expect(box).not.toBeChecked(); + }); + + it('shows the in-between state and does not change its value', () => { + render(); + const box = screen.getByRole('checkbox', { name: 'alles' }) as HTMLInputElement; + + expect(box.indeterminate).toBe(true); + expect(box).not.toBeChecked(); + }); + + it('can be disabled and marked invalid', async () => { + render(); + const box = screen.getByRole('checkbox', { name: 'x' }); + + expect(box).toBeDisabled(); + expect(box).toHaveAttribute('aria-invalid', 'true'); + await userEvent.click(box); + expect(box).not.toBeChecked(); + }); + + it('draws with the colours of the theme (accent when checked, a visible border, a focus ring)', () => { + render(); + const cls = screen.getByRole('checkbox').className; + + expect(cls).toContain('checked:bg-primary'); + expect(cls).toContain('border-muted-foreground'); + expect(cls).toContain('focus-visible:ring-ring'); + expect(cls).toContain('forced-colors:checked:bg-[Highlight]'); + }); + + it('works with react-hook-form register, like the native input it replaces', async () => { + function Form({ onSubmit }: { onSubmit: (v: { on: boolean }) => void }) { + const { register, handleSubmit } = useForm<{ on: boolean }>({ defaultValues: { on: false } }); + return ( +
+ + + + ); + } + let saved: { on: boolean } | null = null; + render( +
{ + saved = v; + }} + />, + ); + + await userEvent.click(screen.getByLabelText('Aan')); + await userEvent.click(screen.getByText('go')); + + await screen.findByText('go'); + expect(saved).toEqual({ on: true }); + }); +}); + +describe('Radio', () => { + it('is a real radio group: the arrow keys move and select, with a name for the group', async () => { + render( + + + + , + ); + expect(screen.getByRole('radiogroup', { name: 'Weergave' })).toBeInTheDocument(); + const first = screen.getByRole('radio', { name: 'Enkel' }); + const second = screen.getByRole('radio', { name: 'Diavoorstelling' }); + + first.focus(); + await userEvent.keyboard('{ArrowDown}'); + + expect(second).toBeChecked(); + expect(first).not.toBeChecked(); + expect(second).toHaveFocus(); + }); + + it('can be disabled and marked invalid, and uses theme colours', () => { + render(); + const radio = screen.getByRole('radio'); + + expect(radio).toBeDisabled(); + expect(radio).toHaveAttribute('aria-invalid', 'true'); + expect(radio.className).toContain('checked:border-primary'); + expect(radio.className).toContain('forced-colors:checked:border-[Highlight]'); + }); +}); diff --git a/frontend/src/components/ui/checkbox.tsx b/frontend/src/components/ui/checkbox.tsx new file mode 100644 index 0000000..bd5e648 --- /dev/null +++ b/frontend/src/components/ui/checkbox.tsx @@ -0,0 +1,77 @@ +import * as React from 'react'; +import { Check, Minus } from 'lucide-react'; +import { cn } from '@/lib/utils'; + +export interface CheckboxProps extends Omit, 'type' | 'size'> { + /** The "some of them" state of a parent checkbox. It is shown with a minus; the value of the box itself does not change. */ + indeterminate?: boolean; + /** The field has an error: a red border (and aria-invalid for screen readers). */ + invalid?: boolean; +} + +/** + * A real checkbox (a native input: Space toggles it, it works with a label, with react-hook-form's register and in forms), + * drawn in the colours of the theme: a clear border, the accent colour with a check mark when checked, a minus for the + * in-between state, hover, a focus ring, disabled and error states. The check mark is an icon over the input in the + * "primary foreground" colour; in forced-colors (high contrast) mode the system colours take over. + */ +export const Checkbox = React.forwardRef( + ({ className, indeterminate = false, invalid = false, ...props }, forwardedRef) => { + const inner = React.useRef(null); + const setRef = (element: HTMLInputElement | null) => { + inner.current = element; + if (typeof forwardedRef === 'function') forwardedRef(element); + else if (forwardedRef) forwardedRef.current = element; + }; + React.useEffect(() => { + if (inner.current) inner.current.indeterminate = indeterminate; + }, [indeterminate]); + + return ( + + + + ); + }, +); +Checkbox.displayName = 'Checkbox'; + +interface CheckboxFieldProps extends CheckboxProps { + label: React.ReactNode; +} + +/** A checkbox with its label: the whole line is clickable and at least 24 px high. */ +export const CheckboxField = React.forwardRef(({ label, id, disabled, ...props }, ref) => { + const generated = React.useId(); + const inputId = id ?? generated; + return ( + + ); +}); +CheckboxField.displayName = 'CheckboxField'; diff --git a/frontend/src/components/ui/radio.tsx b/frontend/src/components/ui/radio.tsx new file mode 100644 index 0000000..c7e7019 --- /dev/null +++ b/frontend/src/components/ui/radio.tsx @@ -0,0 +1,61 @@ +import * as React from 'react'; +import { cn } from '@/lib/utils'; + +export interface RadioProps extends Omit, 'type' | 'size'> { + invalid?: boolean; +} + +/** + * A real radio button (a native input: with the same name the arrow keys move and select inside the group, Space selects), + * drawn in the colours of the theme: a clear round border, the accent colour with a dot when selected, hover, a focus + * ring, disabled and error states, and system colours in forced-colors (high contrast) mode. + */ +export const Radio = React.forwardRef(({ className, invalid = false, ...props }, ref) => ( + + + +)); +Radio.displayName = 'Radio'; + +interface RadioFieldProps extends RadioProps { + label: React.ReactNode; +} + +/** A radio button with its label: the whole line is clickable and at least 24 px high. */ +export const RadioField = React.forwardRef(({ label, id, disabled, ...props }, ref) => { + const generated = React.useId(); + const inputId = id ?? generated; + return ( + + ); +}); +RadioField.displayName = 'RadioField'; + +/** The group around related radio buttons: role radiogroup with a name, so a screen reader announces what is chosen. */ +export function RadioGroup({ label, className, children, ...props }: { label: string } & Omit, 'aria-label'>) { + return ( +
+ {children} +
+ ); +} diff --git a/frontend/src/features/offerings/components/OfferingBannerDialog.tsx b/frontend/src/features/offerings/components/OfferingBannerDialog.tsx index 3d76414..80248f2 100644 --- a/frontend/src/features/offerings/components/OfferingBannerDialog.tsx +++ b/frontend/src/features/offerings/components/OfferingBannerDialog.tsx @@ -5,6 +5,7 @@ import { useTranslation } from 'react-i18next'; import { toast } from 'sonner'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; +import { CheckboxField } from '@/components/ui/checkbox'; import { Label } from '@/components/ui/label'; import { FieldError } from '@/components/ui/FieldError'; import { FormErrorBanner } from '@/components/ui/FormErrorBanner'; @@ -211,16 +212,12 @@ function OfferingBannerForm({

{t('offerings.banners.scheduleHint')}

-
- - -
+