use dioxus::prelude::*;
use freya_elements as dioxus_elements;
use freya_hooks::{
use_applied_theme,
use_focus,
use_platform,
TileTheme,
TileThemeWith,
};
use winit::window::CursorIcon;
#[derive(Debug, Default, PartialEq, Clone, Copy)]
pub enum TileStatus {
#[default]
Idle,
Hovering,
}
#[allow(non_snake_case)]
#[component]
pub fn Tile(
children: Element,
leading: Option<Element>,
onselect: Option<EventHandler<()>>,
theme: Option<TileThemeWith>,
) -> Element {
let mut focus = use_focus();
let mut status = use_signal(TileStatus::default);
let platform = use_platform();
let TileTheme { padding } = use_applied_theme!(&theme, tile);
let focus_id = focus.attribute();
use_drop(move || {
if *status.read() == TileStatus::Hovering {
platform.set_cursor(CursorIcon::default());
}
});
let onclick = move |_| {
focus.focus();
if let Some(onselect) = &onselect {
onselect.call(())
}
};
let onmouseenter = move |_| {
platform.set_cursor(CursorIcon::Pointer);
status.set(TileStatus::Hovering);
};
let onmouseleave = move |_| {
platform.set_cursor(CursorIcon::default());
status.set(TileStatus::default());
};
rsx!(
rect {
onclick,
onmouseenter,
onmouseleave,
focus_id,
direction: "horizontal",
padding: "{padding}",
cross_align: "center",
if let Some(leading) = leading {
rect {
padding: "4",
{leading}
}
}
{children}
}
)
}