use std::rc::Rc;
use dioxus::prelude::*;
use crate::class::{Glass, compose};
use crate::overlay::{focus_element, is_escape};
use super::roving::{Roving, RovingAxis, roving_move};
use super::util::element_text;
pub fn command_matches(text: &str, query: &str) -> bool {
let query = query.trim().to_lowercase();
query.is_empty() || text.to_lowercase().contains(&query)
}
#[derive(Clone, Copy)]
struct CommandContext {
query: Signal<String>,
roving: Roving,
input: Signal<Option<Rc<MountedData>>>,
}
fn use_command(component: &str) -> CommandContext {
try_use_context::<CommandContext>()
.unwrap_or_else(|| panic!("{component} must be used inside a Lumen Command."))
}
#[derive(Props, Clone, PartialEq)]
pub struct CommandProps {
#[props(default, into)]
pub glass: Glass,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn Command(props: CommandProps) -> Element {
let query = use_signal(String::new);
let input = use_signal(|| None::<Rc<MountedData>>);
let roving = Roving::use_new();
use_context_provider(|| CommandContext {
query,
roving,
input,
});
let glass = props.glass.class("ui-command");
let class = compose([Some("ui-command"), glass.as_deref(), props.class.as_deref()]);
rsx! {
div { class, "data-ui-command": "true", ..props.attributes, {props.children} }
}
}
#[derive(Props, Clone, PartialEq)]
pub struct CommandInputProps {
pub on_value_change: Option<EventHandler<String>>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = input)]
pub attributes: Vec<Attribute>,
}
#[component]
pub fn CommandInput(props: CommandInputProps) -> Element {
let CommandContext {
mut query,
roving,
mut input,
} = use_command("CommandInput");
let class = compose([Some("ui-input"), props.class.as_deref()]);
let on_value_change = props.on_value_change;
rsx! {
input {
class,
r#type: "search",
oninput: move |event| {
let value = event.value();
query.set(value.clone());
if let Some(handler) = on_value_change {
handler.call(value);
}
},
onkeydown: move |event| {
let last = match event.key() {
Key::ArrowDown => false,
Key::ArrowUp => true,
_ => return,
};
event.prevent_default();
roving.focus_edge(last);
},
onmounted: move |event| input.set(Some(event.data())),
..props.attributes,
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct CommandListProps {
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn CommandList(props: CommandListProps) -> Element {
rsx! {
div { class: props.class, "data-slot": "command-list", ..props.attributes, {props.children} }
}
}
#[derive(Props, Clone, PartialEq)]
pub struct CommandItemProps {
#[props(into)]
pub value: Option<String>,
#[props(default)]
pub disabled: bool,
pub onclick: Option<EventHandler<MouseEvent>>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = button)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn CommandItem(props: CommandItemProps) -> Element {
let CommandContext {
query,
roving,
input,
} = use_command("CommandItem");
let text = props
.value
.clone()
.unwrap_or_else(|| element_text(&props.children));
let hidden = !command_matches(&text, &query.read());
let key = roving.use_item(props.disabled, hidden);
let onclick = props.onclick;
rsx! {
button {
class: props.class,
"data-ui-command-item": "true",
disabled: props.disabled,
hidden,
r#type: "button",
onclick: move |event| {
if let Some(handler) = onclick {
handler.call(event);
}
},
onfocus: move |_| roving.focused(key),
onkeydown: move |event| {
if is_escape(&event) {
event.prevent_default();
focus_element(input());
return;
}
if let Some(movement) = roving_move(&event.key(), RovingAxis::Vertical, false) {
event.prevent_default();
roving.move_focus(Some(key), movement);
}
},
onmounted: move |event| roving.mount(key, event.data()),
..props.attributes,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct CommandEmptyProps {
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = p)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn CommandEmpty(props: CommandEmptyProps) -> Element {
let CommandContext { query, roving, .. } = use_command("CommandEmpty");
let searching = !query.read().trim().is_empty();
let hidden = !(searching && roving.visible_count() == 0);
rsx! {
p {
class: props.class,
"data-slot": "command-empty",
hidden,
role: "status",
..props.attributes,
{props.children}
}
}
}
#[cfg(test)]
mod tests {
use super::command_matches;
#[test]
fn matching_is_trimmed_and_case_insensitive() {
assert!(command_matches("Open documentation", ""));
assert!(command_matches("Open documentation", " DOC "));
assert!(!command_matches("Toggle theme", "doc"));
assert!(command_matches("Ünïcode", "ünï"));
assert!(!command_matches("short", &"x".repeat(10_000)));
}
}