Checkbox
Select one or more values from a set.
1use herogpui::components::checkbox::Checkbox;Usage
Label content uses 14px medium text with 20px lines, independent of surrounding line height.
1use herogpui::prelude::{Checkbox, FieldVariant};
2use gpui::prelude::*;
3use gpui::px;
4
5gpui::div()
6 .flex()
7 .flex_col()
8 .items_start()
9 .gap(px(12.))
10 .children(vec![
11 Checkbox::new("checkbox-1")
12 .is_selected(basic)
13 .label(gpui::div().child("Accept the terms"))
14 .on_change(bool_cb(cx.listener(|this, v: &bool, _, cx| {
15 this.cb_basic = *v;
16 cx.notify();
17 })))
18 .into_any_element(),
19 Checkbox::new("checkbox-2")
20 .is_selected(colored)
21 .variant(FieldVariant::Secondary)
22 .label(gpui::div().child("Subscribe to updates"))
23 .on_change(bool_cb(cx.listener(|this, v: &bool, _, cx| {
24 this.cb_color = *v;
25 cx.notify();
26 })))
27 .into_any_element(),
28 ])Live HeroGPUI compiled to WebAssembly. Select any example without loading another WASM instance.
Anatomy
Rust types
Checkbox
Checkbox is assembled from these builders. The API reference lists each one.
Customization
Appearance builders and theme tokens Checkbox uses.
Styling
| Style | Description |
|---|---|
text_size + line_height | Label content uses 14px medium text with 20px lines, independent of surrounding line height. |
Checkbox::render flex_col + items_start + gap(px(4.)) | Field root and sibling help/error layout. |
row gap(px(12.)) + text_size(px(14.)) + FontWeight::MEDIUM | Clickable label row. |
16px + mark_radius + field background/shadow | Primary selection box metrics and field chrome. |
12px checkmark or dash | Indicator box and glyph size. |
selected accent fill swaps immediately | Selected background reveal. |
FieldVariant::Secondary + colors.default.color | Lower-emphasis surface variant. |
layout.disabled_opacity | Disabled opacity and cursor treatment. |
danger role + unchecked border | Invalid selected and unselected treatments. |
API reference
Builders
| Builder | Type | Default | Description |
|---|---|---|---|
is_selected( | bool | false | Whether the checkbox is checked (controlled). |
default_selected( | bool | false | Whether the checkbox is checked initially (uncontrolled). |
is_indeterminate( | bool | false | Whether the checkbox displays the indeterminate mark. |
is_disabled( | bool | false | Disables interaction, focus and form submission. |
is_invalid( | bool | false | Marks the checkbox invalid. |
is_read_only( | bool | false | Keeps the checkbox focusable while preventing selection changes. |
is_required( | bool | false | Requires the checkbox to be selected for native validation. |
validate( | Fn( | — | Custom validation function. |
validation_behavior( | ValidationBehavior | ValidationBehavior:: | Whether invalid state blocks form submission or is announced only. |
variant( | FieldVariant | FieldVariant:: | Primary field chrome or the flat surface treatment. |
name( | string | — | Submitted form field name. |
value( | string | — | Submitted value when selected. |
on_change( | Fn( | — | Handler called when selection changes. |
label( | AnyElement | | — | Static children compose or a render function receives the live field state for the clickable content; Description and FieldError remain static sibling builders. |
label( | AnyElement | | — | Clickable control and label content; static content is composed by the monolithic row. |
content( | | | — | Current selected state supplied to root content and the indicator renderer. |
content( | | | — | Current indeterminate state supplied to root content and the indicator renderer. |
content( | | | — | Current disabled state supplied to root content and the indicator renderer. |
content( | | | — | Current read-only state supplied to root content and the indicator renderer. |
content( | | | — | Current invalid state supplied to root content and the indicator renderer. |
content( | | | — | Current required state supplied to root content and the indicator renderer. |
Parts
| Part | Description |
|---|---|
Checkbox | Field root that owns value, validation, focus and form state. |
Checkbox | Clickable row containing the control and label. |
Checkbox | Focusable 16px selection box. |
Checkbox | Checkmark, indeterminate mark or caller-drawn indicator. |
Checkbox | Optional field help text rendered as an indented sibling below the content row. |
Checkbox | Validation message rendered as an indented sibling below the content row. |
States
| Builder | State | Description |
|---|---|---|
checked + accent control | Selected | Accent fill and checkmark are visible. |
is_indeterminate + dash | Indeterminate | Accent fill and dash indicator are visible. |
validation::resolve + danger role | Invalid | Danger treatment reflects controlled, server or custom validation. |
boxel.hover | Hovered | Active control uses the accent hover fill. |
tab_stop_handle + with_focus_ring | Focus visible | Keyboard focus rings the control. |
is_disabled + disabled_opacity | Disabled | Control is dimmed, inert and outside the tab order. |
on_click keyboard/pointer arbitration | Pressed | Press activation toggles on Space or pointer release; declares no additional pressed rule body. |
is_read_only gates activation only | Read only | Control remains focusable but selection cannot change. |