use leptos::prelude::*;
use leptos_style::Style;
const TOGGLE_CLASS: &str = "inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50";
#[component]
pub fn Toggle(
#[prop(into, optional)] variant: MaybeProp<String>,
#[prop(into, optional)] size: MaybeProp<String>,
#[prop(into, optional)] on_click: Option<Callback<()>>,
#[prop(into, optional)] disabled: Signal<bool>,
#[prop(into, optional)] class: MaybeProp<String>,
#[prop(into, optional)] id: MaybeProp<String>,
#[prop(into, optional)] style: Signal<Style>,
#[prop(optional)] children: Option<Children>,
) -> impl IntoView {
let handle_click = {
let on_click = on_click.clone();
move |_| {
if let Some(callback) = &on_click {
callback.run(());
}
}
};
let computed_class = Signal::derive(move || {
let variant_class = match variant.get().unwrap_or_default().as_str() {
"default" => "bg-primary text-primary-foreground hover:bg-primary/90",
"destructive" => "bg-destructive text-destructive-foreground hover:bg-destructive/90",
"outline" => "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
"secondary" => "bg-secondary text-secondary-foreground hover:bg-secondary/80",
"ghost" => "hover:bg-accent hover:text-accent-foreground",
"link" => "text-primary underline-offset-4 hover:underline",
_ => "bg-primary text-primary-foreground hover:bg-primary/90",
};
let size_class = match size.get().unwrap_or_default().as_str() {
"default" => "h-10 px-4 py-2",
"sm" => "h-9 rounded-md px-3",
"lg" => "h-11 rounded-md px-8",
"icon" => "h-10 w-10",
_ => "h-10 px-4 py-2",
};
format!("{} {} {} {}", TOGGLE_CLASS, variant_class, size_class, class.get().unwrap_or_default())
});
view! {
<button
class=computed_class
id=id.get().unwrap_or_default()
style=move || style.get().to_string()
disabled=disabled
on:click=handle_click
>
{children.map(|c| c())}
</button>
}
}