Skip to main content

ui_storybook/components/primitives/
toggle_switch.rs

1//! `ToggleSwitch` — on/off switch (M-UI.4 / AUT-124).
2//!
3//! Renders visual state from `checked`; does not flip itself. The
4//! parent owns the boolean and re-renders with the new value when the
5//! callback (omitted in SSR stories) fires.
6
7use leptos::prelude::*;
8
9#[component]
10pub fn ToggleSwitch(
11    /// Current on/off state.
12    checked: bool,
13    /// `true` to render disabled (dimmed + non-interactive).
14    #[prop(optional)]
15    disabled: bool,
16    /// Accessible label.
17    #[prop(into)]
18    label: String,
19) -> impl IntoView {
20    let mut class = String::from("toggle");
21    if checked {
22        class.push_str(" toggle-on");
23    }
24    if disabled {
25        class.push_str(" toggle-disabled");
26    }
27    view! {
28        <button
29            class=class
30            role="switch"
31            aria-checked=checked
32            aria-label=label
33            disabled=disabled
34        >
35            <span class="toggle-track">
36                <span class="toggle-knob"></span>
37            </span>
38        </button>
39    }
40}