When to use it#
- Put one KvirnProvider at the root of your app. It is optional, but it is where announcements come from, so every real service needs it.
- Without it, components use English, left-to-right text, UTC as the time zone, weeks that start on Monday and a plain
<a>. The theme follows the device. - Add a nested provider for a part of the page in another language. A nested provider inherits everything it doesn’t set.
- Don’t nest providers to change the theme or to add live regions: there is one theme and one pair of live regions per page. To change one component’s text, pass
messagesto that component instead. - For what runs on the server and what runs in the browser, see Rendering: server and client. For texts and number and date formats, see Locales and strings.
Example#
- Language (lang)
sv-SE- Text direction (dir)
ltr- Time zone
Europe/Stockholm- First day of the week
- måndag
- Decision date
- 14 oktober 2026
- Fee
- 1 250,50 kr
import { sv } from '@kvirn-ui/i18n/sv'
import {
DefinitionList,
KvirnProvider,
useDateSettings,
useFormat,
useLocale,
} from '@kvirn-ui/react'
import { useKvirnProviderTexts } from './texts.ts'
export function DefaultKvirnProvider() {
return (
<KvirnProvider locale="sv-SE" messages={sv} timeZone="Europe/Stockholm" weekStart={1}>
<Settings />
</KvirnProvider>
)
}
// A Monday, so the weekday name below is the name of the first day of the week.
const aMonday = Date.UTC(2026, 9, 12)
const aDay = 24 * 60 * 60 * 1000
function Settings() {
const { texts } = useKvirnProviderTexts()
const { locale, dir, localeProps } = useLocale()
const { timeZone, weekStart } = useDateSettings()
const format = useFormat()
// The values are written in Swedish, so each one carries the lang of the provider.
return (
<DefinitionList.Root>
<DefinitionList.Row>
<DefinitionList.Term>{texts.language}</DefinitionList.Term>
<DefinitionList.Description>
<code>{locale}</code>
</DefinitionList.Description>
</DefinitionList.Row>
<DefinitionList.Row>
<DefinitionList.Term>{texts.direction}</DefinitionList.Term>
<DefinitionList.Description>
<code>{dir}</code>
</DefinitionList.Description>
</DefinitionList.Row>
<DefinitionList.Row>
<DefinitionList.Term>{texts.timeZone}</DefinitionList.Term>
<DefinitionList.Description>
<code>{timeZone}</code>
</DefinitionList.Description>
</DefinitionList.Row>
<DefinitionList.Row>
<DefinitionList.Term>{texts.firstWeekday}</DefinitionList.Term>
<DefinitionList.Description>
<span {...localeProps}>
{format.date(aMonday + (weekStart - 1) * aDay, { weekday: 'long', timeZone: 'UTC' })}
</span>
</DefinitionList.Description>
</DefinitionList.Row>
<DefinitionList.Row>
<DefinitionList.Term>{texts.decisionDate}</DefinitionList.Term>
<DefinitionList.Description>
<span {...localeProps}>{format.date('2026-10-14', { dateStyle: 'long' })}</span>
</DefinitionList.Description>
</DefinitionList.Row>
<DefinitionList.Row>
<DefinitionList.Term>{texts.fee}</DefinitionList.Term>
<DefinitionList.Description>
<span {...localeProps}>
{format.number(1250.5, { style: 'currency', currency: 'SEK' })}
</span>
</DefinitionList.Description>
</DefinitionList.Row>
</DefinitionList.Root>
)
}Set up in Next.js (App Router)#
layout.tsx is a Server Component: it runs only on the server. KvirnProvider is a client component: it runs in the browser. Props that cross from one to the other must be plain data. Some can be passed straight from the layout, and some need a small client file.
Passed from the layout#
Strings, numbers and plain objects:
locale,dir,country,timeZone,weekStart- the
themedefaults,toast,iconDefaults childrenand the script’snonce
Needs a client file#
messages: catalogs hold functions, which can’t cross.linkComponent: importnext/linkin a client module.icons: a registry made bydefineIcons, which comes from the client entry. It can hold components, so it lives in a client module.theme.storage: an adapter is an object of functions.env: a window and a document.
The client wrapper#
Put those props in one 'use client' wrapper, and let the layout pass the rest.
'use client'
import { KvirnProvider } from '@kvirn-ui/react'
import type { KvirnProviderProps } from '@kvirn-ui/react'
import { sv } from '@kvirn-ui/i18n/sv'
import NextLink from 'next/link'
import { icons } from './icons' // a 'use client' module that calls defineIcons for the extra icon names you want
export function AppKvirnProvider(
props: Omit<KvirnProviderProps, 'messages' | 'linkComponent' | 'icons' | 'env'>,
) {
return <KvirnProvider {...props} messages={sv} linkComponent={NextLink} icons={icons} />
}The theme object#
One object for the script and the provider, so the defaults are written once.
export const theme = {
defaultColorScheme: 'system',
defaultContrast: 'system',
defaultMotion: 'system',
} as constThe layout#
The layout imports KvirnThemeScript and getLocaleProps from @kvirn-ui/react/server, the entry that is safe in a Server Component.
import { KvirnThemeScript, getLocaleProps } from '@kvirn-ui/react/server'
import { headers } from 'next/headers'
import type { ReactNode } from 'react'
import { AppKvirnProvider } from './kvirn-provider'
import { theme } from './theme'
const locale = 'sv-SE'
export default async function RootLayout({ children }: { children: ReactNode }) {
const nonce = (await headers()).get('x-nonce') ?? undefined
const { lang, dir } = getLocaleProps(locale)
return (
<html lang={lang} dir={dir} suppressHydrationWarning>
<head>
<KvirnThemeScript nonce={nonce} theme={theme} />
</head>
<body>
<AppKvirnProvider locale={locale} timeZone="Europe/Stockholm" weekStart={1} theme={theme}>
{children}
</AppKvirnProvider>
</body>
</html>
)
}getLocaleProps(locale)giveslanganddirfor<html>, the pair the provider works out for the same locale. The provider renders no element of its own, so it can’t set the language of the page (3.1.1 Language of Page, level A).- Set
timeZoneandweekStart, so the server and the browser show the same date. suppressHydrationWarningon<html>is on purpose:KvirnThemeScriptaddsdata-kv-color-scheme,data-kv-contrastanddata-kv-motionbefore React hydrates. It only affects<html>’s own attributes.
Set up with TanStack Router#
TanStack Router’s Link renders an <a> and takes a ref, so it works as linkComponent.
import { KvirnProvider } from '@kvirn-ui/react'
import { fi } from '@kvirn-ui/i18n/fi'
import { Link, RouterProvider } from '@tanstack/react-router'
import { router } from './router' // the result of createRouter
export function App() {
return (
<KvirnProvider locale="fi-FI" messages={fi} timeZone="Europe/Helsinki" linkComponent={Link}>
<RouterProvider router={router} />
</KvirnProvider>
)
}In a client-only app the provider applies the theme when it mounts, so the default theme can flash for a moment. If you render on the server, render KvirnThemeScript in the document’s <head>. Other frameworks are covered in Rendering.
Typed registration#
Register your link component and your icons once to get their props typed, including typed routes. Without it, linkComponent only accepts 'a'. The icons key is the type of the registry you pass to icons, so a misspelt Icon name is a type error. Both keys are optional.
import type NextLink from 'next/link'
import type { icons } from './icons'
declare module '@kvirn-ui/react' {
interface Register {
linkComponent: typeof NextLink
icons: typeof icons
}
}Use cases#
A page part in another language
A page in one language has a notice in another, such as a Finnish notice (“Your application has been received. We will process it within three weeks.”) on an English page. Wrap the part in a nested provider with its locale and its catalog, and spread localeProps on the element that starts the part. A screen reader then reads the part in the right language.
This sentence is English. The part below is Finnish.
Hakemuksesi on vastaanotettu. Käsittelemme sen kolmen viikon kuluessa.
import { fi } from '@kvirn-ui/i18n/fi'
import { KvirnProvider, Link, useLocale } from '@kvirn-ui/react'
import type { ReactNode } from 'react'
import { useKvirnProviderTexts } from './texts.ts'
export function AnotherLanguage() {
const { texts } = useKvirnProviderTexts()
return (
<div>
<p>{texts.pageIntro}</p>
<KvirnProvider locale="fi-FI" messages={fi}>
<FinnishPart>
<p>{texts.finnishNotice}</p>
<p>
<Link.Root href="https://www.suomi.fi/" target="_blank">
{texts.finnishLink} <Link.NewTabNotice />
</Link.Root>
</p>
</FinnishPart>
</KvirnProvider>
</div>
)
}
// The provider renders no element, so the element that starts the part carries the language.
function FinnishPart({ children }: { children: ReactNode }) {
const { localeProps } = useLocale()
return <section {...localeProps}>{children}</section>
}Props in this example:
Warning: Don't forget
Pass the catalog with the locale. Without it, the part’s texts stay in the parent’s language while lang says otherwise, and a development warning names both (3.1.2 Language of Parts, level AA).
Let visitors choose the theme
Some visitors need dark mode or high contrast to read. The provider follows the device until a visitor chooses, keeps the choice in this browser, and useTheme reads and changes it. Build the controls from labelled radio groups, one group per setting. When isForcedColors is true, say that the device’s own colours are in use. This example changes the theme of the whole site, like Display settings at the top of the page. KvirnUI also has a ready-made DisplaySettings component for the same job.
import { Field, RadioGroup, useTheme } from '@kvirn-ui/react'
import { useKvirnProviderTexts } from './texts.ts'
const colorSchemes = ['system', 'light', 'dark'] as const
const contrasts = ['system', 'standard', 'more'] as const
export function ThemeSwitcher() {
const { texts } = useKvirnProviderTexts()
const theme = useTheme()
return (
<div>
<RadioGroup.Root
name="color-scheme"
value={theme.colorScheme}
onValueChange={(value) => {
const choice = colorSchemes.find((option) => option === value)
if (choice !== undefined) {
theme.selectColorScheme(choice)
}
}}
>
<RadioGroup.Legend>{texts.colorScheme}</RadioGroup.Legend>
{colorSchemes.map((option) => (
<RadioGroup.Option key={option}>
<RadioGroup.Radio value={option} />
<Field.Label>{texts[option]}</Field.Label>
</RadioGroup.Option>
))}
</RadioGroup.Root>
<RadioGroup.Root
name="contrast"
value={theme.contrast}
onValueChange={(value) => {
const choice = contrasts.find((option) => option === value)
if (choice !== undefined) {
theme.selectContrast(choice)
}
}}
>
<RadioGroup.Legend>{texts.contrast}</RadioGroup.Legend>
{contrasts.map((option) => (
<RadioGroup.Option key={option}>
<RadioGroup.Radio value={option} />
<Field.Label>{texts[option]}</Field.Label>
</RadioGroup.Option>
))}
</RadioGroup.Root>
{theme.isForcedColors ? <p>{texts.forcedColors}</p> : null}
</div>
)
}Props in this example:
colorSchemeon useTheme (result)contraston useTheme (result)isForcedColorson useTheme (result)selectColorSchemeon useTheme (result)selectContraston useTheme (result)
Warning: Don't forget
A theme change moves no focus and announces nothing, so the checked option is the feedback. Don’t put the result in a live region (3.2.2 On Input, level A).
Accessibility#
What it does for you#
- APG pattern: none. The provider renders no visible element and has no interaction of its own. The outermost one renders two visually hidden, empty live regions (see the Announcer page). A theme switcher built on
useThemeis consumer markup made of native radio groups (APG Radio Group, native<input type="radio">). - Deviations: none
- Native elements used: none rendered by the provider. A theme switcher built on
useThemeuses<fieldset>,<legend>,<label>and<input type="radio">.KvirnThemeScriptrenders one<script>.
The provider's contract is about what it guarantees for other components:
langanddirare available asuseLocale().localePropswherever the locale changes (3.1.2 Language of Parts, AA). The provider renders no element that can carrylang, so the consumer spreadslocalePropson the element that starts a section in another language, and sets<html lang dir>for the page language itself (3.1.1 Language of Page, A).- Every visible and announced component string comes from
messages, resolved as instancemessages, then the nearest provider and its ancestors, then built-inen. An empty or whitespace-only override falls through to the next level, so an accessible name is never empty (4.1.2 Name, Role, Value, A). - The attributes are resolved from
prefers-color-schemeandprefers-contrastuntil the user chooses (without JavaScript the page is the light, standard theme), and the theme never overridesforced-colors(1.4.3 Contrast (Minimum), AA; 1.4.11 Non-text Contrast, AA). 1.4.6 Contrast (Enhanced), AAA, is not counted toward AA. - A theme change moves no focus and announces nothing. The switcher's own control state is the feedback.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| KvirnProvider | none (context only) | none | Writes data-kv-color-scheme, data-kv-contrast and data-kv-motion (resolved values) on <html>. Not exposed to AT |
localeProps | consumer's element | lang, dir attributes | lang is the BCP 47 locale. dir comes from the locale, or the dir prop |
| KvirnThemeScript | <script nonce> | none | Blocking, before first paint. Not exposed to AT |
| Theme switcher | <fieldset> → group, <legend> → name | none added | One group per axis: colour scheme, contrast |
| Theme switcher option | <input type="radio"> in a <label> | native checked | Name from the wrapping <label>. 24 × 24 CSS px (2.5.8 Target Size (Minimum), AA) |
Toast region (toast) | <section class="kv-toast-region" popover="manual"> → region | aria-label from toast.regionLabel, lang, dir | Rendered by the host provider (the first that mounted; the next takes over if it unmounts) after its children, only while a toast exists, in the top layer. Never a live region. Described on the Toast page. The whole page shares one list and one region: only the host's toast, locale and messages are used, and a later or nested provider's toast is ignored with a warning in development |
| Resolved theme text | <p> | none. Deliberately not a live region | Shows the resolved values. Not <output> or role=status, so a change is not announced |
It announces nothing.
What you need to do#
- Set
<html lang dir>to the root provider's locale and direction (3.1.1 Language of Page, A), withgetLocaleProps(locale)from@kvirn-ui/react/server. The provider can't: it renders no element of its own to carry them, and a server layout can't spreadlocalePropson<html>. - Spread
useLocale().localePropson the element that starts a section in another language, solangmatches the strings (3.1.2 Language of Parts, AA). Nested providers inherit everything they don't set. - Pass a catalog (
messages={sv}) for a non-English locale. Otherwise English strings render under a non-Englishlang, with a dev warning per key. - A nested provider that changes the language must pass that language's catalog too (
<KvirnProvider locale="fi-FI" messages={fi}>). Otherwise its strings stay in the parent's language whilelangsays otherwise. A dev warning names both locales. - When overriding a message that forms an accessible name, keep it consistent with the visible label (2.5.3 Label in Name, A).
- Build the theme switcher from native controls with visible labels (for example radio groups in a
<fieldset>with a<legend>), and don't make the resolved-theme text a live region. - Render
KvirnThemeScriptwith the response's CSPnoncein the server-rendered<head>, and putsuppressHydrationWarningon<html>. Pass it the provider'sthemeobject, from the server entry (@kvirn-ui/react/server) in a Server Component. It runs before first paint, and until it does the theme's ownprefers-*rules answer for a visitor's OS setting (1.4.3 Contrast (Minimum), AA; 1.4.11 Non-text Contrast, AA). - Set
timeZoneexplicitly, the same on the server and in the browser. Without one, instants are shown in UTC and the first one formatted warns in development.
Warning: Don't forget
Set lang and dir on <html> with getLocaleProps. The provider can’t: it renders no element to carry them (3.1.1 Language of Page, level A).
Visual and modes#
- Focus indicator: the provider renders nothing visible. A theme switcher relies on the browser's native radio focus ring. The default theme restyles rings in
@kvirn-ui/theme. - Target size: the switcher's radios are 24 × 24 CSS px, in labels at least 28px tall (2.5.8 Target Size (Minimum), AA).
- forced-colors behaviour: the provider keeps writing the resolved attributes.
@kvirn-ui/thememust let the OS palette win and never overrideforced-colors: active. - reduced-motion behaviour: no motion. A theme change is instant, and
data-kv-motion="reduce"stops theme transitions and animations likeprefers-reduced-motion(2.3.3 Animation from Interactions, AAA, is not counted toward AA). - Reflow: No horizontal scrolling at 320 CSS px (1.4.10 Reflow, AA): the reflow sweep checks every story at 320 CSS pixels for horizontal overflow, including those with Swedish text, a theme switcher, a nested locale and right-to-left.
Focus management#
- Initial focus: not moved. The provider never touches focus.
- Trap: no.
- Restore to: not applicable.
- A theme change keeps focus on the radio that caused it.
- Never obscured by: the provider renders nothing visible and nothing focusable.
WCAG success criteria#
- 1.4.3 Contrast (Minimum) (AA) and 1.4.11 Non-text Contrast (AA): OS preferences are honoured by default, and high contrast is user-selectable. The contrast values themselves are
@kvirn-ui/theme's, checked automatically in every theme. 1.4.6 Contrast (Enhanced), AAA, is not counted toward AA. - 1.4.10 Reflow (AA): the reflow sweep checks every story at 320 CSS pixels for horizontal overflow.
- 3.1.1 Language of Page (A): your duty (
<html lang dir>, withgetLocaleProps); see Set-up on this page. - 3.1.2 Language of Parts (AA):
localeProps, and a warning in development for a nested language change without a catalog. - 3.2.2 On Input (A): selecting a theme changes presentation only. No context change, no focus move.
- 4.1.2 Name, Role, Value (A): empty overrides fall through, so names are never empty.
- 4.1.3 Status Messages (AA): not applicable. A theme change is deliberately not announced.
Keyboard#
KvirnProvider has no keys of its own. What you put in it keeps its own keys.
API reference#
import { KvirnProvider, KvirnThemeScript, useLocale, useDateSettings, useFormat, useTheme } from '@kvirn-ui/react'KvirnProvider
Renders no element of its own. The outermost provider adds two visually hidden live regions after its children, and a toast region while a toast shows. Nested providers inherit everything they don’t set.
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | – | What the provider wraps. |
locale | string (BCP 47) | 'en', or the parent's | Sets how numbers and dates are written, the lang value, and which texts are used. A tag such as sv-SE, fi-FI or nn-NO. A bare en is read as US English (10/14/26), so pass en-GB for 14/10/2026. |
dir | 'ltr' | 'rtl' | From locale, or the parent's | Overrides the text direction that the locale gives. |
country | 'SE' | 'FI' | 'NO' | The parent's, else from locale | The country for masks that differ by country. The first of these that applies is used: this prop, the region of the locale (sv-FI is FI), then its language (sv is SE, fi is FI, and nb, nn and no are NO). For en there is none: a country mask then takes only digits and warns once in development. |
messages | PartialMessages | Inherited, then English | A catalog, such as sv from @kvirn-ui/i18n/sv, or a partial override of the texts. A non-English locale needs its catalog, or English texts show under a non-English lang. |
timeZone | string (IANA) | The parent's, or UTC | The zone dates and times are shown in, such as Europe/Stockholm. Set it, the same on the server and in the browser, so both show the same date. Without one, times are shown in UTC and the first one warns in development. For a calendar date use the YYYY-MM-DD string: a date-only instant can show the neighbouring day. |
weekStart | 1 to 7 (ISO weekday, 1 is Monday) | The parent's, else the locale's when it names a region (en-US is 7), else 1 | The first day of the week in Calendar and DatePicker. The first of these that applies is used: the Calendar’s own value, this prop, the locale’s, then Monday. Week numbers are ISO 8601 and only show with a Monday start. Set it for server rendering. |
readAloud | ReadAloudConfig | the parent's | The read-aloud voice service: { url, name, store, timeout, voices }. One engine is built and shared by every ReadAloud below, so the audio element unlocked by the first press is reused. The voice follows the content’s lang, falling back to locale. |
linkComponent | RegisteredLinkComponent | 'a', or the parent's | Your router’s link component. Every KvirnUI component that renders a link uses it. It must forward its ref and render an <a>. Register it for typed link props. |
icons | IconRegistry | The parent's | The names for <Icon name>, from defineIcons: icons from @kvirn-ui/icons, or from Lucide or others. KvirnUI draws no icon by name until you register it, and a registered name also replaces that icon in KvirnUI’s own components. Merged by name. Register only the ones you need: the whole set from @kvirn-ui/icons/all adds every icon to your bundle. |
iconDefaults | IconDefaults | The parent's | Default size for every Icon below. Merged by field. |
theme | ThemeOptions | – | Defaults and storage for the theme. Read by the outermost provider only. A theme on a nested provider is ignored with a development warning. |
toast | KvirnToastOptions | { limit: 10, autoDismiss: false } | Options for the toast region the provider renders. The first outermost provider that mounts owns them: a nested or later provider’s toast is ignored with a development warning. |
env | Env | The page, after hydration | The window and document to use, for an iframe or a test. |
| Class or attribute | Values | Meaning |
|---|---|---|
data-kv-color-scheme | light or dark | Set on <html>: the colour scheme in use, after system is replaced by the device’s setting. |
data-kv-contrast | standard or more | Set on <html>: the contrast in use. |
data-kv-motion | full or reduce | Set on <html>: the motion in use. reduce stops every transition and animation in the theme. |
ThemeOptions
Renders nothing: it is the object you pass as the theme prop. The default theme has three independent settings: colour scheme, contrast and motion. Each follows the device until the user chooses. Under forced colours the device’s own palette always wins. A chosen preference is a small piece of data in the browser, so describe it in your privacy notice. The CSS side is on Theming.
| Prop | Type | Default | Description |
|---|---|---|---|
defaultColorScheme | 'light' | 'dark' | 'system' | 'system' | The colour scheme until the user chooses. |
defaultContrast | 'standard' | 'more' | 'system' | 'system' | The contrast until the user chooses. |
defaultMotion | 'full' | 'reduce' | 'system' | 'system' | The motion until the user chooses. reduce stops every transition and animation in the theme. |
storage | 'local' | 'none' | { read, write } | 'local' | Where a chosen preference is kept. local stores JSON under the localStorage key kvirn-ui:theme, and other tabs follow it. none keeps it in memory. A { read, write } adapter keeps it somewhere else: the store checks what read returns, and a read or write that throws is caught. If storage is off or full, the choice still applies for the visit. |
KvirnToastOptions
Renders nothing: it is the object you pass as the toast prop. All providers on a page share one toast list and one toast region. See Toast for when to use a toast.
| Prop | Type | Default | Description |
|---|---|---|---|
limit | number | 10 | How many toasts show at once. Past it, the oldest toast that can time out is removed for the new one. If there is none, or the user is on a toast, the new toast is ignored with a development warning. It is not queued. |
autoDismiss | false | number | false | false: no toast times out. A number is how long a toast stays, in milliseconds. A toast with an action never times out, and neither does one that is still busy. There is no minimum, so a short time can remove a toast before it is read (2.2.1 Timing Adjustable): tie it to a setting the user can change. 0, a negative number and NaN count as false and warn in development. |
KvirnThemeScript
Renders a small <script> for the document’s <head>. It reads the stored preference and the device settings, and sets the data-kv-* attributes on <html> before the first paint, so there is no flash of the wrong theme. Render it only in the server-rendered document, from @kvirn-ui/react/server in a Server Component. It reads localStorage only, so don’t use it with a custom storage adapter. See No flash of the wrong theme.
| Prop | Type | Default | Description |
|---|---|---|---|
nonce | string | – | The CSP nonce of the response. |
theme | { defaultColorScheme, defaultContrast, defaultMotion } | 'system' for each | The same object as the provider’s theme, so the defaults are written once. Only the three defaults are read: storage is ignored. |
useLocale
Takes no options. It reads the nearest provider: en and ltr without one.
Result
| Prop | Type | Default | Description |
|---|---|---|---|
locale | string | – | The BCP 47 locale. |
dir | 'ltr' | 'rtl' | – | The text direction. |
country | 'SE' | 'FI' | 'NO' | undefined | – | The country for masks. Undefined when neither the prop nor the locale says. |
localeProps | { lang, dir } | – | Spread on the element that starts a part in another language (3.1.2 Language of Parts). |
useDateSettings
Takes no options. It reads the nearest provider.
Result
| Prop | Type | Default | Description |
|---|---|---|---|
timeZone | string | – | The provider’s time zone, or UTC. It is never the browser’s zone. |
weekStart | 1 to 7 | – | The first day of the week, worked out as for the weekStart prop. |
useFormat
Takes no options. It formats the way the nearest provider’s locale writes, and times in its time zone. It is en and UTC without a provider. The object stays the same until the locale or the time zone changes, so it is safe in a dependency list.
Result
| Prop | Type | Default | Description |
|---|---|---|---|
number | (value, options?) => string | – | Intl.NumberFormat options. A currency, a percentage or a plain number. |
date | (value, options?) => string | – | Intl.DateTimeFormat options. A Date or a number is a moment, shown in the time zone. A YYYY-MM-DD string is a calendar date, shown on its own day. |
list | (items, options?) => string | – | Joins items the way the language does: “a, b and c”. |
plural | (count, forms) => string | – | Picks the form for the count by the locale’s plural rules. |
useTheme
Takes no options. It reads and changes the page’s one theme preference. It works without a provider, with the defaults and localStorage.
Result
| Prop | Type | Default | Description |
|---|---|---|---|
colorScheme | 'light' | 'dark' | 'system' | – | The user’s choice, or your default until they choose. |
contrast | 'standard' | 'more' | 'system' | – | The user’s choice, or your default until they choose. |
motion | 'full' | 'reduce' | 'system' | – | The user’s choice, or your default until they choose. reduce is less motion. |
resolvedColorScheme | 'light' | 'dark' | – | The scheme in use, after system is replaced by the device’s setting. |
resolvedContrast | 'standard' | 'more' | – | The contrast in use. |
resolvedMotion | 'full' | 'reduce' | – | The motion in use. |
isForcedColors | boolean | – | The device forces its own colours (for example Windows contrast themes). They go before any choice, so a switcher should say so. |
selectColorScheme | (colorScheme) => void | – | Saves the choice. A choice equal to your default is not kept. |
selectContrast | (contrast) => void | – | Saves the choice. A choice equal to your default is not kept. |
selectMotion | (motion) => void | – | Saves the choice. A choice equal to your default is not kept. |
The server entry
@kvirn-ui/react/server has no 'use client' and is safe in a Server Component.
import { KvirnThemeScript, getLocaleProps, getMessages, createMessageFormat } from '@kvirn-ui/react/server'Known issues#
- Keyboard tests run in Chromium only. The keyboard rows run in Vitest browser mode on Chromium, not in WebKit. Test in Safari with your own assistive technology.
- Storing the theme preference. The library assumes that storing an explicitly chosen theme preference in
localStoragefalls under the ePrivacy Art. 5(3) "strictly necessary" exemption. Confirm that with your own legal advice.
Testing#
The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.
| AT + browser + OS | Agent check | Human check | Result | Notes |
|---|---|---|---|---|
| Automated (re-runnable tests) | ||||
| axe, WCAG 2.2 A and AA, every story, 4 themes | pass | n/a | pass | kvirn-provider.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | kvirn-provider.test.tsx › uses en, ltr, UTC and the en messages; expectNoA11yViolations |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 5 rows, each names a test in use-theme.test.tsx (use-theme.test.tsx › Tab moves focus to the checked radio of the colour-scheme group) |
| Focus order and management | pass | n/a | pass | use-theme.test.tsx › arrow keys change the radio group; focus stays and nothing is announced |
| Display modes (forced colours, 320px reflow, text spacing, reduced motion) | pass | n/a | pass | the four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI |
| Core (required for beta) | ||||
| NVDA + Chrome + Windows | pending | pending | pending | |
| VoiceOver + Safari + iOS | pending | pending | pending | |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | pending | pending | pending | |
| TalkBack + Chrome + Android | pending | pending | pending | |
| VoiceOver + Safari + macOS | pending | pending | pending |
What to check#
- Set the operating system to dark, then to high contrast, and reload: the page follows without a choice.
- In a theme switcher, Tab to the colour-scheme group and use the arrow keys: the checked option is read, focus stays, and nothing else is announced.
- With Windows contrast themes on, the page uses the system colours and the switcher says so.
- A part in another language (a nested provider with
localeProps) is read in that language's voice. - A theme chosen in one tab is followed by a second tab.