Buttons

Toggle Button

Toggle between a selected and unselected state.

Rust
1use herogpui::components::toggle_button::{ToggleButton, ToggleButtonGroup};

Usage

Rust
1use herogpui::prelude::{ToggleButton, ToggleButtonGroup};
2
3ToggleButton::new("toggle-button")
4    .label("Bold")

Live HeroGPUI compiled to WebAssembly. Select any example without loading another WASM instance.

Anatomy

Rust types

ToggleButtonToggleButtonGroup

Toggle Button is assembled from these builders. The API reference lists each one.

Customization

Appearance builders and theme tokens Toggle Button uses.

Styling

Toggle Button styling
StyleDescription
flex + h(36px) + gap(px(8.)) + px(16.) + control_radiusBase toggle geometry, typography and rounded control surface.
no .border_1()The toggle has no border; selected state uses a fill and focus uses a ring.
ToggleVariant::Default/Ghost + colors.defaultDefault unselected tokens, with the ghost variant supplying transparent background.
is_selected + colors.accent.soft/soft_foreground/soft_hoverSelected toggles use the accent-soft palette rather than the solid accent fill.
ToggleVariant::DefaultFilled default variant token overrides.
ToggleVariant::GhostLow-emphasis ghost variant token overrides.
Size::Sm + PRESSED_SCALE_SUBTLESmall desktop metrics and the subtle size-specific press scale.
Size::Md + PRESSED_SCALEMedium is the base size and uses the standard press scale.
Size::Lg + PRESSED_SCALE_FIRMLarge desktop height, typography, default horizontal padding and firm press scale.
is_icon_only + width(height)Square medium icon-only control.
is_icon_only + size-specific heightSmall and large icon-only square sizes.
child(element)Built-in SVG icon sizing and alignment; arbitrary child elements are accepted by without this selector-specific treatment.
anim::pressed + immediate hover/backgroundTransitions press geometry and color; applies the geometry in one frame and does not interpolate the color or press ramp.
flex + items_center + justify_center + gap(px(0.))Connected group container.
orientation(SelectionOrientation)Orientation-controlled group axis.
full_width(true) + slot.flex_1Full-width group and equal-width member slots.
group_edge + group_radius_anyAttached members merge into one control while retaining the group perimeter radius.
if !is_grouped { anim::pressed(...) }Attached members suppress their individual press scale.
ring_if_focused(offset = false)Removes the offset seam but cannot draw GPUI's shadow ring inset.
absolute + foreground.alpha(0.15) + hairline_radiusUses a fixed foreground-derived color rather than each member's currentColor.
separator slot geometry in ToggleButtonGroup::renderOrientation-specific divider geometry.
static separator divFades separator opacity; draws the correct static geometry without that transition.
is_detached(true) + gap(px(4.)) + GroupEdge::OnlyDetached groups separate members, restore full radii and hide dividers.

API reference

Builders

Toggle Button builders
BuilderTypeDefaultDescription
variant(ToggleVariant)ToggleVariantToggleVariant::DefaultVisual style variant
size(Size)SizeSize::MdSize of the toggle button
is_icon_only(bool)boolfalseWhether the button contains only an icon
is_selected(bool)boolControlled selected state
default_selected(bool)boolfalseDefault selected state (uncontrolled)
is_disabled(bool)boolfalseWhether the toggle button is disabled
on_change(callback)Fn(isSelected: bool) -> ()Handler called when selection changes
on_press(event)eventHandler called when the button is pressed
label(value) / content(render) / child(element)AnyElement | Fn(values: ToggleButtonRenderProps) -> AnyElementButton content or render prop
content(render)boolWhether the button is currently selected
selection_mode(SelectionMode)SelectionModeSelectionMode::SingleWhether one or multiple buttons can be selected
selected_keys(keys)iterable of keysControlled selection state; accepts SharedString keys only.
default_selected_keys(keys)iterable of keysDefault selected keys (uncontrolled); accepts SharedString keys only.
on_selection_change(callback)Fn(keys: Set<Key>) -> ()Called when selection changes; reports SharedString keys only.
disallow_empty_selection(bool)boolfalsePrevents clearing all selections
orientation(SelectionOrientation)SelectionOrientationSelectionOrientation::HorizontalLayout direction
is_detached(bool)boolfalseWhether buttons are visually separated with gaps
full_width(bool)boolfalseWhether the group fills available width

Parts

Toggle Button parts
PartDescription
ToggleButtonFocusable toggle root that owns selected, press and disabled state.
ToggleButtonGroupSelection and layout root; separators are explicit composition requested with separators(true).
ToggleButtonGroupOptional explicit divider composition; uses separators(true), and custom style is unavailable.

States

Toggle Button states
BuilderStateDescription
is_selected + colors.accent.soft()SelectedAccent-soft background and foreground show the selected state.
hover + InteractiveState::is_hoveredHoveredVariant-specific hover background, including selected accent-soft-hover.
anim::pressed + InteractiveState::is_pressedPressedPressed feedback applies the size-specific scale and pressed background.
tab_stop_handle + ring_if_focusedFocus visibleKeyboard focus shows the status-focused ring.
is_disabled + disabled_opacityDisabledDisabled toggles are dimmed, inert and removed from the tab order.
selected_keys(keys) + util::controlledControlled selectionThe caller owns the selected key set and receives selection callbacks.
default_selected_keys(keys) + util::controlledUncontrolled selectionThe group owns its selected key set after the initial seed.
selection_mode(SelectionMode::Single)Single selectionSelecting a member replaces the previous selection.
selection_mode(SelectionMode::Multiple)Multiple selectionMembers can be selected independently.
disallow_empty_selection(bool)Disallow empty selectionThe last selected member cannot be cleared in single-selection mode.
orientation(SelectionOrientation::Horizontal)Horizontal groupMembers flow along the horizontal axis.
orientation(SelectionOrientation::Vertical)Vertical groupMembers stack along the vertical axis.
full_width(true) + child slots flex_1Full width groupThe group fills its parent and stretches each member.
group_edge + group_radius_anyAttached groupAdjacent members share an edge and keep only the outer corners rounded.
is_detached(true)Detached groupMembers regain full radii and are separated by a 4px gap.
is_grouped gates anim::pressedGroup pressedAttached members do not scale and therefore cannot open gaps at the seam.
ring_if_focused(offset = false)Group focus visibleRemoves the outer offset but GPUI has no inset box-shadow equivalent.