buttons.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. use dioxus::prelude::*;
  2. use dioxus_html::input_data::keyboard_types::Code;
  3. fn main() {
  4. dioxus_tui::launch(app);
  5. }
  6. #[component]
  7. fn Button(color_offset: u32, layer: u16) -> Element {
  8. let mut toggle = use_signal(|| false);
  9. let mut hovered = use_signal(|| false);
  10. let hue = color_offset % 255;
  11. let saturation = if toggle() { 50 } else { 25 } + if hovered() { 50 } else { 25 };
  12. let brightness = saturation / 2;
  13. let color = format!("hsl({hue}, {saturation}, {brightness})");
  14. rsx! {
  15. div{
  16. width: "100%",
  17. height: "100%",
  18. background_color: "{color}",
  19. tabindex: "{layer}",
  20. onkeydown: move |e| {
  21. if let Code::Space = e.code() {
  22. toggle.toggle();
  23. }
  24. },
  25. onclick: move |_| toggle.toggle(),
  26. onmouseenter: move |_| hovered.set(true),
  27. onmouseleave: move |_| hovered.set(false),
  28. justify_content: "center",
  29. align_items: "center",
  30. display: "flex",
  31. flex_direction: "column",
  32. p { "tabindex: {layer}" }
  33. }
  34. }
  35. }
  36. fn app() -> Element {
  37. rsx! {
  38. div {
  39. display: "flex",
  40. flex_direction: "column",
  41. width: "100%",
  42. height: "100%",
  43. for y in 1..8 {
  44. div {
  45. display: "flex",
  46. flex_direction: "row",
  47. width: "100%",
  48. height: "100%",
  49. for x in 1..8 {
  50. if (x + y) % 2 == 0 {
  51. div {
  52. width: "100%",
  53. height: "100%",
  54. background_color: "rgb(100, 100, 100)",
  55. }
  56. } else {
  57. Button {
  58. color_offset: x * y,
  59. layer: ((x + y) % 3) as u16,
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }