1use dioxus::prelude::*;
4
5use crate::class::{Glass, compose};
6use crate::hooks::use_controllable;
7
8use super::chrome::{NavigationVariant, surface};
9use super::roving::{Roving, RovingAxis, roving_move};
10
11#[derive(Clone, Copy)]
13struct MenubarContext(Roving);
14
15#[derive(Clone, Copy)]
17struct NavigationMenuContext(Roving);
18
19#[derive(Clone, Copy)]
21struct ToggleGroupContext(Roving);
22
23fn handle_roving_key(roving: Roving, key: usize, event: &KeyboardEvent) -> bool {
25 let modifiers = event.modifiers();
26
27 if event.is_composing() || modifiers.alt() || modifiers.ctrl() || modifiers.meta() {
28 return false;
29 }
30
31 let Some(movement) = roving_move(&event.key(), RovingAxis::Both, false) else {
32 return false;
33 };
34
35 event.prevent_default();
36 roving.move_focus(Some(key), movement).is_some()
37}
38
39#[derive(Props, Clone, PartialEq)]
41pub struct MenubarProps {
42 #[props(default, into)]
44 pub glass: Glass,
45 #[props(into)]
47 pub class: Option<String>,
48 #[props(extends = GlobalAttributes, extends = nav)]
50 pub attributes: Vec<Attribute>,
51 pub children: Element,
53}
54
55#[component]
60pub fn Menubar(props: MenubarProps) -> Element {
61 let roving = Roving::use_new();
62 use_context_provider(|| MenubarContext(roving));
63
64 let glass = props.glass.class("ui-menubar");
65 let class = compose([Some("ui-menubar"), glass.as_deref(), props.class.as_deref()]);
66
67 rsx! {
68 nav {
69 class,
70 "data-surface": surface(props.glass),
71 "data-ui-menubar": "true",
72 ..props.attributes,
73 {props.children}
74 }
75 }
76}
77
78#[derive(Props, Clone, PartialEq)]
80pub struct MenubarItemProps {
81 #[props(default)]
83 pub disabled: bool,
84 pub onclick: Option<EventHandler<MouseEvent>>,
86 #[props(into)]
88 pub class: Option<String>,
89 #[props(extends = GlobalAttributes, extends = button)]
91 pub attributes: Vec<Attribute>,
92 pub children: Element,
94}
95
96#[component]
98pub fn MenubarItem(props: MenubarItemProps) -> Element {
99 let MenubarContext(roving) = try_use_context::<MenubarContext>()
100 .unwrap_or_else(|| panic!("MenubarItem must be used inside a Lumen Menubar."));
101 let key = roving.use_item(props.disabled, false);
102 let onclick = props.onclick;
103
104 rsx! {
105 button {
106 class: props.class,
107 disabled: props.disabled,
108 role: "menuitem",
109 tabindex: roving.tabindex(key),
110 r#type: "button",
111 onclick: move |event| {
112 if let Some(handler) = onclick {
113 handler.call(event);
114 }
115 },
116 onfocus: move |_| roving.focused(key),
117 onkeydown: move |event| {
118 handle_roving_key(roving, key, &event);
119 },
120 onmounted: move |event| roving.mount(key, event.data()),
121 ..props.attributes,
122 {props.children}
123 }
124 }
125}
126
127#[derive(Props, Clone, PartialEq)]
129pub struct NavigationMenuProps {
130 #[props(default)]
132 pub variant: NavigationVariant,
133 #[props(default, into)]
135 pub glass: Glass,
136 #[props(into)]
138 pub class: Option<String>,
139 #[props(extends = GlobalAttributes, extends = nav)]
141 pub attributes: Vec<Attribute>,
142 pub children: Element,
144}
145
146#[component]
151pub fn NavigationMenu(props: NavigationMenuProps) -> Element {
152 let roving = Roving::use_new();
153 use_context_provider(|| NavigationMenuContext(roving));
154
155 let unstyled = props.variant.modifier("ui-navigation-menu");
156 let glass = props.glass.class("ui-navigation-menu");
157 let class = compose([
158 Some("ui-navigation-menu"),
159 unstyled.as_deref(),
160 glass.as_deref(),
161 props.class.as_deref(),
162 ]);
163
164 rsx! {
165 nav {
166 class,
167 "data-slot": "navigation-menu",
168 "data-surface": surface(props.glass),
169 "data-ui-navigation-menu": "true",
170 "data-variant": props.variant.as_str(),
171 ..props.attributes,
172 {props.children}
173 }
174 }
175}
176
177#[derive(Props, Clone, PartialEq)]
179pub struct NavigationMenuLinkProps {
180 #[props(default)]
182 pub current: bool,
183 pub onclick: Option<EventHandler<MouseEvent>>,
185 #[props(into)]
187 pub class: Option<String>,
188 #[props(extends = GlobalAttributes, extends = a)]
190 pub attributes: Vec<Attribute>,
191 pub children: Element,
193}
194
195#[component]
197pub fn NavigationMenuLink(props: NavigationMenuLinkProps) -> Element {
198 let roving =
199 try_use_context::<NavigationMenuContext>().map(|NavigationMenuContext(roving)| roving);
200 let key = roving.map(|roving| roving.use_item(false, false));
201 let onclick = props.onclick;
202
203 rsx! {
204 a {
205 aria_current: props.current.then_some("page"),
206 class: props.class,
207 onclick: move |event| {
208 if let Some(handler) = onclick {
209 handler.call(event);
210 }
211 },
212 onkeydown: move |event| {
213 if let (Some(roving), Some(key)) = (roving, key) {
214 handle_roving_key(roving, key, &event);
215 }
216 },
217 onmounted: move |event| {
218 if let (Some(roving), Some(key)) = (roving, key) {
219 roving.mount(key, event.data());
220 }
221 },
222 ..props.attributes,
223 {props.children}
224 }
225 }
226}
227
228#[derive(Props, Clone, PartialEq)]
230pub struct ToggleProps {
231 pub pressed: Option<bool>,
234 #[props(default)]
236 pub default_pressed: bool,
237 pub on_pressed_change: Option<EventHandler<bool>>,
239 #[props(default)]
241 pub disabled: bool,
242 pub onclick: Option<EventHandler<MouseEvent>>,
244 #[props(into)]
246 pub class: Option<String>,
247 #[props(extends = GlobalAttributes, extends = button)]
249 pub attributes: Vec<Attribute>,
250 pub children: Element,
252}
253
254#[component]
258pub fn Toggle(props: ToggleProps) -> Element {
259 let pressed = use_controllable(
260 props.pressed,
261 || props.default_pressed,
262 props.on_pressed_change,
263 );
264 let group = try_use_context::<ToggleGroupContext>().map(|ToggleGroupContext(roving)| roving);
265 let key = group.map(|roving| roving.use_item(props.disabled, false));
266 let class = compose([Some("ui-toggle"), props.class.as_deref()]);
267 let is_pressed = pressed.get();
268 let onclick = props.onclick;
269
270 rsx! {
271 button {
272 aria_pressed: if is_pressed { "true" } else { "false" },
273 class,
274 "data-ui-controlled": props.pressed.is_some().then_some("true"),
275 "data-ui-toggle": "true",
276 disabled: props.disabled,
277 tabindex: group.zip(key).and_then(|(roving, key)| roving.tabindex(key)),
278 r#type: "button",
279 onclick: move |event| {
280 if let Some(handler) = onclick {
281 handler.call(event);
282 }
283
284 pressed.set(!pressed.peek());
285 },
286 onfocus: move |_| {
287 if let (Some(roving), Some(key)) = (group, key) {
288 roving.focused(key);
289 }
290 },
291 onkeydown: move |event| {
292 if let (Some(roving), Some(key)) = (group, key) {
293 handle_roving_key(roving, key, &event);
294 }
295 },
296 onmounted: move |event| {
297 if let (Some(roving), Some(key)) = (group, key) {
298 roving.mount(key, event.data());
299 }
300 },
301 ..props.attributes,
302 {props.children}
303 }
304 }
305}
306
307#[derive(Props, Clone, PartialEq)]
309pub struct ToggleGroupProps {
310 #[props(into)]
312 pub class: Option<String>,
313 #[props(extends = GlobalAttributes, extends = div)]
316 pub attributes: Vec<Attribute>,
317 pub children: Element,
319}
320
321#[component]
323pub fn ToggleGroup(props: ToggleGroupProps) -> Element {
324 let roving = Roving::use_new();
325 use_context_provider(|| ToggleGroupContext(roving));
326
327 let class = compose([Some("ui-toggle-group"), props.class.as_deref()]);
328
329 rsx! {
330 div { class, "data-ui-toggle-group": "true", ..props.attributes, {props.children} }
331 }
332}