Skip to content

Commit 99dcf02

Browse files
authored
feat: General cleanup of freya's components (#313)
1 parent 7de24b4 commit 99dcf02

File tree

19 files changed

+58
-65
lines changed

19 files changed

+58
-65
lines changed

book/src/guides/animating.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ fn main() {
2323
2424
let progress = animation.value();
2525
26-
use_effect(cx, (), move |_| {
26+
use_memo(cx, (), move |_| {
2727
animation.start(Animation::new_linear(0.0..=100.0, 50));
28-
async move {}
2928
});
3029
3130
render!(rect {

crates/components/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dioxus-router = { workspace = true }
3131

3232
winit = { workspace = true }
3333
tokio = { workspace = true }
34+
tracing = { workspace = true }
3435

3536
open = "1"
3637
reqwest = { version = "0.11.13", features = ["json"] }

crates/components/src/accordion.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use dioxus::prelude::*;
22
use freya_elements::elements as dioxus_elements;
33
use freya_elements::events::MouseEvent;
4-
use freya_hooks::{use_animation, use_get_theme, use_node, Animation};
4+
use freya_hooks::{use_animation, use_get_theme, use_node, AccordionTheme, Animation};
55

66
/// [`Accordion`] component properties.
77
#[derive(Props)]
@@ -23,15 +23,15 @@ pub struct AccordionProps<'a> {
2323
#[allow(non_snake_case)]
2424
pub fn Accordion<'a>(cx: Scope<'a, AccordionProps<'a>>) -> Element<'a> {
2525
let theme = use_get_theme(cx);
26-
let accordion_theme = &theme.accordion;
2726
let animation = use_animation(cx, || 0.0);
2827
let open = use_state(cx, || false);
2928
let (node_ref, size) = use_node(cx);
3029

3130
let animation_value = animation.value();
31+
let AccordionTheme { background, color } = theme.accordion;
3232

3333
// Adapt the accordion if the body size changes
34-
use_effect(
34+
use_memo(
3535
cx,
3636
&(
3737
size.area.width(),
@@ -44,7 +44,6 @@ pub fn Accordion<'a>(cx: Scope<'a, AccordionProps<'a>>) -> Element<'a> {
4444
if (height as f64) < animation.value() && !animating {
4545
animation.set_value(size.area.height() as f64);
4646
}
47-
async move {}
4847
}
4948
},
5049
);
@@ -62,12 +61,12 @@ pub fn Accordion<'a>(cx: Scope<'a, AccordionProps<'a>>) -> Element<'a> {
6261
render!(
6362
rect {
6463
overflow: "clip",
65-
color: "{accordion_theme.color}",
64+
color: "{color}",
6665
padding: "10",
6766
corner_radius: "3",
6867
width: "100%",
6968
height: "auto",
70-
background: "{accordion_theme.background}",
69+
background: "{background}",
7170
onclick: onclick,
7271
&cx.props.summary
7372
rect {

crates/components/src/dropdown.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,8 @@ where
170170
let color = theme.dropdown.font_theme.color;
171171

172172
// Update the provided value if the passed value changes
173-
use_effect(cx, &cx.props.value, move |value| {
173+
use_memo(cx, &cx.props.value, move |value| {
174174
*selected.write() = value;
175-
async move {}
176175
});
177176

178177
// Close the dropdown if clicked anywhere

crates/components/src/external_link.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub struct ExternalLinkProps<'a> {
4747
#[allow(non_snake_case)]
4848
pub fn ExternalLink<'a>(cx: Scope<'a, ExternalLinkProps<'a>>) -> Element {
4949
let theme = use_get_theme(cx);
50-
let theme = &theme.external_link;
5150
let is_hovering = use_state(cx, || false);
5251
let show_tooltip = cx.props.show_tooltip.unwrap_or(true);
5352

@@ -68,7 +67,7 @@ pub fn ExternalLink<'a>(cx: Scope<'a, ExternalLinkProps<'a>>) -> Element {
6867
};
6968

7069
let color = if *is_hovering.get() {
71-
theme.highlight_color
70+
theme.external_link.highlight_color
7271
} else {
7372
"inherit"
7473
};

crates/components/src/gesture_area.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type EventsQueue = VecDeque<(Instant, TouchEvent)>;
6262
pub fn GestureArea<'a>(cx: Scope<'a, GestureAreaProps<'a>>) -> Element {
6363
let touch_events = use_ref::<EventsQueue>(cx, VecDeque::new);
6464

65-
use_effect(cx, touch_events, move |_| {
65+
use_memo(cx, touch_events, move |_| {
6666
// Keep the touch events queue under a certain size
6767
if touch_events.read().len() > MAX_EVENTS_QUEUE {
6868
touch_events.write_silent().pop_front();
@@ -132,8 +132,6 @@ pub fn GestureArea<'a>(cx: Scope<'a, GestureAreaProps<'a>>) -> Element {
132132
_ => {}
133133
}
134134
}
135-
136-
async move {}
137135
});
138136

139137
let ontouchcancel = |e: TouchEvent| {

crates/components/src/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct GraphProps {
4545
pub fn Graph(cx: Scope<GraphProps>) -> Element {
4646
let platform = use_platform(cx);
4747

48-
use_effect(cx, (cx.props,), move |_| async move {
48+
use_memo(cx, (cx.props,), move |_| {
4949
platform.send(EventMessage::RequestRerender)
5050
});
5151

crates/components/src/input.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use crate::ScrollView;
33
use dioxus::prelude::*;
44
use freya_elements::elements as dioxus_elements;
55
use freya_elements::events::{KeyboardData, MouseEvent};
6+
use freya_hooks::ButtonTheme;
7+
use freya_hooks::FontTheme;
68
use freya_hooks::{
79
use_editable, use_focus, use_get_theme, EditableConfig, EditableEvent, EditableMode, TextEditor,
810
};
@@ -66,20 +68,18 @@ pub fn Input<'a>(cx: Scope<'a, InputProps<'a>>) -> Element {
6668
let focus_manager = use_focus(cx);
6769

6870
let text = &cx.props.value;
69-
let button_theme = &theme.button;
7071
let cursor_attr = editable.cursor_attr(cx);
7172
let highlights_attr = editable.highlights_attr(cx, 0);
7273
let width = &cx.props.width;
7374
let height = &cx.props.height;
7475
let max_lines = &cx.props.max_lines;
7576

76-
use_effect(cx, &(cx.props.value.to_string(),), {
77+
use_memo(cx, &(cx.props.value.to_string(),), {
7778
to_owned![editable];
7879
move |(text,)| {
7980
editable.editor().with_mut(|editor| {
8081
editor.set(&text);
8182
});
82-
async move {}
8383
}
8484
});
8585

@@ -122,8 +122,11 @@ pub fn Input<'a>(cx: Scope<'a, InputProps<'a>>) -> Element {
122122
} else {
123123
"none".to_string()
124124
};
125-
let background = button_theme.background;
126-
let color = button_theme.font_theme.color;
125+
let ButtonTheme {
126+
background,
127+
font_theme: FontTheme { color, .. },
128+
..
129+
} = theme.button;
127130

128131
render!(
129132
CursorArea {

crates/components/src/loader.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::time::Duration;
22

33
use dioxus::prelude::*;
44
use freya_elements::elements as dioxus_elements;
5-
use freya_hooks::use_get_theme;
5+
use freya_hooks::{use_get_theme, LoaderTheme};
66
use tokio::time::interval;
77

88
/// [`Loader`] component properties. Currently empty.
@@ -22,7 +22,10 @@ pub fn Loader(cx: Scope<LoaderProps>) -> Element {
2222
let theme = use_get_theme(cx);
2323
let degrees = use_state(cx, || 0);
2424

25-
let loader_theme = theme.loader;
25+
let LoaderTheme {
26+
primary_color,
27+
secondary_color,
28+
} = theme.loader;
2629

2730
use_effect(cx, (), move |_| {
2831
to_owned![degrees];
@@ -45,8 +48,8 @@ pub fn Loader(cx: Scope<LoaderProps>) -> Element {
4548
height: "31",
4649
svg_content: r#"
4750
<svg width="31" height="31" viewBox="0 0 31 31" fill="none" xmlns="http://www.w3.org/2000/svg">
48-
<path d="M15.5235 27.6652C22.2292 27.6652 27.6652 22.2292 27.6652 15.5235C27.6652 8.81783 22.2292 3.38182 15.5235 3.38182C8.81783 3.38182 3.38182 8.81783 3.38182 15.5235C3.38182 22.2292 8.81783 27.6652 15.5235 27.6652Z" stroke="{loader_theme.primary_color}" stroke-width="4"/>
49-
<path d="M27.6652 15.5235C27.6652 8.81859 22.2284 3.38182 15.5235 3.38182" stroke="{loader_theme.secondary_color}" stroke-width="4"/>
51+
<path d="M15.5235 27.6652C22.2292 27.6652 27.6652 22.2292 27.6652 15.5235C27.6652 8.81783 22.2292 3.38182 15.5235 3.38182C8.81783 3.38182 3.38182 8.81783 3.38182 15.5235C3.38182 22.2292 8.81783 27.6652 15.5235 27.6652Z" stroke="{primary_color}" stroke-width="4"/>
52+
<path d="M27.6652 15.5235C27.6652 8.81859 22.2284 3.38182 15.5235 3.38182" stroke="{secondary_color}" stroke-width="4"/>
5053
</svg>
5154
"#
5255
})

crates/components/src/scroll_views/scroll_bar.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use dioxus::prelude::*;
22
use freya_elements::elements as dioxus_elements;
3-
use freya_hooks::use_get_theme;
3+
use freya_hooks::{use_get_theme, ScrollbarTheme};
44

55
#[derive(Props)]
66
pub struct ScrollBarProps<'a> {
@@ -22,7 +22,8 @@ pub struct ScrollBarProps<'a> {
2222
#[allow(non_snake_case)]
2323
pub fn ScrollBar<'a>(cx: Scope<'a, ScrollBarProps<'a>>) -> Element<'a> {
2424
let theme = use_get_theme(cx);
25-
let scrollbar_theme = &theme.scrollbar;
25+
let ScrollbarTheme { background, .. } = &theme.scrollbar;
26+
2627
render!(
2728
rect {
2829
overflow: "clip",
@@ -31,7 +32,7 @@ pub fn ScrollBar<'a>(cx: Scope<'a, ScrollBarProps<'a>>) -> Element<'a> {
3132
height: "{cx.props.height}",
3233
offset_x: "{cx.props.offset_x}",
3334
offset_y: "{cx.props.offset_y}",
34-
background: "{scrollbar_theme.background}",
35+
background: "{background}",
3536
&cx.props.children
3637
}
3738
)

0 commit comments

Comments
 (0)