Skip to content

Commit

Permalink
feat: Improve design and UX of Accordion (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 authored Nov 20, 2023
1 parent 8220b9a commit 59ef807
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
51 changes: 48 additions & 3 deletions crates/components/src/accordion.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_elements::events::MouseEvent;
use freya_hooks::{use_animation, use_get_theme, use_node, AccordionTheme, Animation};
use freya_hooks::{
use_animation, use_get_theme, use_node, use_platform, AccordionTheme, Animation,
};
use winit::window::CursorIcon;

/// Indicates the current status of the accordion.
#[derive(Debug, Default, PartialEq, Clone, Copy)]
pub enum AccordionStatus {
/// Default state.
#[default]
Idle,
/// Mouse is hovering the accordion.
Hovering,
}

/// [`Accordion`] component properties.
#[derive(Props)]
Expand All @@ -26,9 +39,15 @@ pub fn Accordion<'a>(cx: Scope<'a, AccordionProps<'a>>) -> Element<'a> {
let animation = use_animation(cx, || 0.0);
let open = use_state(cx, || false);
let (node_ref, size) = use_node(cx);
let status = use_state(cx, AccordionStatus::default);
let platform = use_platform(cx);

let animation_value = animation.value();
let AccordionTheme { background, color } = theme.accordion;
let AccordionTheme {
background,
color,
border_fill,
} = theme.accordion;

// Adapt the accordion if the body size changes
use_memo(
Expand Down Expand Up @@ -58,16 +77,42 @@ pub fn Accordion<'a>(cx: Scope<'a, AccordionProps<'a>>) -> Element<'a> {
open.set(!*open.get());
};

use_on_unmount(cx, {
to_owned![status, platform];
move || {
if *status.get() == AccordionStatus::Hovering {
platform.set_cursor(CursorIcon::default());
}
}
});

let onmouseenter = {
to_owned![status, platform];
move |_| {
platform.set_cursor(CursorIcon::Hand);
status.set(AccordionStatus::Hovering);
}
};

let onmouseleave = move |_| {
platform.set_cursor(CursorIcon::default());
status.set(AccordionStatus::default());
};

render!(
rect {
onmouseenter: onmouseenter,
onmouseleave: onmouseleave,
overflow: "clip",
color: "{color}",
padding: "10",
corner_radius: "3",
margin: "2 4",
corner_radius: "6",
width: "100%",
height: "auto",
background: "{background}",
onclick: onclick,
border: "1 solid {border_fill}",
&cx.props.summary
rect {
overflow: "clip",
Expand Down
7 changes: 5 additions & 2 deletions crates/hooks/src/use_theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub struct ExternalLinkTheme {
pub struct AccordionTheme {
pub color: &'static str,
pub background: &'static str,
pub border_fill: &'static str,
}

/// Theming properties for Loader component.
Expand Down Expand Up @@ -221,7 +222,8 @@ pub const LIGHT_THEME: Theme = Theme {
},
accordion: AccordionTheme {
color: "black",
background: "rgb(215, 215, 215)",
background: "rgb(245, 245, 245)",
border_fill: "rgb(210, 210, 210)",
},
loader: LoaderTheme {
primary_color: "rgb(50, 50, 50)",
Expand Down Expand Up @@ -294,7 +296,8 @@ pub const DARK_THEME: Theme = Theme {
},
accordion: AccordionTheme {
color: "white",
background: "rgb(30, 30, 30)",
background: "rgb(60, 60, 60)",
border_fill: "rgb(80, 80, 80)",
},
loader: LoaderTheme {
primary_color: "rgb(150, 150, 150)",
Expand Down

0 comments on commit 59ef807

Please sign in to comment.