Skip to main content

strata_ui/
menu.rs

1//! High-Level Accessible Menu, ContextMenu & Dropdown System for Strata UI.
2
3#![allow(non_snake_case)]
4
5use std::rc::Rc;
6use strata_dom::{Event, NodeBlueprint, View};
7use strata_icons::prelude::{Icon, IconName};
8use strata_macro::{component, rsx};
9
10/// Dropdown menu container rendered as a floating popup overlay.
11#[component]
12pub fn DropdownMenu(
13    is_open: bool,
14    class: Option<&'static str>,
15    children: Vec<NodeBlueprint>,
16) -> View {
17    if !is_open {
18        return rsx! { <div class="hidden" /> };
19    }
20
21    let custom = class.unwrap_or("");
22    let base = "absolute right-0 top-10 w-72 bg-[#2b2b2b] border border-neutral-700/80 rounded-xl shadow-2xl py-1.5 z-50 text-neutral-200 text-xs font-sans select-none overflow-hidden animate-in fade-in zoom-in-95 duration-100";
23
24    rsx! {
25        <div class={format!("{} {}", base, custom)}>
26            {children}
27        </div>
28    }
29}
30
31/// Context menu container positioned absolutely at specific coordinate offsets.
32#[component]
33pub fn ContextMenu(
34    is_open: bool,
35    position: (f32, f32),
36    class: Option<&'static str>,
37    children: Vec<NodeBlueprint>,
38) -> View {
39    if !is_open {
40        return rsx! { <div class="hidden" /> };
41    }
42
43    let (x, y) = position;
44    let custom = class.unwrap_or("");
45
46    rsx! {
47        <div
48            data-clamp-viewport="true"
49            data-keep-focus="true"
50            class={format!("fixed w-64 bg-[#2b2b2b] border border-neutral-700/80 rounded-xl shadow-2xl py-1.5 z-50 text-neutral-200 text-xs font-sans select-none overflow-hidden {}", custom)}
51            style={format!("left: {}px; top: {}px;", x, y)}
52        >
53            {children}
54        </div>
55    }
56}
57
58/// Individual interactive item within a Menu or ContextMenu.
59#[component]
60pub fn MenuItem(
61    label: String,
62    icon: Option<IconName>,
63    shortcut: Option<String>,
64    disabled: Option<bool>,
65    destructive: Option<bool>,
66    onclick: Option<Rc<dyn Fn()>>,
67) -> View {
68    let is_disabled = disabled.unwrap_or(false);
69    let is_destructive = destructive.unwrap_or(false);
70
71    let (text_color, hover_bg) = if is_disabled {
72        ("text-neutral-500 pointer-events-none", "")
73    } else if is_destructive {
74        ("text-rose-400 hover:text-rose-300", "hover:bg-rose-600/20")
75    } else {
76        ("text-neutral-200 hover:text-white", "hover:bg-blue-600/30")
77    };
78
79    let click_handler = onclick.clone();
80
81    rsx! {
82        <button
83            type="button"
84            class={format!("w-full flex items-center justify-between px-4 py-2 transition-colors cursor-pointer text-left {} {}", text_color, hover_bg)}
85            onclick={move |e: Event| {
86                e.stop_propagation();
87                if let Some(ref cb) = click_handler {
88                    cb();
89                }
90            }}
91        >
92            <div class="flex items-center gap-2.5 min-w-0 flex-1">
93                {if let Some(ic) = icon {
94                    rsx! {
95                        <div class="flex items-center justify-center flex-shrink-0 text-neutral-400">
96                            {Icon(ic, Some("w-4 h-4 text-current")).root}
97                        </div>
98                    }
99                } else {
100                    rsx! { <div class="hidden" /> }
101                }}
102                <span class="truncate">{label}</span>
103            </div>
104            {if let Some(sc) = shortcut {
105                rsx! {
106                    <span class="text-[11px] text-neutral-500 font-mono tracking-wider ml-4 flex-shrink-0">
107                        {sc}
108                    </span>
109                }
110            } else {
111                rsx! { <div class="hidden" /> }
112            }}
113        </button>
114    }
115}
116
117/// Subtle horizontal separator dividing groups of related menu items.
118#[component]
119pub fn MenuSeparator() -> View {
120    rsx! {
121        <div class="h-px bg-neutral-700/60 my-1 w-full" />
122    }
123}
124
125/// Text header labeling a sub-section of menu items.
126#[component]
127pub fn MenuSectionHeader(title: String) -> View {
128    rsx! {
129        <div class="px-4 py-1 text-[10px] font-semibold text-neutral-500 uppercase tracking-wider select-none">
130            {title}
131        </div>
132    }
133}