Dropdown

Displays a menu to the user, offering a series of actions or functions that can be executed by clicking buttons.

Installation

If @melio-ui/react is already installed globally, you can skip this step.

Import

  • DropdownRoot: All parts of the dropdown are included.
  • DropdownTrigger: Used to activate or open the Dropdown component.
  • DropdownPortal: Portal the overlay and content portion into the body.
  • DropdownContent: Includes the content that will be displayed in the open menu box.
  • DropdownArrow: An optional arrow element for the dropdown, to be rendered within DropdownContent for visual association.
  • DropdownItem: It represents a single item in a dropdown menu and can be customized with labels, icons, and actions.

Another way to import

'use client';
import { Dropdown } from '@melio-ui/dropdown';
<Dropdown.Root>
<Dropdown.Trigger asChild>
<button type="button">Dropdown</button>
</Dropdown.Trigger>
<Dropdown.Portal>
<Dropdown.Content>
<Dropdown.Item>DropdownItem - 1</Dropdown.Item>
<Dropdown.Item>DropdownItem - 2</Dropdown.Item>
<Dropdown.Item>DropdownItem - 3</Dropdown.Item>
</Dropdown.Content>
</Dropdown.Portal>
</Dropdown.Root>;

'use client' must be used when rendering on the server side.

API Reference

All parts of the dropdown are included.

PropTypeDefaultDescription
openboolean-Sets whether the dropdown menu is open or closed.
defaultOpenboolean-When first rendered, the dropdown menu is in an opened state. Use when there is no need to control the opened state.
onOpenChange(open: boolean) => void-This is an event handler that is triggered when the open state of the dropdown menu changes.

Used to activate or open the Dropdown component.

PropTypeDefaultDescription
asChildboolean-Changes the default rendering element passed as a child, merging its props and behavior.
Data attributeValues
[data-state]"open" | "closed"

Portal the overlay and content portion into the body.

PropTypeDefaultDescription
containerHTMLElement | (() => HTMLElement)document.bodySpecifies the container element to which the content should be portaled.

Includes the content that will be displayed in the open menu box.

PropTypeDefaultDescription
side"top" | "right" | "bottom" | "left""top"The ideal side of the trigger to render on when it's open.
sideOffsetnumber0The pixel distance from the trigger.
align"start" | "center" | "end""center"The desired alignment with the trigger, which may adjust if collisions happen.
alignOffsetnumber0A pixel offset from the "start" or "end" alignment choices.
destroyOnClosebooleantrueUsed to ensure that the DOM is removed when closed.
forceMountboolean-Used to ensure that a component is always rendered in the DOM, regardless of its visibility or whether it's conditionally displayed.
closeOnEscbooleantrueDetermines whether or not to close when the ESC key is pressed.
closeOnBlurbooleantrueDetermines whether to close on blur.
closeOnItemClickbooleantrueDetermines whether the menu item will close when clicked.
Data attributeValues
[data-state]"open" | "closed"
[data-side]"top" | "right" | "bottom" | "left"
[data-align]"start" | "center" | "end"

An optional arrow element for the dropdown, to be rendered within DropdownContent for visual association.

PropTypeDefaultDescription
asChildboolean-Changes the default rendering element passed as a child, merging its props and behavior.
widthnumber10The arrow's width measured in pixels.
heightnumber5The arrow's height measured in pixels.

It represents a single item in a dropdown menu and can be customized with labels, icons, and actions.

PropTypeDefaultDescription
disabledboolean-Determines whether the menu item is inactive.
Data attributeValues
[data-disabled]Visible when disabled

Examples

Position of dropdown menu

<DropdownRoot>
<DropdownTrigger asChild>
<button
type="button"
style={{
border: '1px solid #475569',
borderRadius: 5,
padding: 10,
}}
>
Dropdown
</button>
</DropdownTrigger>
<DropdownPortal>
<DropdownContent side="right" align="start">
<DropdownItem>DropdownItem - 1</DropdownItem>
<DropdownItem>DropdownItem - 2</DropdownItem>
<DropdownItem>DropdownItem - 3</DropdownItem>
</DropdownContent>
</DropdownPortal>
</DropdownRoot>

Display arrow

<DropdownRoot>
<DropdownTrigger asChild>
<button
type="button"
style={{
border: '1px solid #475569',
borderRadius: 5,
padding: 10,
}}
>
Dropdown
</button>
</DropdownTrigger>
<DropdownPortal>
<DropdownContent>
<DropdownItem>DropdownItem - 1</DropdownItem>
<DropdownItem>DropdownItem - 2</DropdownItem>
<DropdownItem>DropdownItem - 3</DropdownItem>
<DropdownArrow />
</DropdownContent>
</DropdownPortal>
</DropdownRoot>

Custom Dropdown Menu

<DropdownRoot>
<DropdownTrigger asChild>
<button
type="button"
style={{
border: '1px solid #475569',
borderRadius: 5,
padding: 10,
}}
>
Dropdown
</button>
</DropdownTrigger>
<DropdownPortal>
<DropdownContent>
<div style={{ padding: '0.625rem 0.3125rem' }}>
<div style={{ paddingLeft: '0.3125rem', paddingBottom: '0.5rem' }}>Title 1</div>
<DropdownItem>DropdownItem - 1</DropdownItem>
<DropdownItem>DropdownItem - 2</DropdownItem>
<DropdownItem>DropdownItem - 3</DropdownItem>
</div>
<div style={{ padding: '0.625rem 0.3125rem' }}>
<div style={{ paddingLeft: '0.3125rem', paddingBottom: '0.5rem' }}>Title 2</div>
<DropdownItem>DropdownItem - 4</DropdownItem>
<DropdownItem>DropdownItem - 5</DropdownItem>
<DropdownItem>DropdownItem - 6</DropdownItem>
</div>
</DropdownContent>
</DropdownPortal>
</DropdownRoot>
DocsMelioUI
Copyright © Ahn Co. All rights reserved.