Skip to content

Switch

Switch is a one-row boolean control with an optional caption, a bracketed track, a sliding knob, and optional localized On/Off words. Its two-way Signal<boolean> can be changed by the user or by application code, and either route repaints immediately.

Use it for an immediate setting whose two states are easy to understand. Space, Enter, click, and an optional caption accelerator all toggle the same bound value.

Usage

ts
import { Switch, signal } from '@jsvision/ui';

const wifi = signal(false);
const control = new Switch({
  value: wifi,
  label: '~W~i-Fi',
  onLabel: 'On',
  offLabel: 'Off',
});

Live example

Compare focused, on, off, custom-label, and disabled switches; toggle by hotkey, keyboard, and mouse.

The summary line reads the same signals the controls update. Reset proves external signal writes, while a locked switch demonstrates that disabled input remains inert.

Props

Switch accepts SwitchOptions:

PropTypeDefaultPurpose
valueSignal<boolean>Two-way on/off source of truth.
labelstringno captionText left of the track; ~X~ marks an Alt-hotkey.
onLabelstringlocalized OnText right of the track while true; '' hides it.
offLabelstringlocalized OffText right of the track while false; '' hides it.
disabledbooleanfalseMakes the control dim, inert, and unavailable to focus.
i18nI18nEnglish UI catalogTranslation source for default state words.

select(on) changes the bound value programmatically unless the switch is disabled. The public Signal<boolean> remains the simplest route for external reset or synchronized settings.

Size and Layout

measure() returns one row and enough width for the caption, a gap, the six-cell [ ] track, another gap, and the wider of the On/Off words. This stable maximum prevents the surrounding layout from shifting when the value changes.

An unlabelled switch with hidden state words measures 6×1. Explicitly narrowing below the measured width clips trailing content, so prefer auto sizing or reserve the measured width in a row layout.

States and disabled behavior

Off places the knob at the left end of a dim track; on places it at the right end of the button palette. Focus accents the brackets and the on-state foreground. Terminals without Unicode render an ASCII o instead of .

A disabled switch cannot receive focus and ignores click, Space, Enter, Alt-hotkeys, and select. Its track, brackets, and knob use the disabled cluster role, while caption and state text remain on the neutral static-text surface. The constructor captures disabled state; use a newly built control when availability itself must change reactively.

Keyboard & mouse

InputResult
Space / Enter while focusedToggle the bound boolean.
Click anywhere on the controlFocus and toggle on mouse-down.
Alt + caption hotkeyFocus and toggle from elsewhere in the active dialog.
External value.set(...)Repaint knob, track, and state word without synthetic input.

A switch without a marked caption has no Alt-hotkey. Disabled controls retain their accelerator claim for duplicate-detection purposes but do not handle it.

Best Practices

  • Phrase the caption as a setting, not an action: “Wi-Fi” with On/Off is clearer than “Change network.”
  • Use unique dialog accelerators and keep visible On/Off words when the knob position may be unfamiliar.
  • Reserve disabled state for genuinely unavailable settings and explain the reason nearby.
  • Use CheckGroup for several related booleans or RadioGroup when exactly one named alternative must win.

Theming

Switch intentionally reuses established roles:

Theme roleRegion
buttonEnabled on-state track.
buttonFocusedFocused brackets and focused on-state track.
staticTextCaption, state word, row fill, and off-state track.
labelShortcutMarked caption accelerator while enabled.
clusterDisabledDisabled track, brackets, and knob.

Test both boolean states when changing these roles. In particular, the off track must remain visible against staticText, and the focused brackets must be distinguishable without erasing the knob.

  • Check Group — several independent boolean settings.
  • Radio Group — one choice among named alternatives.
  • Slider — a bounded numeric setting.
  • Switch API — generated SwitchOptions, measurement, and selection API.