Skip to content

Commit

Permalink
feat: Keyboard navigation support for Checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Sep 27, 2024
1 parent 8b09791 commit f57a175
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
44 changes: 32 additions & 12 deletions crates/components/src/checkbox.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_elements::{
elements as dioxus_elements,
events::KeyboardEvent,
};
use freya_hooks::{
use_applied_theme,
use_focus,
CheckboxTheme,
CheckboxThemeWith,
};
Expand Down Expand Up @@ -68,30 +72,46 @@ pub fn Checkbox(
/// Theme override.
theme: Option<CheckboxThemeWith>,
) -> Element {
let focus = use_focus();
let CheckboxTheme {
border_fill,
unselected_fill,
selected_fill,
selected_icon_fill,
} = use_applied_theme!(&theme, checkbox);
let (fill, border) = if selected {
let (inner_fill, outer_fill) = if selected {
(selected_fill.as_ref(), selected_fill.as_ref())
} else {
("transparent", unselected_fill.as_ref())
};
let border = if focus.is_selected() {
format!("4 solid {}", border_fill)
} else {
"none".to_string()
};

let onkeydown = move |_: KeyboardEvent| {};

rsx!(
rect {
width: "18",
height: "18",
padding: "4",
main_align: "center",
cross_align: "center",
border,
border_align: "outer",
corner_radius: "4",
border: "2 solid {border}",
background: "{fill}",
if selected {
TickIcon {
fill: selected_icon_fill
rect {
a11y_id: focus.attribute(),
width: "18",
height: "18",
padding: "4",
main_align: "center",
cross_align: "center",
corner_radius: "4",
border: "2 solid {outer_fill}",
background: "{inner_fill}",
onkeydown,
if selected {
TickIcon {
fill: selected_icon_fill
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/hooks/src/theming/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub(crate) const BASE_THEME: Theme = Theme {
unselected_fill: cow_borrowed!("key(solid)"),
selected_fill: cow_borrowed!("key(primary)"),
selected_icon_fill: cow_borrowed!("key(secondary)"),
border_fill: cow_borrowed!("key(surface)"),
},
menu_item: MenuItemTheme {
hover_background: cow_borrowed!("key(focused_surface)"),
Expand Down
1 change: 1 addition & 0 deletions crates/hooks/src/theming/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ define_theme! {
unselected_fill: str,
selected_fill: str,
selected_icon_fill: str,
border_fill: str,
}
}

Expand Down

0 comments on commit f57a175

Please sign in to comment.