MCPcopy Create free account
hub / github.com/DioxusLabs/dioxus-components / DropdownMenuTrigger

Function DropdownMenuTrigger

primitives/src/dropdown_menu.rs:220–274  ·  view source on GitHub ↗
(props: DropdownMenuTriggerProps)

Source from the content-addressed store, hash-verified

218/// - `data-disabled`: Indicates if the dropdown menu is disabled. values are `true` or `false`.
219#[component]
220pub fn DropdownMenuTrigger(props: DropdownMenuTriggerProps) -> Element {
221 let mut ctx: DropdownMenuContext = use_context();
222 let mut element = use_signal(|| None::<Rc<MountedData>>);
223
224 let open = ctx.open;
225 let disabled = ctx.disabled;
226 let data_state = if open() { "open" } else { "closed" };
227
228 let base = attributes!(button {
229 id: ctx.trigger_id,
230 r#type: "button",
231 "data-state": data_state,
232 "data-disabled": disabled,
233 disabled: disabled,
234 aria_expanded: open,
235 aria_haspopup: "listbox",
236 onmounted: move |e: MountedEvent| {
237 element.set(Some(e.data()));
238 },
239 onclick: move |_| {
240 if disabled() {
241 return;
242 }
243
244 let new_open = !open();
245 ctx.set_open.call(new_open);
246
247 // Focus the element on click. Safari does not do this automatically.
248 // https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/button#clicking_and_focus
249 if let Some(data) = element() {
250 spawn(async move {
251 _ = data.set_focus(true).await;
252 });
253 }
254 },
255 onblur: move |_| {
256 if !ctx.focus.any_focused() {
257 ctx.focus.clear_focus();
258 ctx.set_open.call(false);
259 }
260 },
261 });
262 let merged = merge_attributes(vec![base, props.attributes]);
263
264 if let Some(dynamic) = props.r#as {
265 dynamic.call(merged)
266 } else {
267 rsx! {
268 button {
269 ..merged,
270 {props.children}
271 }
272 }
273 }
274}
275
276/// The props for the [`DropdownMenuContent`] component
277#[derive(Props, Clone, PartialEq)]

Callers

nothing calls this directly

Calls 1

merge_attributesFunction · 0.85

Tested by

no test coverage detected