Skip to content

Machine-translated – improvements welcome. Suggest an improvement

Switch

Schalter (switch) is the building block for everything that is on or off – a function, a service, an automation (#1188). The same sliding switch everywhere, next to it the label without a verb (“WebDAV drive”, “Intake folder”) and the state as a word (“On”/“Off”), so it doesn't rely on colour alone.

What it is for

For something that runs or doesn't run. For a choice or option the checkbox stays (Field, HakenFeld).

Switch or checkbox?

What for Examples
Switch (Schalter) something is on or off WebDAV drive, intake folder, sending emails, automatic fetching of a mailbox, automatic backup, automations and workflow templates, expert mode
Checkbox (HakenFeld) a choice or option “Read only”, “also used for work”, “File original mail as .eml”, confirmations (“I have saved …”)

Matching patterns in the pattern catalogue: A7 · Switch or checkbox, C5 · Save via the save bar, C9 · A state is shown and changed in exactly one place.

How to use it

With How it works
Mouse A click on the switch or its label toggles it. On hover the track gets a soft ring.
Keyboard Tab reaches the switch; Space and Enter toggle.
Touch The switch is at least 44 px high, the label toggles it, too (On a phone).

A switch takes effect either when saving (part of a form) or at once (setze returns a promise): then it is busy until the server has answered, and an error appears below it.

What it means

Green and “On” means: it runs. Grey and “Off” means: it doesn't run. On is green because green is a state (principle 2); off is the input border colour (3:1). The label names what is on or off, never a verb (“Switch on”).

No duplicate state: where a switch is at the top, “Status” doesn't repeat the state (“State: switched on” is gone). Status only shows what the switch doesn't – since when, last use, errors.

States

State How it comes about How it looks
on an track green (--gruen), knob on the right, “On”
off an={false} track in --rand-eingabe, knob on the left, “Off”
mouse over – soft ring around the track (--akzent-zart)
keyboard focus – 2 px ring in --akzent around switch and label
busy setze returns a promise that is still running “working” mouse pointer, aria-busy; a second click has no effect
error the promise fails message in --schlecht below the switch
disabled gesperrt faded, cannot be toggled

“Switch” section of the pattern page: on, off, disabled, mouse over, focus, rule, try it

“Switch” section, dark

Accessibility

<button role="switch" aria-checked>: screen readers say “switch, WebDAV drive, on”. The word “On/Off” next to it is aria-hidden (the state is in aria-checked). The hint and an error are linked via aria-describedby, busy via aria-busy. On is green, off is the input border colour (3:1). On a phone the switch is at least 44 px high. Keyboard: Space and Enter toggle.

Code

import { Schalter } from '../bausteine';

// Part of a form: takes effect when saving.
<Schalter id="webdav-aktiv" an={e.aktiv} setze={(an) => setE({ ...e, aktiv: an })} beschriftung={t('werkbank.webdav.schalter')}
  hinweis={t('…ausgeschaltet-antwortet-mit-nicht')} />

// Takes effect at once: setze returns a promise – while it runs the switch is busy; an error appears below it.
<Schalter an={s.unscharf} beschriftung={t('suche.werkbank.unscharf.schalter')} setze={(an) => api.sucheSetze({ unscharf: an })} />
Property Meaning
an, setze state and switching; setze may return a promise
beschriftung what is on or off, without a verb; name for screen readers
hinweis explanation below (e.g. what “off” means), linked via aria-describedby
beschriftungVerbergen label for screen readers only – when it already stands next to it (head of an automation)
gesperrt, id, className disabled (faded); fixed id; additional class

Converted (#1188)

WebDAV drive (without “State” under Status), intake folder, sending emails, mailbox (automatic fetching), automatic backup and weekly test, shrink new PDFs automatically, “Show translation” for everyone, “Export my data” for all users, similar spellings (search), sign-in with a provider (OIDC), expert mode, automations (before: own switch in Automatisierungen.tsx) and workflow templates (before: button “Switch on/off”).

Files and tests

  • Building block: web/src/bausteine/schalter/Schalter.tsx, look: Schalter.module.css
  • Pattern page: web/src/bausteine/schalter/muster.tsx
  • Test: web/test/baustein-schalter.test.ts (building block and the converted places)