Skip to content

Commit 11bf9e4

Browse files
committed
fix: toolbar demo toggle buttons not toggling off
- Replace broken Vec-based toggle logic with individual boolean signals - Use Dioxus native CSS-in-RSX properties instead of style string memo - Add data-state attribute for visual button pressed state - Add CSS styling for pressed toolbar buttons
1 parent d1d14fd commit 11bf9e4

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

preview/src/components/toolbar/style.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@
2121

2222
.toolbar button:hover:not([disabled]),
2323
.toolbar button:focus-visible {
24-
background: var(--light, var(--primary-color-4))
24+
background: var(--light, var(--primary-color-4))
2525
var(--dark, var(--primary-color-7));
2626
color: var(--secondary-color-1);
2727
}
2828

29+
.toolbar button[data-state="on"] {
30+
background: var(--light, var(--primary-color-5))
31+
var(--dark, var(--primary-color-6));
32+
color: var(--secondary-color-1);
33+
}
34+
2935
.toolbar button:disabled {
3036
color: var(--secondary-color-5);
3137
cursor: not-allowed;

preview/src/components/toolbar/variants/main/mod.rs

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,61 @@ use dioxus::prelude::*;
33

44
#[component]
55
pub fn Demo() -> Element {
6-
let mut text_style = use_signal(Vec::new);
7-
let mut text_align = use_signal(|| String::from("left"));
8-
let mut toggle_style = move |style: &str| {
9-
let mut current_styles = text_style.write();
10-
current_styles.retain(|s| s != style);
11-
current_styles.push(style.to_string());
12-
};
13-
let mut set_align = move |align: &str| {
14-
text_align.set(align.to_string());
15-
};
16-
let text_styles = use_memo(move || {
17-
let mut classes = Vec::new();
18-
for style in text_style() {
19-
match style.as_str() {
20-
"bold" => classes.push("font-weight: bold;"),
21-
"italic" => classes.push("font-style: italic;"),
22-
"underline" => classes.push("text-decoration: underline;"),
23-
_ => {}
24-
}
25-
}
26-
classes.join(" ")
27-
});
28-
let text_align_style =
29-
use_memo(move || format!("text-align: {}; {}", text_align(), text_styles()));
6+
let mut is_bold = use_signal(|| false);
7+
let mut is_italic = use_signal(|| false);
8+
let mut is_underline = use_signal(|| false);
9+
let mut text_align = use_signal(|| "left".to_string());
10+
3011
rsx! {
3112
Toolbar { aria_label: "Text formatting",
3213
ToolbarGroup {
33-
ToolbarButton { index: 0usize, on_click: move |_| toggle_style("bold"), "Bold" }
34-
ToolbarButton { index: 1usize, on_click: move |_| toggle_style("italic"), "Italic" }
14+
ToolbarButton {
15+
index: 0usize,
16+
on_click: move |_| is_bold.toggle(),
17+
"data-state": if is_bold() { "on" } else { "off" },
18+
"Bold"
19+
}
20+
ToolbarButton {
21+
index: 1usize,
22+
on_click: move |_| is_italic.toggle(),
23+
"data-state": if is_italic() { "on" } else { "off" },
24+
"Italic"
25+
}
3526
ToolbarButton {
3627
index: 2usize,
37-
on_click: move |_| toggle_style("underline"),
28+
on_click: move |_| is_underline.toggle(),
29+
"data-state": if is_underline() { "on" } else { "off" },
3830
"Underline"
3931
}
4032
}
4133
ToolbarSeparator {}
4234
ToolbarGroup {
43-
ToolbarButton { index: 3usize, on_click: move |_| set_align("left"), "Align Left" }
44-
ToolbarButton { index: 4usize, on_click: move |_| set_align("center"), "Align Center" }
45-
ToolbarButton { index: 5usize, on_click: move |_| set_align("right"), "Align Right" }
35+
ToolbarButton {
36+
index: 3usize,
37+
on_click: move |_| text_align.set("left".to_string()),
38+
"data-state": if text_align() == "left" { "on" } else { "off" },
39+
"Align Left"
40+
}
41+
ToolbarButton {
42+
index: 4usize,
43+
on_click: move |_| text_align.set("center".to_string()),
44+
"data-state": if text_align() == "center" { "on" } else { "off" },
45+
"Align Center"
46+
}
47+
ToolbarButton {
48+
index: 5usize,
49+
on_click: move |_| text_align.set("right".to_string()),
50+
"data-state": if text_align() == "right" { "on" } else { "off" },
51+
"Align Right"
52+
}
4653
}
4754
}
48-
p { style: text_align_style, max_width: "30rem",
55+
p {
56+
max_width: "30rem",
57+
text_align: "{text_align}",
58+
font_weight: if is_bold() { "bold" } else { "normal" },
59+
font_style: if is_italic() { "italic" } else { "normal" },
60+
text_decoration: if is_underline() { "underline" } else { "none" },
4961
"This is a sample text that will be formatted according to the toolbar buttons you click. Try clicking the buttons above to see how the text formatting changes."
5062
}
5163
}

0 commit comments

Comments
 (0)