Switch

Switch

HkSwitch is the immediate on/off setting: a track with a thumb that slides to the right when the value is on. It is built on a native <input type="checkbox"> hidden inside a <label>, so a click on the caption, the track or the thumb all toggle the same value, and the change is announced as a checkbox would be. Reach for it when flipping the value takes effect at once — a preference, a live filter — rather than being collected for a submit.

  • For a value collected into a form and confirmed later, use HkCheckbox.
  • For a choice that excludes its siblings, use HkRadio.
  • For a numeric level picked along a range, use HkSlider.

Import

tsx
1
import { HkSwitch } from "@celestia-island/hikari";
rust
1
2
use hikari_components::*;          // Switch, SwitchSize, SwitchColor
use hikari_components::prelude::*; // rsx!, VNode

The Rust form is a plain function, Switch(SwitchProps { … }). modelValue becomes checked, and update:modelValue becomes on_change, which carries the flipped value rather than the one the host already holds.

Sizes and colours

Three track sizes — sm, md (the default), lg — and four checked inks: primary (the default), secondary, danger and warning. color only paints the track while the switch is on; the off track is the shared neutral fill, which is why a danger switch turned off looks like any other.

Text inside the track

checkedContent and uncheckedContent put a short word inside the track, next to the thumb: only one of them is ever rendered, and only on its own side of the state. They are plain strings rather than slots, so a track can say ON / OFF without extra markup.

The Vue example is live: press a switch to see its track word swap.

The radio is on.

Disabled, caption and accessible name

disabled dims the switch and silences both toggle paths. The caption is label, and the default slot wins over it for rich content. ariaLabel names the control explicitly: it wins over label, and an empty string never invents a name — so a switch driven by an external caption keeps working without the component fabricating an empty label for it.

The Rust example below attaches a handler; the fence runs without a browser, so its output is the rendered snapshot.

API

Vue

Props

NameTypeDefaultRequired
modelValueBooleanfalse—
disabledBooleanfalse—
size<code>String as PropType<"sm" | "md" | "lg"></code>"md"—
color<code>String as PropType<"primary" | "secondary" | "danger" | "warning"></code>"primary"—
labelStringundefined—
ariaLabelStringundefined—
checkedContentStringundefined—
uncheckedContentStringundefined—

Emits

EventPayload
update:modelValueboolean

Rust

Props — Switch

PropTypeDefaultDescription
checkedboolfalseControlled checked state (Vue: modelValue, default false).
disabledboolfalse—
sizeSwitchSizeSwitchSize::Md—
colorSwitchColorSwitchColor::PrimaryChecked ink (Vue: color).
labelOption<String>NoneLabel text; the default slot (children) wins over it when present.
aria_labelOption<String>NoneExplicit accessible name for caption-less callers (Vue ariaLabel); wins over label, empty strings never fabricate a name.
checked_contentOption<String>NoneText shown inside the track while checked (Vue: checkedContent).
unchecked_contentOption<String>NoneText shown inside the track while unchecked (Vue: uncheckedContent).
on_changeOption<EventHandler<bool>>NoneEmitted with the toggled value; a no-op while disabled.
childrenElement(empty)Rich label content (Vue: the default slot).
classString""Vue attribute fallthrough onto the root <label>.
styleString""—

Accessibility

  • The root is a <label> around the native <input type="checkbox">, so the caption is the switch's name and a click anywhere on the row toggles it.
  • The native input owns the role and the checked state; the component adds no aria-checked, which would announce the state twice.
  • ariaLabel gives a caption-less switch a deterministic name: the explicit label wins, label is the fallback, and nothing is invented when both are empty. Without it, a switch whose track shows ON / OFF would take that word as part of its name.
  • The input is visually erased, so the focus ring is drawn on the track from the input's :focus-visible; the switch stays reachable and visible in the Tab order.
  • disabled uses the native attribute, so the control leaves the keyboard order and is announced as unavailable.

Related components

  • HkCheckbox — the value collected into a form, with a third indeterminate state.
  • HkRadio — the single-choice group.
  • HkSlider — a number picked along a range.
  • HkFormField — the caption and message shell for a control that needs a label line.