Switch
Rust UI component that displays a control that allows the user to toggle between checked and not checked.
utils
use leptos::prelude::*; use crate::components::ui::switch::Switch; #[component] pub fn DemoSwitch() -> impl IntoView { view! { <style> {" /** checked:translate-x-[-100%] checked:left-full */ .switch__label input:checked ~ .my__switch::before { transform: translateX(-100%); left: 100%; } .switch__label input:checked ~ .my__switch { background: var(--primary); } .my__switch::before { transition: 0.2s cubic-bezier(0.7, 0.15, 0.36, 1); } /* Use design system colors for better dark mode contrast */ .my__switch { background: var(--muted); } .switch__label input:checked ~ .my__switch { background: var(--primary); } "} </style> <div class="flex items-center space-x-2"> <Switch checked=true /> <label for="airplane">Airplane</label> </div> } }
Installation
You can run either of the following commands:
# cargo install ui-cli --forceui add demo_switchui add switch
Update the imports to match your project setup.
Copy and paste the following code into your project:
components/ui/switch.rs
use leptos::prelude::*; // mod components { // use super::*; // clx! {SwitchContainer, div, "flex items-center cursor-pointer select-none switch__container"} // div! {SwitchToggle, "relative p-[3px] bg-[#e1e1e1] rounded-[66px] w-[82.5px]", // "transition duration-[0.2s] ease-[cubic-bezier(0.7,0.15,0.36,1)]", // "before:size-[33px] before:rounded-[33px]", // "before:block before:bg-white before:relative before:left-0"} // } // pub use components::*; #[component] pub fn Switch(#[prop(into, optional)] checked: bool) -> impl IntoView { view! { <label class="flex items-center cursor-pointer select-none switch__label"> <input type="checkbox" hidden checked=checked /> <div class="relative my__switch p-[3px] rounded-[40px] w-[50px] before:size-[20px] before:rounded-[20px] transition duration-[0.2s] ease-[cubic-bezier(0.7,0.15,0.36,1)] before:block before:bg-white before:relative before:left-0" /> </label> } }
Update the imports to match your project setup.
Usage
// Coming soon 🦀