ZUI

Search documentation

Find a page or a heading.

Dropdown menu

A menu of actions that opens from a button.

Installation

bash

The CLI writes components/ui/dropdown-menu.tsx, lib/menu.ts, lib/motion.ts and lib/overlay.ts, installs Direction, Icon, Text and Utils, and adds @rn-primitives/dropdown-menu, @rn-primitives/portal, lucide-react-native, react-native-reanimated and react-native-worklets. The menu draws in the app's PortalHost; see Add a portal host. Without one it draws nothing, and logs an error saying so while the app is in development.

Usage

tsx
tsx

Examples

Checkboxes and radio items

tsx
tsx

API reference

Takes every prop of React Native's View, plus:

Without open the menu holds its own state, starting from defaultOpen. With it, the menu follows open, and a press on the trigger, an item, outside the menu or the back button only reports the change it asks for through onOpenChange.

Takes every prop of React Native's Pressable. A press opens the menu, and another closes it. Pass asChild to make a Button do it. It is announced as expanded or collapsed, and focus returns to it when the menu closes.

Takes every prop of React Native's View, plus:

It is a menu, and accessibility focus moves into it as it opens. Like upstream's, it is modal: VoiceOver stays inside it, and TalkBack does once the app hides its screens while an overlay is open (see Add a portal host). A press outside it, the Android back button, the iOS escape gesture (a two-finger Z) and Escape on the web close it. It fades in while growing from 95% and sliding 0.5rem away from the trigger over 100ms, and out the same way. When the system asks for reduced motion it appears and goes at once. Pass a width class, such as w-56, to size it.

Takes every prop of React Native's Pressable, plus:

A menuitem. A press runs onPress and closes the menu, unless closeOnClick is false. It is highlighted while a finger is on it, and on the web while the pointer or the keyboard's focus is. inset indents it to line up with the text of a checkbox or radio item. A disabled item is dimmed, announced as disabled and ignores presses.

Takes every prop of DropdownMenuItem but variant, plus:

A press toggles it and leaves the menu open, as upstream's does; set closeOnClick to close it. A check mark shows at its end while it is checked.

The group takes every prop of React Native's View, plus value, defaultValue and onValueChange, and each item a value and every prop of DropdownMenuCheckboxItem but the checked ones. A press chooses the item and leaves the menu open; the chosen one shows a check mark.

The sub-menu takes open, defaultOpen and onOpenChange. Its trigger is a menuitem with a chevron at its end, announced as expanded or collapsed, and takes inset.

The group is a group; the label, which takes inset, is muted text over it. The separator is a hairline between groups, and the shortcut the keys of an item, drawn muted at its end. DropdownMenuPortal is exported so upstream's markup ports as it is, and draws its children where it stands.

Right to left

The chevron of a DropdownMenuSubTrigger is mirrored when Direction says text reads right to left, as upstream's cn-rtl-flip does.

Differences from upstream

  • Upstream's render prop becomes asChild, and onClick becomes onPress, as on every native item.
  • On a device the menu opens above or below its trigger, so left and right open it below. It is kept on the screen but not moved to the other side when there is no room. The web places it on any side, and flips it.
  • On a device a sub-menu opens in place, under its trigger, and a second press on the trigger closes it. The web opens it beside the trigger. DropdownMenuSubContent takes no side or offset, and it appears at once.
  • React Native has no menuitemcheckbox or menuitemradio role, so on a device a checkbox item is announced as a checkbox and a radio item as a radio button, each with its checked state. The web gives them upstream's roles.
  • Upstream sizes the menu to its trigger's width at least; the native menu is as wide as its items, or as a width class sets.
  • The back button and the iOS escape gesture remove the menu at once, without its exit animation.
  • Upstream's animation runs whatever the system's motion setting. The native one is skipped when the system asks for reduced motion.
  • portalHost is added, for an app that draws overlays into a PortalHost of its own.