pub struct ToggleGroup { /* private fields */ }Expand description
A group of pressable toggle buttons for selection (shadcn/ui Toggle Group)
Supports single selection (radio-like) or multiple selection (checkbox-like).
§Example
// Single selection — clicking one deselects the others
let mut selected = vec![true, false, false];
ToggleGroup::new(ToggleGroupType::Single)
.variant(ToggleGroupVariant::Outline)
.show(ui, &["Bold", "Italic", "Underline"], &mut selected);
// Multiple selection — each item toggles independently
let mut selected = vec![false, false, false];
ToggleGroup::new(ToggleGroupType::Multiple)
.show(ui, &["Bold", "Italic", "Underline"], &mut selected);Implementations§
Source§impl ToggleGroup
impl ToggleGroup
Sourcepub const fn new(group_type: ToggleGroupType) -> ToggleGroup
pub const fn new(group_type: ToggleGroupType) -> ToggleGroup
Create a new toggle group
Sourcepub fn id(self, id: impl Into<Id>) -> ToggleGroup
pub fn id(self, id: impl Into<Id>) -> ToggleGroup
Set ID for state persistence
Sourcepub const fn variant(self, variant: ToggleGroupVariant) -> ToggleGroup
pub const fn variant(self, variant: ToggleGroupVariant) -> ToggleGroup
Set the visual variant
Sourcepub const fn size(self, size: ToggleGroupSize) -> ToggleGroup
pub const fn size(self, size: ToggleGroupSize) -> ToggleGroup
Set the size
Sourcepub const fn spacing(self, spacing: f32) -> ToggleGroup
pub const fn spacing(self, spacing: f32) -> ToggleGroup
Set spacing between items (0 = joined, >0 = separated)
Sourcepub const fn padding(self, padding: f32) -> ToggleGroup
pub const fn padding(self, padding: f32) -> ToggleGroup
Override horizontal padding around each item’s text. When set, this takes precedence over the size-based default padding.
Sourcepub const fn vertical(self, vertical: bool) -> ToggleGroup
pub const fn vertical(self, vertical: bool) -> ToggleGroup
Set vertical orientation
Sourcepub const fn disabled(self, disabled: bool) -> ToggleGroup
pub const fn disabled(self, disabled: bool) -> ToggleGroup
Set disabled state
Sourcepub const fn item_width(self, width: f32) -> ToggleGroup
pub const fn item_width(self, width: f32) -> ToggleGroup
Sourcepub fn show(
self,
ui: &mut Ui,
items: &[&str],
selected: &mut Vec<bool>,
) -> ToggleGroupResponse
pub fn show( self, ui: &mut Ui, items: &[&str], selected: &mut Vec<bool>, ) -> ToggleGroupResponse
Show the toggle group
selected is a bool per item. Will be resized to match items.len().
Single: clicking an item deselects all others (radio behavior)Multiple: each item toggles independently
Sourcepub fn show_ui(
self,
ui: &mut Ui,
count: usize,
selected: &mut Vec<bool>,
render_item: impl Fn(usize, &mut Ui, &ContentContext),
) -> ToggleGroupResponse
pub fn show_ui( self, ui: &mut Ui, count: usize, selected: &mut Vec<bool>, render_item: impl Fn(usize, &mut Ui, &ContentContext), ) -> ToggleGroupResponse
Show the toggle group with custom content for each item.
The closure receives the item index, a &mut Ui, and a ContentContext.
Use item_width to set uniform item width.
If not set, items default to square (height x height).
§Example
let mut selected = vec![false, false, false];
ToggleGroup::new(ToggleGroupType::Single)
.item_width(40.0)
.show_ui(ui, 3, &mut selected, |index, ui, ctx| {
// Render icon for item `index` using ctx.color
});