1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
use dioxus::prelude::*;
use freya_elements as dioxus_elements;
use freya_hooks::{
use_activable_route,
use_applied_theme,
use_focus,
use_platform,
BottomTabTheme,
BottomTabThemeWith,
TabTheme,
TabThemeWith,
};
use winit::window::CursorIcon;
/// Horizontal container for Tabs. Use in combination with [`Tab`]
#[allow(non_snake_case)]
#[component]
pub fn Tabsbar(children: Element) -> Element {
rsx!(
rect {
direction: "horizontal",
{children}
}
)
}
/// Current status of the Tab.
#[derive(Debug, Default, PartialEq, Clone, Copy)]
pub enum TabStatus {
/// Default state.
#[default]
Idle,
/// Mouse is hovering the Tab.
Hovering,
}
/// Clickable Tab. Usually used in combination with [`Tabsbar`], [`Link`] and [`ActivableRoute`].
///
/// # Styling
/// Inherits the [`TabTheme`](freya_hooks::TabTheme) theme.
///
/// # Example
///
/// ```no_run
/// # use freya::prelude::*;
/// # use dioxus_router::prelude::Routable;
/// # #[allow(non_snake_case)]
/// # fn PageNotFound() -> Element { None }
/// # #[allow(non_snake_case)]
/// # fn Settings() -> Element { None }
/// # #[derive(Routable, Clone, PartialEq)]
/// # #[rustfmt::skip]
/// # pub enum Route {
/// # #[route("/settings")]
/// # Settings,
/// # #[route("/..route")]
/// # PageNotFound { },
/// # }
/// fn app() -> Element {
/// rsx!(
/// Tabsbar {
/// Tab {
/// label {
/// "Home"
/// }
/// }
/// Link {
/// to: Route::Settings,
/// Tab {
/// label {
/// "Go to Settings"
/// }
/// }
/// }
/// }
/// )
/// }
/// ```
#[allow(non_snake_case)]
#[component]
pub fn Tab(children: Element, theme: Option<TabThemeWith>) -> Element {
let focus = use_focus();
let mut status = use_signal(TabStatus::default);
let platform = use_platform();
let is_active = use_activable_route();
let focus_id = focus.attribute();
let TabTheme {
background,
hover_background,
border_fill,
focus_border_fill,
padding,
width,
height,
font_theme,
} = use_applied_theme!(&theme, tab);
use_drop(move || {
if *status.read() == TabStatus::Hovering {
platform.set_cursor(CursorIcon::default());
}
});
let onmouseenter = move |_| {
platform.set_cursor(CursorIcon::Pointer);
status.set(TabStatus::Hovering);
};
let onmouseleave = move |_| {
platform.set_cursor(CursorIcon::default());
status.set(TabStatus::default());
};
let background = match *status.read() {
TabStatus::Hovering => hover_background,
TabStatus::Idle => background,
};
let border = if focus.is_selected() || is_active {
focus_border_fill
} else {
border_fill
};
rsx!(
rect {
onmouseenter,
onmouseleave,
focus_id,
width: "{width}",
height: "{height}",
focusable: "true",
overflow: "clip",
role: "tab",
color: "{font_theme.color}",
background: "{background}",
text_align: "center",
content: "fit",
rect {
padding: "{padding}",
main_align: "center",
cross_align: "center",
{children}
}
rect {
height: "2",
width: "fill-min",
background: "{border}"
}
}
)
}
/// Clickable BottomTab. Same thing as Tab but designed to be placed in the bottom of your app,
/// usually used in combination with [`Tabsbar`], [`Link`] and [`ActivableRoute`].
///
/// # Styling
/// Inherits the [`BottomTabTheme`](freya_hooks::BottomTabTheme) theme.
///
/// # Example
///
/// ```no_run
/// # use freya::prelude::*;
/// # use dioxus_router::prelude::Routable;
/// # #[allow(non_snake_case)]
/// # fn PageNotFound() -> Element { None }
/// # #[allow(non_snake_case)]
/// # fn Settings() -> Element { None }
/// # #[derive(Routable, Clone, PartialEq)]
/// # #[rustfmt::skip]
/// # pub enum Route {
/// # #[route("/settings")]
/// # Settings,
/// # #[route("/..route")]
/// # PageNotFound { },
/// # }
/// fn app() -> Element {
/// rsx!(
/// Tabsbar {
/// BottomTab {
/// label {
/// "Home"
/// }
/// }
/// Link {
/// to: Route::Settings,
/// BottomTab {
/// label {
/// "Go to Settings"
/// }
/// }
/// }
/// }
/// )
/// }
/// ```
#[allow(non_snake_case)]
#[component]
pub fn BottomTab(children: Element, theme: Option<BottomTabThemeWith>) -> Element {
let focus = use_focus();
let mut status = use_signal(TabStatus::default);
let platform = use_platform();
let is_active = use_activable_route();
let focus_id = focus.attribute();
let BottomTabTheme {
background,
hover_background,
padding,
width,
height,
font_theme,
} = use_applied_theme!(&theme, bottom_tab);
use_drop(move || {
if *status.read() == TabStatus::Hovering {
platform.set_cursor(CursorIcon::default());
}
});
let onmouseenter = move |_| {
platform.set_cursor(CursorIcon::Pointer);
status.set(TabStatus::Hovering);
};
let onmouseleave = move |_| {
platform.set_cursor(CursorIcon::default());
status.set(TabStatus::default());
};
let background = match *status.read() {
_ if focus.is_selected() || is_active => hover_background,
TabStatus::Hovering => hover_background,
TabStatus::Idle => background,
};
rsx!(
rect {
onmouseenter,
onmouseleave,
focus_id,
width: "{width}",
height: "{height}",
focusable: "true",
overflow: "clip",
role: "tab",
color: "{font_theme.color}",
background: "{background}",
text_align: "center",
padding: "{padding}",
main_align: "center",
cross_align: "center",
corner_radius: "99",
margin: "2 4",
{children}
}
)
}