use dioxus::prelude::*;
use crate::class::{Glass, compose};
use crate::hooks::use_controllable;
use super::chrome::{NavigationVariant, surface};
use super::roving::{Roving, RovingAxis, roving_move};
#[derive(Clone, Copy)]
struct MenubarContext(Roving);
#[derive(Clone, Copy)]
struct NavigationMenuContext(Roving);
#[derive(Clone, Copy)]
struct ToggleGroupContext(Roving);
fn handle_roving_key(roving: Roving, key: usize, event: &KeyboardEvent) -> bool {
let modifiers = event.modifiers();
if event.is_composing() || modifiers.alt() || modifiers.ctrl() || modifiers.meta() {
return false;
}
let Some(movement) = roving_move(&event.key(), RovingAxis::Both, false) else {
return false;
};
event.prevent_default();
roving.move_focus(Some(key), movement).is_some()
}
#[derive(Props, Clone, PartialEq)]
pub struct MenubarProps {
#[props(default, into)]
pub glass: Glass,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = nav)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn Menubar(props: MenubarProps) -> Element {
let roving = Roving::use_new();
use_context_provider(|| MenubarContext(roving));
let glass = props.glass.class("ui-menubar");
let class = compose([Some("ui-menubar"), glass.as_deref(), props.class.as_deref()]);
rsx! {
nav {
class,
"data-surface": surface(props.glass),
"data-ui-menubar": "true",
..props.attributes,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct MenubarItemProps {
#[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 MenubarItem(props: MenubarItemProps) -> Element {
let MenubarContext(roving) = try_use_context::<MenubarContext>()
.unwrap_or_else(|| panic!("MenubarItem must be used inside a Lumen Menubar."));
let key = roving.use_item(props.disabled, false);
let onclick = props.onclick;
rsx! {
button {
class: props.class,
disabled: props.disabled,
role: "menuitem",
tabindex: roving.tabindex(key),
r#type: "button",
onclick: move |event| {
if let Some(handler) = onclick {
handler.call(event);
}
},
onfocus: move |_| roving.focused(key),
onkeydown: move |event| {
handle_roving_key(roving, key, &event);
},
onmounted: move |event| roving.mount(key, event.data()),
..props.attributes,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct NavigationMenuProps {
#[props(default)]
pub variant: NavigationVariant,
#[props(default, into)]
pub glass: Glass,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = nav)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn NavigationMenu(props: NavigationMenuProps) -> Element {
let roving = Roving::use_new();
use_context_provider(|| NavigationMenuContext(roving));
let unstyled = props.variant.modifier("ui-navigation-menu");
let glass = props.glass.class("ui-navigation-menu");
let class = compose([
Some("ui-navigation-menu"),
unstyled.as_deref(),
glass.as_deref(),
props.class.as_deref(),
]);
rsx! {
nav {
class,
"data-slot": "navigation-menu",
"data-surface": surface(props.glass),
"data-ui-navigation-menu": "true",
"data-variant": props.variant.as_str(),
..props.attributes,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct NavigationMenuLinkProps {
#[props(default)]
pub current: bool,
pub onclick: Option<EventHandler<MouseEvent>>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = a)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn NavigationMenuLink(props: NavigationMenuLinkProps) -> Element {
let roving =
try_use_context::<NavigationMenuContext>().map(|NavigationMenuContext(roving)| roving);
let key = roving.map(|roving| roving.use_item(false, false));
let onclick = props.onclick;
rsx! {
a {
aria_current: props.current.then_some("page"),
class: props.class,
onclick: move |event| {
if let Some(handler) = onclick {
handler.call(event);
}
},
onkeydown: move |event| {
if let (Some(roving), Some(key)) = (roving, key) {
handle_roving_key(roving, key, &event);
}
},
onmounted: move |event| {
if let (Some(roving), Some(key)) = (roving, key) {
roving.mount(key, event.data());
}
},
..props.attributes,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct ToggleProps {
pub pressed: Option<bool>,
#[props(default)]
pub default_pressed: bool,
pub on_pressed_change: Option<EventHandler<bool>>,
#[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 Toggle(props: ToggleProps) -> Element {
let pressed = use_controllable(
props.pressed,
|| props.default_pressed,
props.on_pressed_change,
);
let group = try_use_context::<ToggleGroupContext>().map(|ToggleGroupContext(roving)| roving);
let key = group.map(|roving| roving.use_item(props.disabled, false));
let class = compose([Some("ui-toggle"), props.class.as_deref()]);
let is_pressed = pressed.get();
let onclick = props.onclick;
rsx! {
button {
aria_pressed: if is_pressed { "true" } else { "false" },
class,
"data-ui-controlled": props.pressed.is_some().then_some("true"),
"data-ui-toggle": "true",
disabled: props.disabled,
tabindex: group.zip(key).and_then(|(roving, key)| roving.tabindex(key)),
r#type: "button",
onclick: move |event| {
if let Some(handler) = onclick {
handler.call(event);
}
pressed.set(!pressed.peek());
},
onfocus: move |_| {
if let (Some(roving), Some(key)) = (group, key) {
roving.focused(key);
}
},
onkeydown: move |event| {
if let (Some(roving), Some(key)) = (group, key) {
handle_roving_key(roving, key, &event);
}
},
onmounted: move |event| {
if let (Some(roving), Some(key)) = (group, key) {
roving.mount(key, event.data());
}
},
..props.attributes,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct ToggleGroupProps {
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn ToggleGroup(props: ToggleGroupProps) -> Element {
let roving = Roving::use_new();
use_context_provider(|| ToggleGroupContext(roving));
let class = compose([Some("ui-toggle-group"), props.class.as_deref()]);
rsx! {
div { class, "data-ui-toggle-group": "true", ..props.attributes, {props.children} }
}
}