Biji UI

Toggle Group

A set of toggle buttons where one or more can be pressed at a time.

Alignment (single)

Formatting (multiple)

Installation

biji-ui = { version = "0.5.0", features = ["toggle_group"] }

Usage

use leptos::prelude::*;
use biji_ui::components::toggle_group::{self, ToggleGroupType};

#[component]
pub fn MyToggleGroup() -> impl IntoView {
    view! {
        <toggle_group::Root
            group_type={ToggleGroupType::Single}
            class="inline-flex rounded-md border border-border"
        >
            <toggle_group::Item value="left" class="px-3 py-2 text-sm data-[state=on]:bg-accent">
                "Left"
            </toggle_group::Item>
            <toggle_group::Item value="center" class="px-3 py-2 text-sm data-[state=on]:bg-accent">
                "Center"
            </toggle_group::Item>
            <toggle_group::Item value="right" class="px-3 py-2 text-sm data-[state=on]:bg-accent">
                "Right"
            </toggle_group::Item>
        </toggle_group::Root>
    }
}

RootWith

Use <RootWith> when you need direct access to ToggleGroupState inside the children. The let:tg binding exposes tg.value as a reactive signal for reading the current selection without callbacks.

Alignment (single)

Selected: center

Formatting (multiple)

Active: bold

use leptos::prelude::*;
use biji_ui::components::toggle_group::{self, ToggleGroupType};

#[component]
pub fn AlignmentPicker() -> impl IntoView {
    let item_class = "px-3 py-2 text-sm font-medium transition-colors \
        first:rounded-l-md last:rounded-r-md border-r border-border last:border-r-0 \
        data-[state=on]:bg-accent data-[state=on]:text-accent-foreground";

    view! {
        <toggle_group::RootWith
            group_type={ToggleGroupType::Single}
            default_value="center"
            class="inline-flex rounded-md border border-border"
            let:tg
        >
            <toggle_group::Item value="left" class={item_class}>"Left"</toggle_group::Item>
            <toggle_group::Item value="center" class={item_class}>"Center"</toggle_group::Item>
            <toggle_group::Item value="right" class={item_class}>"Right"</toggle_group::Item>
            <p class="text-xs text-muted-foreground w-full text-center mt-2">
                "Selected: " {move || tg.value.with(|v| v.first().cloned().unwrap_or_default())}
            </p>
        </toggle_group::RootWith>
    }
}

API Reference

Root / RootWith

NameTypeDefaultDescription
classString""CSS class applied to the root wrapper element.
group_typeToggleGroupTypeSingleWhether one or multiple items can be pressed at a time.
valueOption<String>NoneInitial pressed value for Single mode.
valuesOption<Vec<String>>NoneInitial pressed values for Multiple mode.
disabledboolfalseWhen true, all items are disabled.

Item

NameTypeDefaultDescription
valueString The value associated with this item.
classString""CSS class applied to the button element.
disabledboolfalseWhen true, this item cannot be toggled.

ToggleGroupType

NameTypeDefaultDescription
SingleToggleGroupTypedefaultAt most one item can be pressed; pressing the active item deselects it.
MultipleToggleGroupType Any number of items can be pressed simultaneously.

Data Attributes

AttributeDescription
data-state"on" when the item is pressed, "off" otherwise. Present on Item.
data-disabledPresent on Item when the item or the Root is disabled.

Keyboard Navigation

KeyDescription
ArrowRight / ArrowDownMoves focus to the next item (wraps).
ArrowLeft / ArrowUpMoves focus to the previous item (wraps).
HomeMoves focus to the first item.
EndMoves focus to the last item.
Enter / SpaceToggles the focused item.