import { LanguageSwitcher } from "@senvenli/react/action";
import { useState } from "react";
const LOCALES = ["en", "de", "fr", "es", "ja"] as const;
export default function Example() {
// In an app, `onSelect` changes the route or saves the preference.
const [locale, setLocale] = useState<(typeof LOCALES)[number]>("en");
return <LanguageSwitcher locales={LOCALES} value={locale} onChange={setLocale} />;
}import { LanguageSwitcher, LanguageLabel } from "@senvenli/react/action";Examples
With a mark
A renderMark function puts a code chip (or a flag) before each name.import { Badge } from "@senvenli/react/display";
import { LanguageSwitcher } from "@senvenli/react/action";
import { useState } from "react";
const LOCALES = ["en", "de", "fr", "es"] as const;
export default function Example() {
const [locale, setLocale] = useState<(typeof LOCALES)[number]>("en");
return <LanguageSwitcher locales={LOCALES} value={locale} onChange={setLocale} renderMark={(code) => <Badge>{code.toUpperCase()}</Badge>} />;
}Compact and collapsed
The small size for dense bars, and the mark alone for a collapsed rail.import { Badge } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { LanguageSwitcher } from "@senvenli/react/action";
import { useState } from "react";
const LOCALES = ["en", "de", "fr"] as const;
export default function Example() {
const [locale, setLocale] = useState<(typeof LOCALES)[number]>("en");
const mark = (code: string) => <Badge>{code.toUpperCase()}</Badge>;
return (
<Stack direction="horizontal" space="space-200">
<LanguageSwitcher size="small" locales={LOCALES} value={locale} onChange={setLocale} />
<LanguageSwitcher size="small" isCollapsed label="Interface language" locales={LOCALES} value={locale} onChange={setLocale} renderMark={mark} />
</Stack>
);
}Guidelines
When to use it
- The header or sidebar of a product offered in several languages.
Accessibility
Keyboard
| Keys | What happens |
|---|---|
| Arrow up / down | Moves between languages. |
| Enter | Chooses the focused language. |
Built in
- A listbox with the current language selected and checked.
- Names never fall back to the host's language.
- A mark is decorative; the name always tells options apart.
You provide
- The product's locales, and names for languages Intl names wrongly (registerLanguageNames).
API reference
LanguageSwitcher
| Prop | Type | Default | Description |
|---|---|---|---|
localesRequired | readonly Locale[] | – | Locales to offer, already narrowed to what this product enables. |
valueRequired | Locale | – | The active UI locale. |
onChangeRequired | (locale: Locale) => void | – | Called with the picked locale (routing, persistence). |
label | string | – | The trigger's accessible name and tooltip; "Language" (the DS string) by default. |
isLoading | boolean | false | Hold the disabled trigger while the product's locale list resolves. |
variant | "button" | "sidebar" | "button" |
|
isCollapsed | boolean | false | A collapsed rail: the mark alone (or the name, without one), kept for screen readers either way. |
renderMark | (locale: Locale) => ReactNode | – | A mark before each language's name: a flag, an |
size | "small" | "medium" | "medium" | Control height: |
aria-describedby | string | – | Ids of elements that describe the trigger ("Changes the language of the whole panel"). It lands on the trigger button, the focusable control, also while it is disabled for |
localesRequiredreadonly Locale[]Locales to offer, already narrowed to what this product enables.
valueRequiredLocaleThe active UI locale.
onChangeRequired(locale: Locale) => voidCalled with the picked locale (routing, persistence).
labelstringThe trigger's accessible name and tooltip; "Language" (the DS string) by default.
isLoadingbooleanDefaultfalseHold the disabled trigger while the product's locale list resolves.
variant"button" | "sidebar"Default"button"button(default): a trigger in a bar.sidebar: a full-width row whose list opens upward, for a panel sidebar.isCollapsedbooleanDefaultfalseA collapsed rail: the mark alone (or the name, without one), kept for screen readers either way.
renderMark(locale: Locale) => ReactNodeA mark before each language's name: a flag, an
IsoChip, an icon. None by default: a language is not a country, and the name alone tells the options apart.size"small" | "medium"Default"medium"Control height:
medium(default) matches the DS controls,smallis the compact step.aria-describedbystringIds of elements that describe the trigger ("Changes the language of the whole panel"). It lands on the trigger button, the focusable control, also while it is disabled for
isLoading; left out, nothing is added. While the trigger's tooltip is open it takes the place of the tooltip's id, which only repeatslabel.
LanguageLabel
| Prop | Type | Default | Description |
|---|---|---|---|
localeRequired | string | – | |
uiLocaleRequired | string | – | |
hideName | boolean | false | |
mark | React.ReactNode | – |
localeRequiredstringuiLocaleRequiredstringhideNamebooleanDefaultfalsemarkReact.ReactNode