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
import { HkSwitch } from "@celestia-island/hikari";
use hikari_components::*; use hikari_components::prelude::*;
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.
import { HkSwitch } from "@celestia-island/hikari";
import type { CSSProperties } from "vue";
const column: CSSProperties = { display: "grid", gap: "12px", maxWidth: "320px" };
export default () => (
<div style={column}>
<HkSwitch modelValue={true} size="sm" label="Small" />
<HkSwitch modelValue={true} label="Primary, the default" />
<HkSwitch modelValue={true} size="lg" label="Large" />
<HkSwitch modelValue={true} color="secondary" label="Secondary" />
<HkSwitch modelValue={true} color="danger" label="Danger" />
<HkSwitch modelValue={true} color="warning" label="Warning" />
</div>
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
rsx! {
div { style: "display:grid;gap:12px;max-width:320px",
Switch {
checked: true,
size: SwitchSize::Sm,
label: Some("Small".to_string()),
}
Switch {
checked: true,
label: Some("Primary, the default".to_string()),
}
Switch {
checked: true,
size: SwitchSize::Lg,
label: Some("Large".to_string()),
}
Switch {
checked: true,
color: SwitchColor::Secondary,
label: Some("Secondary".to_string()),
}
Switch {
checked: true,
color: SwitchColor::Danger,
label: Some("Danger".to_string()),
}
Switch {
checked: true,
color: SwitchColor::Warning,
label: Some("Warning".to_string()),
}
}
}
import { HkSwitch } from "@celestia-island/hikari";
import type { CSSProperties } from "vue";
const column: CSSProperties = { display: "grid", gap: "12px", maxWidth: "320px" };
export default () => (
<div style={column}>
<HkSwitch modelValue={true} size="sm" label="Small" />
<HkSwitch modelValue={true} label="Primary, the default" />
<HkSwitch modelValue={true} size="lg" label="Large" />
<HkSwitch modelValue={true} color="secondary" label="Secondary" />
<HkSwitch modelValue={true} color="danger" label="Danger" />
<HkSwitch modelValue={true} color="warning" label="Warning" />
</div>
);rsx! {
div { style: "display:grid;gap:12px;max-width:320px",
Switch {
checked: true,
size: SwitchSize::Sm,
label: Some("Small".to_string()),
}
Switch {
checked: true,
label: Some("Primary, the default".to_string()),
}
Switch {
checked: true,
size: SwitchSize::Lg,
label: Some("Large".to_string()),
}
Switch {
checked: true,
color: SwitchColor::Secondary,
label: Some("Secondary".to_string()),
}
Switch {
checked: true,
color: SwitchColor::Danger,
label: Some("Danger".to_string()),
}
Switch {
checked: true,
color: SwitchColor::Warning,
label: Some("Warning".to_string()),
}
}
}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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { HkSwitch } from "@celestia-island/hikari";
import { ref } from "vue";
import type { CSSProperties } from "vue";
const column: CSSProperties = { display: "grid", gap: "12px", maxWidth: "320px" };
const wifi = ref(true);
export default () => (
<div style={column}>
<HkSwitch
modelValue={wifi.value}
onUpdate:modelValue={(value) => (wifi.value = value)}
checkedContent="ON"
uncheckedContent="OFF"
label="Wi-Fi"
/>
<span>The radio is {wifi.value ? "on" : "off"}.</span>
</div>
);
rsx! {
div { style: "display:grid;gap:12px;max-width:320px",
Switch {
checked: true,
checked_content: Some("ON".to_string()),
unchecked_content: Some("OFF".to_string()),
label: Some("Wi-Fi".to_string()),
}
Switch {
checked: false,
checked_content: Some("ON".to_string()),
unchecked_content: Some("OFF".to_string()),
label: Some("Wi-Fi".to_string()),
}
}
}
import { HkSwitch } from "@celestia-island/hikari";
import { ref } from "vue";
import type { CSSProperties } from "vue";
const column: CSSProperties = { display: "grid", gap: "12px", maxWidth: "320px" };
const wifi = ref(true);
export default () => (
<div style={column}>
<HkSwitch
modelValue={wifi.value}
onUpdate:modelValue={(value) => (wifi.value = value)}
checkedContent="ON"
uncheckedContent="OFF"
label="Wi-Fi"
/>
<span>The radio is {wifi.value ? "on" : "off"}.</span>
</div>
);rsx! {
div { style: "display:grid;gap:12px;max-width:320px",
Switch {
checked: true,
checked_content: Some("ON".to_string()),
unchecked_content: Some("OFF".to_string()),
label: Some("Wi-Fi".to_string()),
}
Switch {
checked: false,
checked_content: Some("ON".to_string()),
unchecked_content: Some("OFF".to_string()),
label: Some("Wi-Fi".to_string()),
}
}
}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.
import { HkSwitch } from "@celestia-island/hikari";
import type { CSSProperties } from "vue";
const column: CSSProperties = { display: "grid", gap: "12px", maxWidth: "320px" };
export default () => (
<div style={column}>
<HkSwitch modelValue={true} label="Send usage data" onUpdate:modelValue={() => undefined} />
<HkSwitch modelValue={false} disabled label="Locked by the administrator" />
<HkSwitch modelValue={true} ariaLabel="Beta features" />
<HkSwitch modelValue={false}>
<span>Compact <strong>rows</strong></span>
</HkSwitch>
</div>
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
rsx! {
div { style: "display:grid;gap:12px;max-width:320px",
Switch {
checked: true,
label: Some("Send usage data".to_string()),
on_change: Some(EventHandler::new(|_checked: bool| {})),
}
Switch {
checked: false,
disabled: true,
label: Some("Locked by the administrator".to_string()),
}
Switch {
checked: true,
aria_label: Some("Beta features".to_string()),
}
Switch {
checked: false,
children: rsx! {
span { "Compact " strong { "rows" } }
},
}
}
}
import { HkSwitch } from "@celestia-island/hikari";
import type { CSSProperties } from "vue";
const column: CSSProperties = { display: "grid", gap: "12px", maxWidth: "320px" };
export default () => (
<div style={column}>
<HkSwitch modelValue={true} label="Send usage data" onUpdate:modelValue={() => undefined} />
<HkSwitch modelValue={false} disabled label="Locked by the administrator" />
<HkSwitch modelValue={true} ariaLabel="Beta features" />
<HkSwitch modelValue={false}>
<span>Compact <strong>rows</strong></span>
</HkSwitch>
</div>
);rsx! {
div { style: "display:grid;gap:12px;max-width:320px",
Switch {
checked: true,
label: Some("Send usage data".to_string()),
on_change: Some(EventHandler::new(|_checked: bool| {})),
}
Switch {
checked: false,
disabled: true,
label: Some("Locked by the administrator".to_string()),
}
Switch {
checked: true,
aria_label: Some("Beta features".to_string()),
}
Switch {
checked: false,
children: rsx! {
span { "Compact " strong { "rows" } }
},
}
}
}
API
Vue
Props
Emits
Rust
Props — Switch
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.