euv_ui/component/notification/view/fn.rs
1use super::*;
2
3/// A notification stack pinned to the top right of the viewport.
4///
5/// The component is purely presentational: it renders the notifications it
6/// is 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 notifications
11/// in without remounting the component.
12///
13/// # Arguments
14///
15/// - `VirtualNode<EuvNotificationProps>` - The props node containing the notifications.
16///
17/// # Returns
18///
19/// - `VirtualNode` - The notification stack virtual DOM tree.
20#[component]
21pub fn euv_notification(node: VirtualNode<EuvNotificationProps>) -> VirtualNode {
22 let EuvNotificationProps { items }: EuvNotificationProps =
23 node.try_get_props().unwrap_or_default();
24 let notifications: Vec<VirtualNode> = items
25 .into_iter()
26 .enumerate()
27 .map(
28 |(index, item): (usize, notification::view::r#struct::EuvNotificationItem)| {
29 euv_notification_item(index, item)
30 },
31 )
32 .collect();
33 html! {
34 div {
35 role: "status"
36 aria-live: "polite"
37 class: c_euv_notification_stack()
38 notifications
39 }
40 }
41}
42
43/// Renders one notification: its body and the optional close button.
44///
45/// # Arguments
46///
47/// - `usize` - The index of the notification in the stack, used as the diff key.
48/// - `EuvNotificationItem` - The notification to render, consumed.
49///
50/// # Returns
51///
52/// - `VirtualNode` - The notification virtual DOM tree.
53fn euv_notification_item(index: usize, item: EuvNotificationItem) -> VirtualNode {
54 let EuvNotificationItem {
55 title,
56 text,
57 on_close,
58 dismissible,
59 }: EuvNotificationItem = item;
60 let is_dismissible: bool = dismissible || on_close.is_some();
61 let close_handler: Option<Rc<dyn Fn(Event)>> = notification_callback_handler(on_close);
62 html! {
63 div {
64 key: index.to_string()
65 class: c_euv_notification()
66 div {
67 class: c_euv_notification_body()
68 div {
69 class: c_euv_notification_title()
70 title
71 }
72 div {
73 class: c_euv_notification_text()
74 text
75 }
76 }
77 if is_dismissible {
78 button {
79 type: "button"
80 class: c_euv_notification_close()
81 onclick: close_handler
82 "×"
83 }
84 }
85 }
86 }
87}
88
89/// Builds the click handler that fires a caller's notification callback.
90///
91/// The event type is a parameter so the DOM-free test suite can drive the
92/// handler with a stand-in event; the component always instantiates it
93/// with [`Event`].
94///
95/// # Arguments
96///
97/// - `Option<Rc<dyn Fn()>>` - The callback fired by the handler.
98///
99/// # Returns
100///
101/// - `Option<Rc<dyn Fn(E)>>` - The click handler, or `None` when there is no callback.
102pub fn notification_callback_handler<E: 'static>(
103 callback: Option<Rc<dyn Fn()>>,
104) -> Option<Rc<dyn Fn(E)>> {
105 callback.map(|handler: Rc<dyn Fn()>| -> Rc<dyn Fn(E)> {
106 Rc::new(move |_: E| {
107 handler();
108 })
109 })
110}
111
112/// A toast card.
113///
114/// The card carries `role="status"` and `aria-live="polite"`, so a toast
115/// that arrives while the reader is somewhere else is announced without
116/// interrupting what they are doing.
117///
118/// # Arguments
119///
120/// - `VirtualNode<EuvToastProps>` - The props node carrying the component settings.
121///
122/// # Returns
123///
124/// - `VirtualNode` - The euv toast virtual DOM tree.
125#[component]
126pub fn euv_toast(node: VirtualNode<EuvToastProps>) -> VirtualNode {
127 let EuvToastProps {
128 variant,
129 title,
130 description,
131 action_label,
132 dismissible,
133 on_action,
134 on_close,
135 }: EuvToastProps = node.try_get_props().unwrap_or_default();
136 let toast_class: fn() -> &'static Css = match variant {
137 EuvToastVariant::Destructive => c_euv_toast_destructive,
138 EuvToastVariant::Default => c_euv_toast,
139 };
140 let mut children: Vec<VirtualNode> = Vec::new();
141 if !title.is_empty() {
142 children.push(html! {
143 div {
144 "data-slot": "toast-title"
145 class: c_euv_toast_title()
146 {
147 title
148 }
149 }
150 });
151 }
152 if !description.is_empty() {
153 children.push(html! {
154 div {
155 "data-slot": "toast-description"
156 class: c_euv_toast_description()
157 {
158 description
159 }
160 }
161 });
162 }
163 if !action_label.is_empty() {
164 let on_click: Option<Rc<dyn Fn(Event)>> = Some(Rc::new(move |_: Event| {
165 if let Some(callback) = &on_action {
166 callback();
167 }
168 }));
169 children.push(html! {
170 button {
171 type: "button"
172 "data-slot": "toast-action"
173 class: c_euv_toast_action()
174 onclick: on_click
175 {
176 action_label
177 }
178 }
179 });
180 }
181 if dismissible || on_close.is_some() {
182 let on_click: Option<Rc<dyn Fn(Event)>> = Some(Rc::new(move |_: Event| {
183 if let Some(callback) = &on_close {
184 callback();
185 }
186 }));
187 children.push(html! {
188 button {
189 type: "button"
190 "data-slot": "toast-close"
191 class: c_euv_toast_close()
192 "aria-label": "Dismiss"
193 onclick: on_click
194 }
195 });
196 }
197 html! {
198 div {
199 role: "status"
200 "aria-live": "polite"
201 "data-slot": "toast"
202 class: toast_class()
203 children
204 }
205 }
206}
207
208/// The shell a toast is built from.
209///
210/// The reference lets the caller compose a toast out of a headline, a
211/// body line, an action and a close button. This component is the box
212/// those parts live in: it carries the `toast` slot and the card
213/// styling, and it keeps whatever the caller puts inside it.
214///
215/// # Arguments
216///
217/// - `VirtualNode<EuvToastCardProps>` - The props node carrying the component settings.
218///
219/// # Returns
220///
221/// - `VirtualNode` - The euv toast card virtual DOM tree.
222#[component]
223pub fn euv_toast_card(node: VirtualNode<EuvToastCardProps>) -> VirtualNode {
224 let EuvToastCardProps {}: EuvToastCardProps = node.try_get_props().unwrap_or_default();
225 let children: VirtualNode = node.get_children().into();
226 html! {
227 div {
228 role: "status"
229 "aria-live": "polite"
230 "data-slot": "toast"
231 class: c_euv_toast()
232 children
233 }
234 }
235}
236
237/// The headline of a toast.
238///
239/// # Arguments
240///
241/// - `VirtualNode<EuvToastTitleProps>` - The props node carrying the component settings.
242///
243/// # Returns
244///
245/// - `VirtualNode` - The euv toast title virtual DOM tree.
246#[component]
247pub fn euv_toast_title(node: VirtualNode<EuvToastTitleProps>) -> VirtualNode {
248 let EuvToastTitleProps { text }: EuvToastTitleProps = node.try_get_props().unwrap_or_default();
249 html! {
250 div {
251 "data-slot": "toast-title"
252 class: c_euv_toast_title()
253 {
254 text
255 }
256 }
257 }
258}
259
260/// The body line of a toast.
261///
262/// # Arguments
263///
264/// - `VirtualNode<EuvToastDescriptionProps>` - The props node carrying the component settings.
265///
266/// # Returns
267///
268/// - `VirtualNode` - The euv toast description virtual DOM tree.
269#[component]
270pub fn euv_toast_description(node: VirtualNode<EuvToastDescriptionProps>) -> VirtualNode {
271 let EuvToastDescriptionProps { text }: EuvToastDescriptionProps =
272 node.try_get_props().unwrap_or_default();
273 html! {
274 div {
275 "data-slot": "toast-description"
276 class: c_euv_toast_description()
277 {
278 text
279 }
280 }
281 }
282}
283
284/// The action button of a toast.
285///
286/// # Arguments
287///
288/// - `VirtualNode<EuvToastActionProps>` - The props node carrying the component settings.
289///
290/// # Returns
291///
292/// - `VirtualNode` - The euv toast action virtual DOM tree.
293#[component]
294pub fn euv_toast_action(node: VirtualNode<EuvToastActionProps>) -> VirtualNode {
295 let EuvToastActionProps { label, on_click }: EuvToastActionProps =
296 node.try_get_props().unwrap_or_default();
297 let handler: Option<Rc<dyn Fn(Event)>> = Some(Rc::new(move |_: Event| {
298 if let Some(callback) = &on_click {
299 callback();
300 }
301 }));
302 html! {
303 button {
304 type: "button"
305 "data-slot": "toast-action"
306 class: c_euv_toast_action()
307 onclick: handler
308 {
309 label
310 }
311 }
312 }
313}
314
315/// The provider that owns the queue of the toasts.
316///
317/// The reference hands the queue to a provider that sits above the
318/// viewport and hands every toast to it. euv has no context, so the
319/// provider renders a wrapper that keeps its children in the flow of the
320/// page and leaves the queue to the caller.
321///
322/// # Arguments
323///
324/// - `VirtualNode<EuvToastProviderProps>` - The props node carrying the component settings.
325///
326/// # Returns
327///
328/// - `VirtualNode` - The euv toast provider virtual DOM tree.
329#[component]
330pub fn euv_toast_provider(node: VirtualNode<EuvToastProviderProps>) -> VirtualNode {
331 let EuvToastProviderProps {}: EuvToastProviderProps = node.try_get_props().unwrap_or_default();
332 let children: VirtualNode = node.get_children().into();
333 html! {
334 div {
335 "data-slot": "toast-provider"
336 class: c_euv_toast_provider()
337 children
338 }
339 }
340}
341
342/// The fixed box the toasts stack inside.
343///
344/// The viewport is the anchor the toasts are placed in: it is pinned to
345/// the edge of the viewport and the toasts inside it stack downwards.
346///
347/// # Arguments
348///
349/// - `VirtualNode<EuvToastViewportProps>` - The props node carrying the component settings.
350///
351/// # Returns
352///
353/// - `VirtualNode` - The euv toast viewport virtual DOM tree.
354#[component]
355pub fn euv_toast_viewport(node: VirtualNode<EuvToastViewportProps>) -> VirtualNode {
356 let EuvToastViewportProps {}: EuvToastViewportProps = node.try_get_props().unwrap_or_default();
357 let children: VirtualNode = node.get_children().into();
358 html! {
359 div {
360 "data-slot": "toast-viewport"
361 class: c_euv_toast_viewport()
362 children
363 }
364 }
365}
366
367/// The button that dismisses one toast.
368///
369/// The close button reports the click to the caller, which owns the
370/// queue and decides what dismissing a toast means.
371///
372/// # Arguments
373///
374/// - `VirtualNode<EuvToastCloseProps>` - The props node carrying the component settings.
375///
376/// # Returns
377///
378/// - `VirtualNode` - The euv toast close virtual DOM tree.
379#[component]
380pub fn euv_toast_close(node: VirtualNode<EuvToastCloseProps>) -> VirtualNode {
381 let EuvToastCloseProps { on_click }: EuvToastCloseProps =
382 node.try_get_props().unwrap_or_default();
383 let handler: Option<Rc<dyn Fn(Event)>> = Some(Rc::new(move |_: Event| {
384 if let Some(callback) = &on_click {
385 callback();
386 }
387 }));
388 let children: VirtualNode = node.get_children().into();
389 html! {
390 button {
391 type: "button"
392 "data-slot": "toast-close"
393 "aria-label": "Dismiss"
394 class: c_euv_toast_close()
395 onclick: handler
396 children
397 }
398 }
399}