change button example to showcase tabindex

This commit is contained in:
Evan Almloff committed 2022-05-03 17:50:04 -05:00
1 parent 9ed722ae45
commit 592f6ca225
1 file changed
+68 -35
+68 -35
View File
@@ -1,28 +1,51 @@
#![allow(non_snake_case)] use dioxus::{events::KeyCode, prelude::*};
use dioxus::prelude::*;
fn main() { fn main() {
dioxus::tui::launch(app); dioxus::tui::launch(app);
} }
fn Button(cx: Scope) -> Element { #[derive(PartialEq, Props)]
let state = use_state(&cx, || false); struct ButtonProps {
let color = if *state.get() { "red" } else { "blue" }; color_offset: u32,
let text = if *state.get() { "☐" } else { "☒" }; layer: u16,
}
#[allow(non_snake_case)]
fn Button(cx: Scope<ButtonProps>) -> Element {
let toggle = use_state(&cx, || false);
let hovered = use_state(&cx, || false);
let hue = cx.props.color_offset % 255;
let saturation = if *toggle.get() { 50 } else { 25 } + if *hovered.get() { 50 } else { 25 };
let brightness = saturation / 2;
let color = format!("hsl({hue}, {saturation}, {brightness})");
cx.render(rsx! { cx.render(rsx! {
div { div{
border_width: "1px", width: "100%",
width: "50%",
height: "100%", height: "100%",
background_color: "{color}", background_color: "{color}",
tabindex: "{cx.props.layer}",
onkeydown: |e| {
if let KeyCode::Space = e.data.key_code{
toggle.modify(|f| !f);
}
},
onclick: |_| {
toggle.modify(|f| !f);
},
onmouseenter: |_|{
hovered.set(true);
},
onmouseleave: |_|{
hovered.set(false);
},
justify_content: "center", justify_content: "center",
align_items: "center", align_items: "center",
onkeydown: |_| state.modify(|s| !s), display: "flex",
onclick: |_| state.modify(|s| !s), flex_direction: "column",
"{text}" p{"tabindex: {cx.props.layer}"}
} }
}) })
} }
@@ -30,31 +53,41 @@ fn Button(cx: Scope) -> Element {
fn app(cx: Scope) -> Element { fn app(cx: Scope) -> Element {
cx.render(rsx! { cx.render(rsx! {
div { div {
display: "flex",
flex_direction: "column",
width: "100%", width: "100%",
height: "100%", height: "100%",
flex_direction: "column",
div { (1..8).map(|y|
width: "100%", cx.render(rsx!{
height: "50%", div{
flex_direction: "row", display: "flex",
flex_direction: "row",
Button{}, width: "100%",
Button{}, height: "100%",
Button{}, (1..8).map(|x|{
Button{}, if (x + y) % 2 == 0{
} cx.render(rsx!{
div{
div { width: "100%",
width: "100%", height: "100%",
height: "50%", background_color: "rgb(100, 100, 100)",
flex_direction: "row", }
})
Button{}, }
Button{}, else{
Button{}, let layer = (x + y) % 3;
Button{}, cx.render(rsx!{
} Button{
color_offset: x * y,
layer: layer as u16,
}
})
}
})
}
})
)
} }
}) })
} }