use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_elements::events::MouseEvent;
use freya_hooks::{use_focus, use_get_theme};
#[derive(Props)]
pub struct ButtonProps<'a> {
#[props(default = "10 14".to_string(), into)]
pub padding: String,
#[props(default = "4".to_string(), into)]
pub margin: String,
#[props(default = "10".to_string(), into)]
pub corner_radius: String,
#[props(default = "auto".to_string(), into)]
pub width: String,
#[props(default = "auto".to_string(), into)]
pub height: String,
pub children: Element<'a>,
#[props(optional)]
pub onclick: Option<EventHandler<'a, MouseEvent>>,
}
#[derive(Debug, Default, PartialEq, Clone, Copy)]
pub enum ButtonStatus {
#[default]
Idle,
Hovering,
}
#[allow(non_snake_case)]
pub fn Button<'a>(cx: Scope<'a, ButtonProps<'a>>) -> Element {
let focus = use_focus(cx);
let theme = use_get_theme(cx);
let status = use_state(cx, ButtonStatus::default);
let focus_id = focus.attribute(cx);
let onclick = move |ev| {
focus.focus();
if let Some(onclick) = &cx.props.onclick {
onclick.call(ev)
}
};
let onmouseenter = move |_| {
status.set(ButtonStatus::Hovering);
};
let onmouseleave = move |_| {
status.set(ButtonStatus::default());
};
let background = match *status.get() {
ButtonStatus::Hovering => theme.button.hover_background,
ButtonStatus::Idle => theme.button.background,
};
let color = theme.button.font_theme.color;
let border_fill = theme.button.border_fill;
let ButtonProps {
width,
height,
corner_radius,
padding,
margin,
..
} = &cx.props;
render!(
rect {
onclick: onclick,
onmouseenter: onmouseenter,
onmouseleave: onmouseleave,
focus_id: focus_id,
width: "{width}",
height: "{height}",
padding: "{padding}",
margin: "{margin}",
focusable: "true",
overflow: "clip",
role: "button",
color: "{color}",
shadow: "0 4 5 0 rgb(0, 0, 0, 30)",
border: "1 solid {border_fill}",
corner_radius: "{corner_radius}",
background: "{background}",
align: "center",
main_align: "center",
cross_align: "center",
&cx.props.children
}
)
}