toggle
Edit a boolean through a binding while sharing the standard control activation behavior.
Use toggle(label, value: Binding<bool>) -> Toggle for a preference whose value already belongs to a view or shared model. The binding lets the control read and edit that value directly, so other views can observe the same preference without maintaining synchronized copies.
Boolean Bindings
use hera_kit::{toggle, view_fn};
let preferences = view_fn(|cx| {
let enabled = cx.state("enabled", || true);
Ok(toggle("Notifications", enabled))
});Here, preferences owns the mounted enabled slot and passes its binding to the toggle. The toggle retains its own interaction state through its internal button composition, while the preference itself remains owned by the parent.
Control Appearance
The current appearance is a button whose label combines the supplied text with On or Off. Activating it negates the bound value and allows evaluation to update the label. This API currently uses that button presentation rather than a platform switch with an animated thumb.
The control inherits ControlStyleKey and shared disabled behavior. Configure the environment when several controls need the same appearance.
Shared and Projected State
Another child can share the same value through a cloned binding. If the boolean lives inside a larger model value, a mapped binding can expose just that field, though changes still invalidate at the parent cell's granularity.
Do not initialize a separate boolean state slot from the prop on every mount unless independent local editing is intentional. Otherwise the control can drift away from the model it is meant to edit.
Preference Persistence
The binding's lifetime keeps the preference available in the runtime or mounted model, but it does not save it to disk. Preferences that must survive restarts or reach other processes need an application service to persist and distribute them, with results fed back into the model.
See state and bindings, environment, and button.
Component-Specific Reference
fn toggle(label: impl Into<String>, value: Binding<bool>) -> ToggleToggle has no additional component-specific builder methods. The label and boolean binding are construction inputs; the binding is the source of truth that activation reads and negates. Supply a different description when the label or binding changes rather than creating a second local copy of the preference.
The control uses a button internally and inherits the same ControlStyleKey. The button style reference describes its background, pressed background, foreground, padding, and corner-radius fields. Common disabled, layout, and appearance modifiers remain available through ViewExt.
Configuration Example
use hera_kit::{
toggle, view_fn, ControlStyle, ControlStyleKey, ViewExt,
};
let preference = view_fn(|cx| {
let enabled = cx.state("enabled", || true);
Ok(toggle("Notifications", enabled)
.environment::<ControlStyleKey>(ControlStyle::compact()))
});The view owns the mounted preference, and the style provider configures the toggle’s internal button. See View Modifiers for shared configuration rather than looking for toggle-specific style setters.
Source Reference
The following files in the Hera checkout define the behavior described on this page.
crates/kit/src/view/controls.rscrates/kit/src/ag/binding.rs