1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use freya_animation::prelude::{
AnimNum,
Ease,
Function,
use_animation,
};
use freya_core::prelude::*;
use torin::{
gaps::Gaps,
prelude::VisibleSize,
};
use crate::{
get_theme,
theming::component_themes::AccordionThemePartial,
};
/// A container that expands/collapses vertically when pressed.
///
/// # Example
///
/// ```rust
/// # use freya::prelude::*;
/// const LOREM_IPSUM: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
///
/// fn app() -> impl IntoElement {
/// rect()
/// .center()
/// .expanded()
/// .spacing(4.)
/// .children_iter((0..2).map(|_| {
/// Accordion::new()
/// .header("Click to expand!")
/// .child(LOREM_IPSUM)
/// .into()
/// }))
/// }
///
/// # use freya_testing::prelude::*;
/// # use std::time::Duration;
/// # launch_doc_hook(|| {
/// # rect().child(app())
/// # }, (250., 250.).into(), "./images/gallery_accordion.png", |t| {
/// # t.click_cursor((125., 115.));
/// # t.poll(Duration::from_millis(1), Duration::from_millis(300));
/// # t.sync_and_update();
/// # });
/// ```
///
/// # Preview
/// ![Accordion Preview][accordion]
#[cfg_attr(feature = "docs",
doc = embed_doc_image::embed_image!("accordion", "images/gallery_accordion.png")
)]
#[derive(Clone, PartialEq, Default)]
pub struct Accordion {
pub(crate) theme: Option<AccordionThemePartial>,
header: Option<Element>,
children: Vec<Element>,
key: DiffKey,
}
impl KeyExt for Accordion {
fn write_key(&mut self) -> &mut DiffKey {
&mut self.key
}
}
impl Accordion {
pub fn new() -> Self {
Self::default()
}
pub fn header<C: Into<Element>>(mut self, header: C) -> Self {
self.header = Some(header.into());
self
}
}
impl ChildrenExt for Accordion {
fn get_children(&mut self) -> &mut Vec<Element> {
&mut self.children
}
}
impl Render for Accordion {
fn render(self: &Accordion) -> impl IntoElement {
let header = use_focus();
let accordion_theme = get_theme!(&self.theme, accordion);
let mut open = use_state(|| false);
let mut animation = use_animation(move |_conf| {
AnimNum::new(0., 100.)
.time(300)
.function(Function::Expo)
.ease(Ease::Out)
});
let clip_percent = animation.get().value();
rect()
.a11y_id(header.a11y_id())
.a11y_role(AccessibilityRole::Header)
.a11y_focusable(true)
.corner_radius(CornerRadius::new_all(8.))
.padding(Gaps::new_all(8.))
.color(accordion_theme.color)
.background(accordion_theme.background)
.border(
Border::new()
.fill(accordion_theme.border_fill)
.width(1.)
.alignment(BorderAlignment::Inner),
)
.on_pointer_enter(move |_| {
Cursor::set(CursorIcon::Pointer);
})
.on_pointer_leave(move |_| {
Cursor::set(CursorIcon::default());
})
.on_press(move |_| {
if open.toggled() {
animation.start();
} else {
animation.reverse();
}
})
.maybe_child(self.header.clone())
.child(
rect()
.a11y_role(AccessibilityRole::Region)
.a11y_builder(|b| {
b.set_labelled_by([header.a11y_id()]);
if !open() {
b.set_hidden();
}
})
.overflow(Overflow::Clip)
.visible_height(VisibleSize::inner_percent(clip_percent))
.children(self.children.clone()),
)
}
fn render_key(&self) -> DiffKey {
self.key.clone().or(self.default_key())
}
}