Skip to main content

euv_ui/component/command/view/
fn.rs

1use super::*;
2
3/// The frame that holds a command.
4///
5/// The frame is a column: the field sits at its top, the rows scroll
6/// below it. It is the surface the rows are read against, so it carries
7/// the popover colours of the design system.
8///
9/// # Arguments
10///
11/// - `VirtualNode<EuvCommandProps>` - The props node carrying the component settings.
12///
13/// # Returns
14///
15/// - `VirtualNode` - The euv command virtual DOM tree.
16#[component]
17pub fn euv_command(node: VirtualNode<EuvCommandProps>) -> VirtualNode {
18    let EuvCommandProps {
19        value: _value,
20        label,
21    }: EuvCommandProps = node.try_get_props().unwrap_or_default();
22    let children: VirtualNode = node.get_children().into();
23    let base_class: &Css = c_euv_command();
24    html! {
25            div {
26                role: "group"
27                "aria-label": label
28                class: base_class
29                "data-slot": "command"
30                children
31            }
32    }
33}
34
35/// The field the reader types the query into.
36///
37/// The field is wrapped in a row that carries the rule under it, which
38/// is what separates the field from the rows below.
39///
40/// # Arguments
41///
42/// - `VirtualNode<EuvCommandInputProps>` - The props node carrying the component settings.
43///
44/// # Returns
45///
46/// - `VirtualNode` - The euv command input virtual DOM tree.
47#[component]
48pub fn euv_command_input(node: VirtualNode<EuvCommandInputProps>) -> VirtualNode {
49    let EuvCommandInputProps {
50        value,
51        placeholder,
52        disabled,
53    }: EuvCommandInputProps = node.try_get_props().unwrap_or_default();
54    let disabled_signal: Signal<bool> = Signal::create(disabled);
55    let text: String = value.get();
56    html! {
57            div {
58                class: c_euv_command_input_wrapper()
59                "data-slot": "command-input-wrapper"
60                input {
61                    type: "text"
62                    role: "combobox"
63                    "aria-expanded": "true"
64                    value: text
65                    placeholder: placeholder
66                    disabled: disabled_signal
67                    class: c_euv_command_input()
68                    "data-slot": "command-input"
69                    oninput: on_command_input(value)
70                }
71            }
72    }
73}
74
75/// Builds the handler that keeps the query in the caller's signal.
76///
77/// # Arguments
78///
79/// - `Signal<String>` - The signal holding the query.
80///
81/// # Returns
82///
83/// - `Option<Rc<dyn Fn(Event)>>` - The input handler.
84fn on_command_input(value: Signal<String>) -> Option<Rc<dyn Fn(Event)>> {
85    Some(Rc::new(move |event: Event| {
86        let Some(target) = event.target() else {
87            return;
88        };
89        let Ok(input) = target.dyn_into::<HtmlInputElement>() else {
90            return;
91        };
92        value.set(input.value());
93    }))
94}
95
96/// The rows of the command.
97///
98/// The list is the listbox the rows live in: it carries the role, and
99/// every row inside it is one of its options.
100///
101/// # Arguments
102///
103/// - `VirtualNode<EuvCommandListProps>` - The props node carrying the component settings.
104///
105/// # Returns
106///
107/// - `VirtualNode` - The euv command list virtual DOM tree.
108#[component]
109pub fn euv_command_list(node: VirtualNode<EuvCommandListProps>) -> VirtualNode {
110    let EuvCommandListProps { label }: EuvCommandListProps =
111        node.try_get_props().unwrap_or_default();
112    let children: VirtualNode = node.get_children().into();
113    html! {
114        div {
115            role: "listbox"
116            "aria-label": label
117            class: c_euv_command_list()
118            "data-slot": "command-list"
119            children
120        }
121    }
122}
123
124/// The message shown when the query matched nothing.
125///
126/// # Arguments
127///
128/// - `VirtualNode<EuvCommandEmptyProps>` - The props node carrying the component settings.
129///
130/// # Returns
131///
132/// - `VirtualNode` - The euv command empty virtual DOM tree.
133#[component]
134pub fn euv_command_empty(node: VirtualNode<EuvCommandEmptyProps>) -> VirtualNode {
135    let EuvCommandEmptyProps { text }: EuvCommandEmptyProps =
136        node.try_get_props().unwrap_or_default();
137    html! {
138        div {
139            class: c_euv_command_empty()
140            "data-slot": "command-empty"
141            {
142                text
143            }
144        }
145    }
146}
147
148/// A named group of rows.
149///
150/// # Arguments
151///
152/// - `VirtualNode<EuvCommandGroupProps>` - The props node carrying the component settings.
153///
154/// # Returns
155///
156/// - `VirtualNode` - The euv command group virtual DOM tree.
157#[component]
158pub fn euv_command_group(node: VirtualNode<EuvCommandGroupProps>) -> VirtualNode {
159    let EuvCommandGroupProps { heading }: EuvCommandGroupProps =
160        node.try_get_props().unwrap_or_default();
161    let children: VirtualNode = node.get_children().into();
162    html! {
163        div {
164            role: "group"
165            class: c_euv_command_group()
166            "data-slot": "command-group"
167            div {
168                class: c_euv_command_group_heading()
169                {
170                    heading
171                }
172            }
173            children
174        }
175    }
176}
177
178/// One row of the command.
179///
180/// The row is an option of the listbox, so the screen reader reads it as
181/// one of the choices the query narrows down.
182///
183/// # Arguments
184///
185/// - `VirtualNode<EuvCommandItemProps>` - The props node carrying the component settings.
186///
187/// # Returns
188///
189/// - `VirtualNode` - The euv command item virtual DOM tree.
190#[component]
191pub fn euv_command_item(node: VirtualNode<EuvCommandItemProps>) -> VirtualNode {
192    let EuvCommandItemProps {
193        value,
194        label,
195        selected,
196        disabled,
197        on_select,
198    }: EuvCommandItemProps = node.try_get_props().unwrap_or_default();
199    let children: VirtualNode = node.get_children().into();
200    let item_class: fn() -> &'static Css = if disabled {
201        c_euv_command_item_disabled
202    } else if selected {
203        c_euv_command_item_selected
204    } else {
205        c_euv_command_item
206    };
207    let on_click: Option<Rc<dyn Fn(Event)>> = if disabled {
208        None
209    } else {
210        Some(Rc::new(move |_: Event| {
211            if let Some(callback) = &on_select {
212                callback(value);
213            }
214        }))
215    };
216    html! {
217        div {
218            role: "option"
219            "aria-selected": if selected { "true" } else { "false" }
220            "aria-disabled": if disabled { "true" } else { "false" }
221            class: command_item_class(item_class)
222            "data-slot": "command-item"
223            onclick: on_click
224            { label }
225            children
226        }
227    }
228}
229
230/// The rule that divides one group of rows from the next.
231///
232/// # Arguments
233///
234/// - `VirtualNode<EuvCommandSeparatorProps>` - The props node carrying the component settings.
235///
236/// # Returns
237///
238/// - `VirtualNode` - The euv command separator virtual DOM tree.
239#[component]
240pub fn euv_command_separator(node: VirtualNode<EuvCommandSeparatorProps>) -> VirtualNode {
241    let EuvCommandSeparatorProps { label }: EuvCommandSeparatorProps =
242        node.try_get_props().unwrap_or_default();
243    html! {
244        div {
245            role: "separator"
246            class: c_euv_command_separator()
247            "data-slot": "command-separator"
248            "aria-label": label
249        }
250    }
251}
252
253/// The key hint shown at the trailing edge of a row.
254///
255/// # Arguments
256///
257/// - `VirtualNode<EuvCommandShortcutProps>` - The props node carrying the component settings.
258///
259/// # Returns
260///
261/// - `VirtualNode` - The euv command shortcut virtual DOM tree.
262#[component]
263pub fn euv_command_shortcut(node: VirtualNode<EuvCommandShortcutProps>) -> VirtualNode {
264    let EuvCommandShortcutProps { text }: EuvCommandShortcutProps =
265        node.try_get_props().unwrap_or_default();
266    html! {
267        span {
268            class: c_euv_command_shortcut()
269            "data-slot": "command-shortcut"
270            {
271                text
272            }
273        }
274    }
275}
276
277/// Builds the class list of a command row.
278///
279/// The base class is resolved first and the theme variant second, so the
280/// base's rule is injected before the variant's and the variant wins the
281/// cascade. A class injects its stylesheet on its first call, so the
282/// order the two are resolved in is the order their rules land in.
283///
284/// # Arguments
285///
286/// - `fn() -> &'static Css` - The state class of the row.
287///
288/// # Returns
289///
290/// - `String` - The class list.
291fn command_item_class(state_class: fn() -> &'static Css) -> String {
292    let base_class: &str = c_euv_command_item().get_name();
293    let mut class_name: String = String::with_capacity(64);
294    class_name.push_str(base_class);
295    class_name.push(' ');
296    class_name.push_str(state_class().get_name());
297    class_name
298}