Skip to main content

Switch

A Switch toggles between two mutually exclusive states (“on” / “off”) and applies the change immediately.

Code example
import { Switch } from '@yleisradio/yds-components-react';

<Switch id="darkmode" name="darkmode" label="Enable dark mode" defaultChecked />

Why to use​

Switch communicates an instant, persistent state change. Use it when the user expects something to happen immediately after toggling (e.g., enable/disable a feature or visibility), and when the two states are clear opposites.

When to use​

Use a Switch when the user is toggling a binary setting and expects the change to take effect immediately.

Do
  • Use when the choice is strictly binary (on / off, enabled / disabled).
  • Use when the change should apply immediately without a save or submit action.
  • Use when the control should remain visible to reflect the current state.
  • Use for preferences, feature toggles, and visibility settings.
Don’t
  • Don’t use for selecting one option among many — use RadioGroup.
  • Don’t use for selecting multiple independent items — use Checkbox.
  • Don’t use when changes are applied only after form submission.
  • Don’t use for non-persistent or momentary actions.

Content Guidelines​

Switch labels must clearly describe what is enabled when the switch is on. Users should understand the outcome of toggling the switch without guessing.

Do
  • Write labels as states or features (e.g., “Tumma tila”, “Sähköposti-ilmoitukset”).
  • Ensure the meaning of the on state is clear.
  • Keep labels short, concrete, and easy to scan.
  • Use helper text if the effect is complex or has consequences.
  • Use consistent wording across similar switches.
Don’t
  • Don’t use vague labels (e.g., “Käytössä”, “Vaihtoehto”, “Asetus”).
  • Don’t phrase labels as actions (e.g., “Ota tumma tila käyttöön”).
  • Don’t use long explanatory sentences as labels.
  • Don’t rely on color alone to communicate the state.

Key Props​

Use the following props to customize the Switch component to your needs.

label​

Descriptive text for the setting—phrase its meaning in the ON state.

TypeExampleDescription
stringVisible label describing the effect when enabled.
Code example
<Switch id="metrics" name="metrics" label="Enable usage analytics" defaultChecked />

defaultChecked​

Uncontrolled initial state.

TypeExampleDescription
booleanSets starting state to ON without manual state management.
Code example
<Switch id="auto-sync" name="auto-sync" label="Auto sync" defaultChecked />

checked​

Controlled state—must pair with onChange.

TypeExampleDescription
booleanExplicitly sets state; component is read-only without onChange.
Code example
const [enabled, setEnabled] = useState(false);
<Switch
id="beta"
name="beta"
label="Enable beta features"
checked={enabled}
onChange={(e) => setEnabled(e.currentTarget.checked)}
/>

isDisabled​

Non-interactive, dimmed styling.

TypeExampleDescription
booleanTemporarily prevents user interaction.
Code example
<Switch id="sync" name="sync" label="Auto sync" isDisabled />

Behavior​

  • Toggling the switch applies the change immediately and persistently; no submit or confirmation is required.
  • The switch always reflects the current state and should never appear “unset”.
  • Disabled switches remain visible but are non-interactive.
  • Disabled switches remain visible but are non-interactive.
  • Supports both uncontrolled (defaultChecked) and controlled (checked + onChange) usage.
  • Related content that appears or disappears based on the state should be placed close to the switch.
  • In complex layouts, the label may be separated or wrapped as needed without changing behavior.

Accessibility​

Implementation examples​

Accessibility naming without label​

You may use aria-label / aria-labelledby for programmatic naming.

Code example
<Switch id="updates2" name="updates2" label="" aria-label="Enable updates" />

API Reference​

Props​

The Switch component accepts all standard HTML <input> attributes in addition to the following props:

PropTypeRequiredDefaultDescription
isDisabledbooleanNofalse
namestringYes—
labelstringYes—
labelOptionsFormElementLabelDSPropsNo{}

Type Definitions​

export interface SwitchDSProps {
isDisabled?: boolean;
name: string;
label: string;
labelOptions?: FormElementLabelDSProps;
}

export type SwitchProps = InputHTMLAttributes<HTMLInputElement> & SwitchDSProps;
  • Radio: Single required choice among options.
  • Checkbox: Independent multi-select options.
  • TextInput: For text entry when changes saved on submit.