Skip to main content

euv_ui/component/code_block/view/
fn.rs

1use super::*;
2
3/// Holds a snippet of code.
4///
5/// The block is a column: the header names the snippet, the content
6/// holds the code and the footer holds whatever the snippet needs to
7/// say below it.
8///
9/// # Arguments
10///
11/// - `VirtualNode<EuvCodeBlockProps>` - The props node carrying the
12///   variant and the size of the block.
13///
14/// # Returns
15///
16/// - `VirtualNode` - The euv code block virtual DOM tree.
17#[component]
18pub fn euv_code_block(node: VirtualNode<EuvCodeBlockProps>) -> VirtualNode {
19    let EuvCodeBlockProps { variant, size }: EuvCodeBlockProps =
20        node.try_get_props().unwrap_or_default();
21    let variant_attr: &str = match variant {
22        EuvCodeBlockVariant::Default => CODE_BLOCK_VARIANT_DEFAULT,
23        EuvCodeBlockVariant::Destructive => "destructive",
24    };
25    let variant_class: fn() -> &'static Css = match variant {
26        EuvCodeBlockVariant::Default => c_euv_code_block_default,
27        EuvCodeBlockVariant::Destructive => c_euv_code_block_destructive,
28    };
29    let size_attr: &str = match size {
30        EuvCodeBlockSize::Default => CODE_BLOCK_SIZE_DEFAULT,
31        EuvCodeBlockSize::Sm => "sm",
32    };
33    let size_class: fn() -> &'static Css = match size {
34        EuvCodeBlockSize::Default => c_euv_code_block_size_default,
35        EuvCodeBlockSize::Sm => c_euv_code_block_size_sm,
36    };
37    let children: VirtualNode = node.get_children().into();
38    let base_class: &Css = c_euv_code_block();
39    html! {
40            div {
41                "data-slot": "code-block"
42                "data-variant": variant_attr
43                "data-size": size_attr
44                class: base_class
45                class: variant_class()
46                class: size_class()
47                children
48            }
49    }
50}
51
52/// Holds the header of a code block.
53///
54/// The header names the snippet and holds the actions that belong to
55/// it.
56///
57/// # Arguments
58///
59/// - `VirtualNode<EuvCodeBlockHeaderProps>` - The props node carrying
60///   the settings of the header.
61///
62/// # Returns
63///
64/// - `VirtualNode` - The euv code block header virtual DOM tree.
65#[component]
66pub fn euv_code_block_header(node: VirtualNode<EuvCodeBlockHeaderProps>) -> VirtualNode {
67    let EuvCodeBlockHeaderProps {}: EuvCodeBlockHeaderProps =
68        node.try_get_props().unwrap_or_default();
69    let children: VirtualNode = node.get_children().into();
70    let base_class: &Css = c_euv_code_block_header();
71    html! {
72            div {
73                "data-slot": "code-block-header"
74                class: base_class
75                children
76            }
77    }
78}
79
80/// Holds the name of the snippet.
81///
82/// The name is cut short with an ellipsis when it is too long, so the
83/// actions beside it stay reachable.
84///
85/// # Arguments
86///
87/// - `VirtualNode<EuvCodeBlockTitleProps>` - The props node carrying the
88///   settings of the title.
89///
90/// # Returns
91///
92/// - `VirtualNode` - The euv code block title virtual DOM tree.
93#[component]
94pub fn euv_code_block_title(node: VirtualNode<EuvCodeBlockTitleProps>) -> VirtualNode {
95    let EuvCodeBlockTitleProps {}: EuvCodeBlockTitleProps =
96        node.try_get_props().unwrap_or_default();
97    let children: VirtualNode = node.get_children().into();
98    html! {
99            div {
100                "data-slot": "code-block-title"
101                class: c_euv_code_block_title()
102                children
103            }
104    }
105}
106
107/// Holds the actions that belong to a code block.
108///
109/// The actions sit at the end of the header.
110///
111/// # Arguments
112///
113/// - `VirtualNode<EuvCodeBlockActionsProps>` - The props node carrying
114///   the settings of the actions.
115///
116/// # Returns
117///
118/// - `VirtualNode` - The euv code block actions virtual DOM tree.
119#[component]
120pub fn euv_code_block_actions(node: VirtualNode<EuvCodeBlockActionsProps>) -> VirtualNode {
121    let EuvCodeBlockActionsProps {}: EuvCodeBlockActionsProps =
122        node.try_get_props().unwrap_or_default();
123    let children: VirtualNode = node.get_children().into();
124    let base_class: &Css = c_euv_code_block_actions();
125    html! {
126            div {
127                "data-slot": "code-block-actions"
128                class: base_class
129                children
130            }
131    }
132}
133
134/// Puts the snippet on the clipboard.
135///
136/// The button writes the text it carries to the clipboard and says so
137/// on its own element: the confirmation lives in the `data-copied`
138/// attribute of the button, which is set on the element itself and
139/// taken away again after two seconds. The confirmation never travels
140/// through a signal, because a signal the page reads re-renders the
141/// page, and the re-render rewrites the button back to its resting
142/// state before the reader can see the confirmation.
143///
144/// # Arguments
145///
146/// - `VirtualNode<EuvCodeBlockCopyButtonProps>` - The props node carrying
147///   the text the button copies.
148///
149/// # Returns
150///
151/// - `VirtualNode` - The euv code block copy button virtual DOM tree.
152#[component]
153pub fn euv_code_block_copy_button(node: VirtualNode<EuvCodeBlockCopyButtonProps>) -> VirtualNode {
154    let EuvCodeBlockCopyButtonProps { value }: EuvCodeBlockCopyButtonProps =
155        node.try_get_props().unwrap_or_default();
156    let clipboard: UseClipboard = use_clipboard();
157    let copy: Option<Rc<dyn Fn(Event)>> = Some(Rc::new(move |event: Event| {
158        let started: bool = clipboard.copy(value);
159        let Some(target) = event.target() else {
160            return;
161        };
162        let Ok(button) = target.dyn_into::<Element>() else {
163            return;
164        };
165        set_copy_state(&button, started);
166        let button: Element = button.clone();
167        schedule_after(COPY_FEEDBACK_MILLIS, move || {
168            set_copy_state(&button, false);
169        });
170    }));
171    let children: VirtualNode = node.get_children().into();
172    html! {
173            button {
174                "data-slot": "code-block-copy-button"
175                "aria-label": "Copy"
176                type: "button"
177                class: c_euv_code_block_copy_button()
178                onclick: copy
179                children
180            }
181    }
182}
183
184/// Says on the button whether the text was copied.
185///
186/// The state lives in an attribute of the button, so it survives a
187/// re-render of the page and does not travel through a signal.
188///
189/// # Arguments
190///
191/// - `&Element` - The button the state is written on.
192/// - `bool` - Whether the text was copied.
193///
194/// # Returns
195///
196/// - `()` - The state is written on the element.
197fn set_copy_state(button: &Element, copied: bool) {
198    let attribute: &str = if copied { "true" } else { "false" };
199    let _: Result<(), JsValue> = button.set_attribute("data-copied", attribute);
200}
201
202/// Holds the code of a snippet.
203///
204/// The code is read from the left even in a right to left page, because
205/// code is not prose. The block scrolls sideways when the lines are
206/// long, unless the caller asks for them to wrap.
207///
208/// # Arguments
209///
210/// - `VirtualNode<EuvCodeBlockContentProps>` - The props node carrying
211///   the wrap and the numbering settings.
212///
213/// # Returns
214///
215/// - `VirtualNode` - The euv code block content virtual DOM tree.
216#[component]
217pub fn euv_code_block_content(node: VirtualNode<EuvCodeBlockContentProps>) -> VirtualNode {
218    let EuvCodeBlockContentProps {
219        wrap,
220        line_numbers,
221        start_line,
222    }: EuvCodeBlockContentProps = node.try_get_props().unwrap_or_default();
223    // The counter of the line numbers starts one below the first line,
224    // so the first line is numbered the way the caller asked for.
225    let counter_style: String = format!("counter-reset: line {};", start_line - 1);
226    let counter_ref: &str = counter_style.as_str();
227    let children: VirtualNode = node.get_children().into();
228    html! {
229        pre {
230            "data-slot": "code-block-content"
231            "data-wrap": if wrap {
232                "true"
233            } else {
234                "false"
235            }
236            "data-numbered": if line_numbers {
237                "true"
238            } else {
239                "false"
240            }
241            "dir": "ltr"
242            tabindex: "0"
243            style: counter_ref
244            class: c_euv_code_block_content()
245            class: if wrap {
246                c_euv_code_block_content_wrap()
247            } else {
248                c_euv_code_block_content_no_wrap()
249            }
250            code {
251                "data-slot": "code-block-code"
252                class: c_euv_code_block_code()
253                children
254            }
255        }
256    }
257}
258
259/// Holds one line of a snippet.
260///
261/// The line carries the state of the line inside the change the snippet
262/// shows.
263///
264/// # Arguments
265///
266/// - `VirtualNode<EuvCodeBlockLineProps>` - The props node carrying the
267///   variant of the line.
268///
269/// # Returns
270///
271/// - `VirtualNode` - The euv code block line virtual DOM tree.
272#[component]
273pub fn euv_code_block_line(node: VirtualNode<EuvCodeBlockLineProps>) -> VirtualNode {
274    let EuvCodeBlockLineProps { variant }: EuvCodeBlockLineProps =
275        node.try_get_props().unwrap_or_default();
276    let variant_attr: &str = match variant {
277        EuvCodeBlockLineVariant::Default => CODE_BLOCK_LINE_VARIANT_DEFAULT,
278        EuvCodeBlockLineVariant::Added => "added",
279        EuvCodeBlockLineVariant::Removed => "removed",
280        EuvCodeBlockLineVariant::Highlighted => "highlighted",
281    };
282    let variant_class: fn() -> &'static Css = match variant {
283        EuvCodeBlockLineVariant::Default => c_euv_code_block_line_default,
284        EuvCodeBlockLineVariant::Added => c_euv_code_block_line_added,
285        EuvCodeBlockLineVariant::Removed => c_euv_code_block_line_removed,
286        EuvCodeBlockLineVariant::Highlighted => c_euv_code_block_line_highlighted,
287    };
288    let children: VirtualNode = node.get_children().into();
289    html! {
290        span {
291            "data-slot": "code-block-line"
292            "data-variant": variant_attr
293            class: c_euv_code_block_line()
294            class: variant_class()
295            span {
296                "data-slot": "code-block-line-content"
297                class: c_euv_code_block_line_content()
298                children
299            }
300        }
301    }
302}
303
304/// Holds the footer of a code block.
305///
306/// # Arguments
307///
308/// - `VirtualNode<EuvCodeBlockFooterProps>` - The props node carrying the
309///   settings of the footer.
310///
311/// # Returns
312///
313/// - `VirtualNode` - The euv code block footer virtual DOM tree.
314#[component]
315pub fn euv_code_block_footer(node: VirtualNode<EuvCodeBlockFooterProps>) -> VirtualNode {
316    let EuvCodeBlockFooterProps {}: EuvCodeBlockFooterProps =
317        node.try_get_props().unwrap_or_default();
318    let children: VirtualNode = node.get_children().into();
319    let base_class: &Css = c_euv_code_block_footer();
320    html! {
321        div {
322            "data-slot": "code-block-footer"
323            class: base_class
324            children
325        }
326    }
327}