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> {
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;
render!(
rect {
overflow: "clip",
margin: "2",
onclick: onclick,
onmouseenter: onmouseenter,
onmouseleave: onmouseleave,
focus_id: focus_id,
focusable: "true",
role: "button",
width: "auto",
height: "auto",
direction: "both",
color: "{color}",
shadow: "0 5 20 0 rgb(0, 0, 0, 100)",
corner_radius: "5",
padding: "8",
background: "{background}",
&cx.props.children
}
)
}