Skip to content

Dropdown Menu Primitive

Hooks Primitives Portal Primitives Slot Primitives Types Primitives Utils Primitives

@radix-ui/react-dropdown-menu


Shows a menu with options or actions when a user clicks the trigger button.

Installation

Install @radix-ui/react-dropdown-menu

Terminal window
npx expo install @radix-ui/react-dropdown-menu

Copy/paste the following code for web to ~/components/primitives/dropdown-menu/dropdown-menu.web.tsx

Copy/paste the following code for native to ~/components/primitives/dropdown-menu/dropdown-menu.tsx

Copy/paste the following code for types to ~/components/primitives/dropdown-menu/types.ts

Copy/paste the following code for exporting to ~/components/primitives/dropdown-menu/index.ts

Usage

import * as React from 'react';
import * as DropdownMenuPrimitive from '~/components/primitives/dropdown-menu';
import { Text } from 'react-native';
function Example() {
const [open, setOpen] = React.useState(false);
const [openSub, setOpenSub] = React.useState(false);
const [checkboxValue, setCheckboxValue] = React.useState(false);
const [subCheckboxValue, setSubCheckboxValue] = React.useState(false);
const [radioValue, setRadioValue] = React.useState('pedro');
return (
<DropdownMenu.Root
open={open}
onOpenChange={(newVal) => {
setOpen(newVal);
if (!newVal) {
setOpenSub(false);
}
}}>
<DropdownMenu.Trigger>
<Text>
Open Dropdown Menu
</Text>
</DropdownMenu.Trigger>
<DropdownMenuPrimitive.Portal>
<DropdownMenuPrimitive.Overlay>
<DropdownMenu.Content>
<DropdownMenu.Item>
<Text>Back</Text>
</DropdownMenu.Item>
<DropdownMenu.Item>
<Text>Forward</Text>
</DropdownMenu.Item>
<DropdownMenu.Item>
<Text>Reload</Text>
</DropdownMenu.Item>
<DropdownMenu.Sub open={openSub} onOpenChange={setOpenSub}>
<DropdownMenu.SubTrigger>
<Text>More Tools</Text>
</DropdownMenu.SubTrigger>
<DropdownMenu.SubContent>
<DropdownMenu.Item>
<Text>Save Page As...</Text>
</DropdownMenu.Item>
<DropdownMenu.Item>
<Text>Create Shortcut...</Text>
</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Item>
<Text>Developer Tools</Text>
</DropdownMenu.Item>
</DropdownMenuSubContent>
</DropdownMenuSub>
<DropdownMenu.Separator />
<DropdownMenu.CheckboxItem
checked={checkboxValue}
onCheckedChange={setCheckboxValue}
closeOnPress={false}
>
<Text>Show Bookmarks Bar</Text>
</DropdownMenu.CheckboxItem>
<DropdownMenu.CheckboxItem
checked={subCheckboxValue}
onCheckedChange={setSubCheckboxValue}
closeOnPress={false}
>
<Text>Show Full URLs</Text>
</DropdownMenu.CheckboxItem>
<DropdownMenu.Separator />
<DropdownMenu.RadioGroup value={radioValue} onValueChange={setRadioValue}>
<DropdownMenu.Label>People</DropdownMenu.Label>
<DropdownMenu.Separator />
<DropdownMenu.RadioItem value='pedro' closeOnPress={false}>
<Text>Elmer Fudd</Text>
</DropdownMenu.RadioItem>
<DropdownMenu.RadioItem value='colm' closeOnPress={false}>
<Text>Foghorn Leghorn</Text>
</DropdownMenu.RadioItem>
</DropdownMenu.RadioGroup>
</DropdownMenu.Content>
</DropdownMenuPrimitive.Overlay>
</DropdownMenuPrimitive.Portal>
</DropdownMenu.Root>
);
}

Props

Root

Extends View props

PropTypeNote
open*boolean
onOpenChange*(open: boolean) => void
asChildboolean(optional)

Trigger

Extends Pressable props

PropTypeNote
asChildboolean(optional)

Portal

PropTypeNote
children*React.ReactNode
forceMounttrue | undefined(optional)
hostNamestringWeb Only (optional)
containerHTMLElement | null | undefinedWeb Only (optional)

Overlay

Extends Pressable props

PropTypeNote
asChildboolean(optional)
forceMounttrue | undefined;(optional)

Content

Extends View props

PropTypeNote
asChildboolean(optional)
forceMounttrue | undefined(optional)
alignOffsetnumber(optional)
insetsInsets(optional)
avoidCollisionsboolean(optional)
align’start’ | ‘center’ | ‘end’(optional)
side’top’ | ‘bottom’(optional)
sideOffsetnumber(optional)
disablePositioningStylebooleanNative Only (optional)
loopbooleanWeb Only (optional)
onCloseAutoFocus(event: Event) => voidWeb Only (optional)
onEscapeKeyDown(event: KeyboardEvent) => voidWeb Only (optional)
onPointerDownOutside(event: PointerDownOutsideEvent) => voidWeb Only (optional)
onFocusOutside(event: FocusOutsideEvent) => voidWeb Only (optional)
onInteractOutsidePointerDownOutsideEvent | FocusOutsideEventWeb Only (optional)
collisionBoundaryElement | null | Array<Element | null>Web Only (optional)
sticky’partial’ | ‘always’Web Only (optional)
hideWhenDetachedbooleanWeb Only (optional)

Group

Extends Text props

PropTypeNote
asChildboolean(optional)

Label

Extends Text props

PropTypeNote
asChildboolean(optional)

Item

Extends Pressable props

PropTypeNote
asChildboolean(optional)
textValueboolean(optional)
closeOnPressboolean(optional)

CheckboxItem

Extends Pressable props

PropTypeNote
checked*boolean
onCheckedChange*(value: boolean) => void
textValue*string
asChildboolean(optional)
closeOnPressbooleanNative Only_(optional)_

RadioGroup

Extends View props

PropTypeNote
value*boolean
onValueChange*(value: boolean) => void
asChildboolean(optional)

RadioItem

Extends Pressable props

PropTypeNote
value*boolean
onCheckedChange*(value: boolean) => void
asChildboolean(optional)
closeOnPressbooleanNative Only_(optional)_

ItemIndicator

Extends View props

PropTypeNote
asChildboolean(optional)
forceMounttrue /undefined

Separator

Extends View props

PropTypeNote
asChildboolean(optional)
decorativeboolean(optional)

Sub

Extends View props

PropTypeNote
asChildboolean(optional)
open*boolean
onOpenChange*(value: boolean) => void

SubTrigger

Extends Pressable props

PropTypeNote
textValuestring(optional)
asChildboolean(optional)

SubContent

Extends Pressable props

PropTypeNote
asChildboolean(optional)
forceMounttrue /undefined

useRootContext

Must be used within a Root component. It provides the following values from the dropdown menu: open, and onOpenChange.

useSubContext

Must be used within a Sub component. It provides the following values from the dropdown menu: open, and onOpenChange.