Liking cljdoc? Tell your friends :D

Menus from data

You build every menu from plain ClojureScript data. A vector of item maps becomes the menu; nesting an item's :items makes a sub-menu.

Components

All of these live in the single public namespace reagent-mui-nested-menu.core. The API reference (signatures and docstrings) sits in the namespace list on the left.

ComponentPurpose
nested-menuDropdown button that opens a nested menu.
context-menuWrap content so a right-click opens a nested menu at the pointer.
nested-menu-itemA menu item that opens a sub-menu (low-level).
icon-menu-itemA leaf menu item with optional icons (low-level).
menu-items-from-dataTurn item data into a seq of menu elements.
chevron-right / chevron-downThe default MUI SvgIcon chevrons.

Dropdown

[nested-menu
 {:items        items          ;; vector of item maps (see below)
  :label        "Actions"      ;; button text (or :label inside :button-props)
  :button-props {...}          ;; props forwarded to MUI Button
  :menu-props   {...}          ;; props forwarded to the root MUI Menu
  :direction    :right         ;; :right (default) or :left
  :value        :high          ;; currently selected value (highlights leaves)
  :on-click     (fn [e] ...)}] ;; extra handler when the button is clicked

Context menu

Right-click anywhere inside the wrapped content to open the menu at the pointer.

[context-menu
 {:items items}                ;; same item maps as the dropdown
 [:div {:style {:height 200}}
  "Right-click anywhere in here"]]

Item maps

Each item map accepts:

KeyTypeDescription
:labelstringItem text.
:render-label(fn [] hiccup)Custom label; takes precedence over :label.
:left-iconhiccup/elementRendered before the label.
:right-iconhiccup/elementRendered after the label (defaults to a chevron for sub-menus).
:callback(fn [event item])Runs when a leaf is clicked, then the menu closes.
:itemsvectorChild items that turn this entry into a sub-menu.
:disabledbooleanDisable the item.
:delaynumberms to hover before the sub-menu opens (default 0).
:valueanySelection value; highlighted when it equals the root :value.
:sxmapForwarded to the MUI MenuItem (MUI v5+ only).
:uidstringStable React key (otherwise :label/index is used).

Clicking an item that has :items opens its sub-menu and skips :callback. Only leaf items run :callback.

Icons

Pass any element as :left-icon or :right-icon. MUI icon components work through r/create-element:

(:require ["@mui/icons-material/ContentCopy$default" :as CopyIcon]
          [reagent.core :as r])

[nested-menu
 {:label "Edit"
  :items [{:label "Copy"
           :left-icon (r/create-element CopyIcon)
           :callback (fn [_e _item] (copy!))}
          {:label "Disabled" :disabled true}]}]

Custom labels and selection

:render-label returns hiccup, so an item can show a title with a subtitle or a badge. Pair root :value with per-item :value to highlight the current choice:

[nested-menu
 {:value @priority
  :items (for [k [:low :high]]
           {:label (name k)
            :value k
            :callback (fn [_ _] (reset! priority k))})}]

Next: React usage and Styling.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close