euv_ui/component/message/view/fn.rs
1use super::*;
2
3/// A toast stack pinned to the top centre of the viewport.
4///
5/// The component is purely presentational: it renders the messages it is
6/// given and reports the clicks through the item callbacks. It owns no
7/// queue and starts no timers, so the auto-dismiss timing stays with the
8/// caller — `use_timer` or `App::use_interval` — and the rendered tree
9/// stays deterministic and testable. The stack container is always
10/// mounted, even when `items` is empty, so a caller can push messages in
11/// without remounting the component.
12///
13/// # Arguments
14///
15/// - `VirtualNode<EuvMessageProps>` - The props node containing the messages.
16///
17/// # Returns
18///
19/// - `VirtualNode` - The message stack virtual DOM tree.
20#[component]
21pub fn euv_message(node: VirtualNode<EuvMessageProps>) -> VirtualNode {
22 let EuvMessageProps { items }: EuvMessageProps = node.try_get_props().unwrap_or_default();
23 let messages: Vec<VirtualNode> = items
24 .into_iter()
25 .enumerate()
26 .map(
27 |(index, item): (usize, message::view::r#struct::EuvMessageItem)| {
28 euv_message_item(index, item)
29 },
30 )
31 .collect();
32 html! {
33 div {
34 role: "status"
35 aria-live: "polite"
36 "data-slot": "message"
37 class: c_euv_message_stack()
38 messages
39 }
40 }
41}
42
43/// Renders one message: its text, the optional action and the optional close button.
44///
45/// # Arguments
46///
47/// - `usize` - The index of the message in the stack, used as the diff key.
48/// - `EuvMessageItem` - The message to render, consumed.
49///
50/// # Returns
51///
52/// - `VirtualNode` - The message virtual DOM tree.
53fn euv_message_item(index: usize, item: EuvMessageItem) -> VirtualNode {
54 let EuvMessageItem {
55 text,
56 action_label,
57 on_action,
58 on_close,
59 dismissible,
60 }: EuvMessageItem = item;
61 let has_action: bool = !action_label.is_empty();
62 let is_dismissible: bool = dismissible || on_close.is_some();
63 let action_handler: Option<Rc<dyn Fn(Event)>> = message_callback_handler(on_action);
64 let close_handler: Option<Rc<dyn Fn(Event)>> = message_callback_handler(on_close);
65 html! {
66 div {
67 key: index.to_string()
68 class: c_euv_message()
69 div {
70 class: c_euv_message_text()
71 text
72 }
73 if has_action {
74 button {
75 type: "button"
76 class: c_euv_message_action()
77 onclick: action_handler
78 action_label
79 }
80 }
81 if is_dismissible {
82 button {
83 type: "button"
84 class: c_euv_message_close()
85 onclick: close_handler
86 "×"
87 }
88 }
89 }
90 }
91}
92
93/// Builds the click handler that fires a caller's message callback.
94///
95/// The event type is a parameter so the DOM-free test suite can drive the
96/// handler with a stand-in event; the component always instantiates it
97/// with [`Event`].
98///
99/// # Arguments
100///
101/// - `Option<Rc<dyn Fn()>>` - The callback fired by the handler.
102///
103/// # Returns
104///
105/// - `Option<Rc<dyn Fn(E)>>` - The click handler, or `None` when there is no callback.
106pub fn message_callback_handler<E: 'static>(
107 callback: Option<Rc<dyn Fn()>>,
108) -> Option<Rc<dyn Fn(E)>> {
109 callback.map(|handler: Rc<dyn Fn()>| -> Rc<dyn Fn(E)> {
110 Rc::new(move |_: E| {
111 handler();
112 })
113 })
114}
115
116/// Holds a cluster of messages.
117///
118/// The group stacks the messages one under the other and keeps them
119/// from spilling out of the row they sit in.
120///
121/// # Arguments
122///
123/// - `VirtualNode<EuvMessageGroupProps>` - The props node carrying the
124/// settings of the group.
125///
126/// # Returns
127///
128/// - `VirtualNode` - The euv message group virtual DOM tree.
129#[component]
130pub fn euv_message_group(node: VirtualNode<EuvMessageGroupProps>) -> VirtualNode {
131 let EuvMessageGroupProps {}: EuvMessageGroupProps = node.try_get_props().unwrap_or_default();
132 let children: VirtualNode = node.get_children().into();
133 html! {
134 div {
135 "data-slot": "message-group"
136 class: c_euv_message_group()
137 children
138 }
139 }
140}
141
142/// Holds the head of one message.
143///
144/// The header holds the picture of the sender and the name beside it.
145///
146/// # Arguments
147///
148/// - `VirtualNode<EuvMessageHeaderProps>` - The props node carrying the
149/// settings of the header.
150///
151/// # Returns
152///
153/// - `VirtualNode` - The euv message header virtual DOM tree.
154#[component]
155pub fn euv_message_header(node: VirtualNode<EuvMessageHeaderProps>) -> VirtualNode {
156 let EuvMessageHeaderProps {}: EuvMessageHeaderProps = node.try_get_props().unwrap_or_default();
157 let children: VirtualNode = node.get_children().into();
158 html! {
159 div {
160 "data-slot": "message-header"
161 class: c_euv_message_header()
162 children
163 }
164 }
165}
166
167/// Holds the picture of the sender of one message.
168///
169/// The avatar is a round frame the picture is cut to, so the messages
170/// of one conversation line up with one another.
171///
172/// # Arguments
173///
174/// - `VirtualNode<EuvMessageAvatarProps>` - The props node carrying the
175/// settings of the avatar.
176///
177/// # Returns
178///
179/// - `VirtualNode` - The euv message avatar virtual DOM tree.
180#[component]
181pub fn euv_message_avatar(node: VirtualNode<EuvMessageAvatarProps>) -> VirtualNode {
182 let EuvMessageAvatarProps {}: EuvMessageAvatarProps = node.try_get_props().unwrap_or_default();
183 let children: VirtualNode = node.get_children().into();
184 html! {
185 div {
186 "data-slot": "message-avatar"
187 class: c_euv_message_avatar()
188 children
189 }
190 }
191}
192
193/// Holds the body of one message.
194///
195/// The content holds the words of the message and takes the width the
196/// row gives it, so a long message wraps instead of pushing the row
197/// wider. The words sit in the bubble, which is the surface the
198/// reference draws them on.
199///
200/// # Arguments
201///
202/// - `VirtualNode<EuvMessageContentProps>` - The props node carrying the
203/// settings of the content.
204///
205/// # Returns
206///
207/// - `VirtualNode` - The euv message content virtual DOM tree.
208#[component]
209pub fn euv_message_content(node: VirtualNode<EuvMessageContentProps>) -> VirtualNode {
210 let EuvMessageContentProps {}: EuvMessageContentProps =
211 node.try_get_props().unwrap_or_default();
212 let children: VirtualNode = node.get_children().into();
213 html! {
214 div {
215 "data-slot": "message-content"
216 class: c_euv_message_content()
217 euv_message_bubble {
218 children
219 }
220 }
221 }
222}
223
224/// The surface the words of one message sit on.
225///
226/// The bubble is the `bubble` part of the reference: it wraps the words
227/// of the message so they read as one block of speech.
228///
229/// # Arguments
230///
231/// - `VirtualNode<EuvMessageBubbleProps>` - The props node carrying the
232/// settings of the bubble.
233///
234/// # Returns
235///
236/// - `VirtualNode` - The euv message bubble virtual DOM tree.
237#[component]
238pub fn euv_message_bubble(node: VirtualNode<EuvMessageBubbleProps>) -> VirtualNode {
239 let EuvMessageBubbleProps {}: EuvMessageBubbleProps = node.try_get_props().unwrap_or_default();
240 let children: VirtualNode = node.get_children().into();
241 html! {
242 div {
243 "data-slot": "bubble"
244 class: c_euv_message_bubble()
245 children
246 }
247 }
248}
249
250/// Holds the trailing actions of one message.
251///
252/// The footer holds what the reader can do with the message, so the
253/// actions sit below the words they belong to.
254///
255/// # Arguments
256///
257/// - `VirtualNode<EuvMessageFooterProps>` - The props node carrying the
258/// settings of the footer.
259///
260/// # Returns
261///
262/// - `VirtualNode` - The euv message footer virtual DOM tree.
263#[component]
264pub fn euv_message_footer(node: VirtualNode<EuvMessageFooterProps>) -> VirtualNode {
265 let EuvMessageFooterProps {}: EuvMessageFooterProps = node.try_get_props().unwrap_or_default();
266 let children: VirtualNode = node.get_children().into();
267 html! {
268 div {
269 "data-slot": "message-footer"
270 class: c_euv_message_footer()
271 children
272 }
273 }
274}