Menu

A list of actions that opens from a button, such as Edit, Duplicate, and Delete.

Usage

Code
import { Menu } from '@leverui/components';
Code
<Menu.Root>
  <Menu.Trigger />
  <Menu.Positioner>
    <Menu.Content>
      <Menu.Item />

      <Menu.ItemGroup>
        <Menu.Item />
      </Menu.ItemGroup>

      <Menu.Separator />
      <Menu.Arrow />

      <Menu.CheckboxItem>
        <Menu.ItemIndicator />
      </Menu.CheckboxItem>

      <Menu.RadioItemGroup>
        <Menu.RadioItem>
          <Menu.ItemIndicator />
        </Menu.RadioItem>
      </Menu.RadioItemGroup>
    </Menu.Content>
  </Menu.Positioner>
</Menu.Root>

Examples

Controlled

Use the open and onOpenChange prop to control the visibility of the menu.

open: false

Store

An alternative way to control the menu is to use the RootProvider component and the useMenu store hook.

open: false

This way you can access the menu state and methods from outside the menu.

Command

Use the Menu.ItemCommand component to display a command in the menu.

Context menu

Use the Menu.ContextTrigger component to create a context menu.

Group

Use the Menu.ItemGroup component to group related menu items.

Danger Item

Here's an example of how to style a menu item that is used to delete an item.

Submenu

Here's an example of how to create a submenu.

Pass the asChild prop to the Menu.Item component to render a link.

When using custom router links, you need to set the navigate prop on the Menu.Root component.

Code
import { Menu } from '@leverui/components';
import { useNavigate } from '@solidjs/router';

export function Demo() {
  const navigate = useNavigate();

  return (
    <Menu.Root navigate={({ value }) => navigate(`/${value}`)}>
      {/* ... */}
    </Menu.Root>
  );
}

Radio Items

Here's an example of how to create a menu with radio items.

Checkbox Items

Here's an example of how to create a menu with checkbox items.

Icon and Command

Compose the menu to include icons and commands.

Placement

Use the positioning.placement prop to control the placement of the menu.

Avatar

Here's an example that composes the Menu with the Avatar component to display a menu underneath an avatar.

Anchor Point

Use the positioning.anchorPoint prop to control the anchor point of the menu.

Anchor

You can derive it from the getBoundingClientRect of a DOM element, or use something like DOMRect.fromRect({ x: 0, y: 0, width: 1, height: 1 }) to create a new rect.

Mixed Layout

Here's an example of how to create a mixed layout of menu items. In this layout, the top horizontal menu includes common menu items.

Overflow

When you have a long list of menu items, you can set max-height with the style prop on the Menu.Content to create a scrollable menu.

The content has a default max-height: var(--available-height), which is the maximum available height for the content relative to the viewport.

Hide When Detached

When the menu is rendered in an scrolling container, set the positioning.hideWhenDetached to true to hide the menu when the trigger is scrolled out of view.

Item0

Item1

Item2

Item3

Item4

Item5

Open From Dialog

To use the Menu within a Dialog, you need to avoid portalling the Menu.Positioner to the document's body.

Code
-<Portal>
  <Menu.Positioner>
    <Menu.Content>
      {/* ... */}
    </Menu.Content>
  </Menu.Positioner>
-</Portal>

If you have set scrollBehavior="inside" on the Dialog, you need to:

  • Set the menu positioning to fixed to avoid the menu from being clipped by the dialog.
  • Set hideWhenDetached to true to hide the menu when the trigger is scrolled out of view.
Code
<Menu.Root positioning={{ strategy: 'fixed', hideWhenDetached: true }}>
  {/* ... */}
</Menu.Root>

Open from Dialog (Nested)

When a Menu item opens a Dialog that contains another Menu, use createOverlay to avoid unexpected close behavior caused by event bubbling.

Guide

Styling highlighted items

An item is highlighted while the pointer is over it or the keyboard has moved to it. Style [data-highlighted].

Code
<Menu.Item value="custom" class="custom-highlight">
  Custom highlighted item
</Menu.Item>
Code
.custom-highlight[data-highlighted] {
  background: var(--leverui-colors-blue-500);
  color: white;
}

Styling open state

Style [data-state='open'] on the trigger.

Code
<Menu.Trigger
  asChild={(getProps) => (
    <Button {...getProps()} class="custom-trigger">
      Menu
    </Button>
  )}
/>
Code
.custom-trigger[data-state='open'] {
  background: var(--leverui-colors-blue-500);
  color: white;
}

Props

Root

PropDefaultTypeDescription
closeOnSelecttruebooleanClose when an item is selected.
compositetruebooleanCombined with another composite widget, such as a combobox or tabs.
lazyMounttruebooleanRender the content only once the menu first opens.
typeaheadtruebooleanTyping moves to the matching item.
unmountOnExittruebooleanRemove the content from the page on close.
variantsubtle'subtle' | 'solid'Menu style.
sizemd'sm' | 'md'Menu size.
unstyled—booleanTurn off the default styles.
anchorPoint—PointPoint to position the menu at. The context menu or button trigger can set it.
aria-label—stringAccessible label.
defaultHighlightedValue—stringItem highlighted at first. Uncontrolled.
defaultOpen—booleanOpen at first. Uncontrolled.
defaultTriggerValue—stringTrigger value at first. Uncontrolled.
highlightedValue—stringHighlighted item. Controlled.
id—stringUnique id.
ids—Partial<{ trigger: string | ((value?: string | undefined) => string); contextTrigger: string | ((value?: string | undefined) => string); content: string; groupLabel: (id: string) => string; group: (id: string) => string; positioner: string; arrow: string }>Ids of the menu's elements. For combining with other components.
immediate—booleanApply a present change right away, not on the next frame.
loopFocusfalsebooleanWrap keyboard navigation from the last item to the first.
navigate—(details: NavigateDetails) => voidCalled to go to the selected item when it is a link.
onEscapeKeyDown—(event: KeyboardEvent) => voidCalled when Escape is pressed.
onExitComplete—VoidFunctionCalled when the closing animation ends.
onFocusOutside—(event: FocusOutsideEvent) => voidCalled when focus moves outside.
onHighlightChange—(details: HighlightChangeDetails) => voidCalled when the highlighted item changes.
onInteractOutside—(event: InteractOutsideEvent) => voidCalled on an interaction outside.
onOpenChange—(details: OpenChangeDetails) => voidCalled when the menu opens or closes.
onPointerDownOutside—(event: PointerDownOutsideEvent) => voidCalled when the pointer is pressed outside.
onRequestDismiss—(event: LayerDismissEvent) => voidCalled when this layer closes because a parent layer closed.
onSelect—(details: SelectionDetails) => voidCalled when an item is selected.
onTriggerValueChange—(details: TriggerValueChangeDetails) => voidCalled when the trigger value changes.
open—booleanOpen state. Controlled.
positioning—PositioningOptionsPlacement options.
present—booleanPresent state. Controlled.
skipAnimationOnMountfalsebooleanSkip the animation on first show.
triggerValue—stringTrigger value. Controlled.

Item

PropDefaultTypeDescription
value—stringUnique value for the item. Required.
asdivNative tagHTML tag to render.
asChild—Solid render functionRender your own element with the item's props.
closeOnSelect—booleanClose the menu when this item is selected.
disabled—booleanDisable the item.
onSelect—VoidFunctionCalled when the item is selected.
valueText—stringText matched when someone types. Default: the item's text content.

Give each item a value that does not change and a clear name. Run actions in onSelect. Links can be native a elements. Do not put a navigation link that must stay visible only in a menu.

Customize with CSS

Styles ship for size="sm" | "md" and variant="subtle" | "solid". Both take one value, an array with a value per breakpoint, or an object keyed by breakpoint name.

Each part has a leverui-menu__<part> class, a part attribute (such as data-menu-content), and size and variant attributes. Menus in a portal and nested menus keep them.

Code
<Menu.Content class="account-menu" style={{ 'max-height': '20rem' }}>
  ...
</Menu.Content>
Code
.account-menu {
  --leverui-menu-background: var(--leverui-colors-bg-subtle);
}

Variables: --leverui-menu-background, --leverui-menu-z-index, --leverui-menu-highlight-background, --leverui-menu-highlight-color. unstyled on Root or a part turns off its default styles.

Explorer

Click a part name in the sidebar to highlight it in the preview.

Component Anatomy

Hover to highlight, click to select parts