Checkbox
HkCheckbox is the independent on/off choice: a square box with a check glyph, an optional caption, and a third state — indeterminate — that draws a dash for "some of the things below are selected". It is controlled only: the host passes modelValue and listens for update:modelValue. The same component also renders the round variant (type="radio"), which is one row of a radio set.
- For a choice that excludes its siblings — exactly one of a set — use HkRadio, which owns the whole group and its options.
- For a setting that takes effect at once, with no form submit, use HkSwitch.
- For a numeric value picked along a range, use HkSlider or the stepper field HkNumberInput.
Import
;
use *; // Checkbox, CheckboxSize, CheckboxType
use *; // rsx!, VNode
The Rust form is a plain function, Checkbox(CheckboxProps { … }). Vue calls the state modelValue, a boolean that doubles as the tri-state marker (null renders the dash); the Rust mirror splits it into two flags, checked and indeterminate, because a Rust bool cannot carry null.
Checked, unchecked and indeterminate
modelValue is the whole state: true draws the check glyph, false draws an empty box, and null draws the indeterminate dash. A click on the dash reports true — the browser clears the dash and checks the box — so the same control can stand for "all of the rows below" and settle itself. In the Rust mirror that first box is the pair checked: false / indeterminate: true. The Vue example below is live: click the boxes to see the state follow the click.
HkCheckbox declares modelValue as a Boolean prop, so the null arm is a runtime state rather than a declared TypeScript type; a host that drives the dash keeps its own boolean | null state and passes it through. The page's examples stay on the declared type.
Sizes and the radio variant
size — sm / md (the default) / lg — scales the box, the glyph and the caption together. type="radio" swaps the square box and its check glyph for a round box with a dot, and indeterminate is ignored on that arm: a radio has no third state.
Disabled, caption and change
disabled dims the control, takes it out of the Tab order and silences the change: a click reports nothing. The caption is label, and the default slot wins over it when a caller needs markup rather than text — the same order the Rust mirror keeps between label and its children element. Every accepted toggle emits update:modelValue with the checked state the browser now reports; wire your own state there.
The Rust examples in this section attach an event handler; the fences run without a browser, so the output is the rendered snapshot.
API
Vue
Props
| Name | Type | Default | Required |
|---|---|---|---|
| modelValue | Boolean | false | — |
| label | String | undefined | — |
| disabled | Boolean | false | — |
| type | <code>String as PropType<"checkbox" | "radio"></code> | "checkbox" | — |
| size | <code>String as PropType<"sm" | "md" | "lg"></code> | "md" | — |
Emits
| Event | Payload |
|---|---|
| update:modelValue | boolean |
Rust
Props — Checkbox
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | bool | false | Controlled checked state (Vue: modelValue, default false). |
| indeterminate | bool | false | Controlled indeterminate marker (Vue: modelValue === null). |
| disabled | bool | false | — |
| input_type | CheckboxType | CheckboxType::Checkbox | Native input kind (Vue: type). |
| size | CheckboxSize | CheckboxSize::Md | — |
| label | Option<String> | None | Label text; the default slot (children) wins over it when present. |
| on_change | Option<EventHandler<bool>> | None | Emitted with the checked state after a toggle; a no-op while disabled. |
| children | Element | (empty) | Rich label content (Vue: the default slot). |
| class | String | "" | Vue attribute fallthrough onto the root <label>. |
| style | String | "" | — |
Accessibility
- The root is a <label> and the native <input type="checkbox"> sits inside it, so a click anywhere on the caption or the box activates the control, and the caption is its accessible name with no id wiring.
- The input owns the role and the checked state; the component adds no aria-checked of its own, because that would announce the state twice.
- The indeterminate state is the browser's own indeterminate flag on the input; the dash is drawn beside it and is not a second state to describe.
- disabled uses the native attribute, so the control leaves the Tab order and is announced as unavailable.
- The focus ring is drawn on the box from the nested input's :focus-visible, so the keyboard focus is visible even though the input is transparent.
Related components
- HkRadio — the single-choice sibling, one row per option with a group around them.
- HkSwitch — the immediate on/off setting.
- HkSlider — a choice along a numeric range.
- HkFormField — the caption and message shell for a control that needs a label line.