# Dropdown

A composable dropdown component that can handle both simple navigation menus as well as complex action menus with checkboxes, radios, and submenus.

```tsx
import { Button, Dropdown } from '@fluxui-dev/react';
import { ChevronDownIcon, PlusIcon, TrashIcon } from '@fluxui-dev/react/icons';

<Dropdown>
    <Dropdown.Trigger>
        <Button iconTrailing={ChevronDownIcon}>Options</Button>
    </Dropdown.Trigger>

    <Dropdown.Menu>
        <Dropdown.Item icon={PlusIcon}>New post</Dropdown.Item>

        <Dropdown.Separator />

        <Dropdown.Submenu heading="Sort by">
            <Dropdown.RadioGroup defaultValue="name">
                <Dropdown.Radio value="name">Name</Dropdown.Radio>
                <Dropdown.Radio value="date">Date</Dropdown.Radio>
                <Dropdown.Radio value="popularity">Popularity</Dropdown.Radio>
            </Dropdown.RadioGroup>
        </Dropdown.Submenu>

        <Dropdown.Submenu heading="Filter">
            <Dropdown.Checkbox defaultChecked>Draft</Dropdown.Checkbox>
            <Dropdown.Checkbox defaultChecked>Published</Dropdown.Checkbox>
            <Dropdown.Checkbox>Archived</Dropdown.Checkbox>
        </Dropdown.Submenu>

        <Dropdown.Separator />

        <Dropdown.Item variant="danger" icon={TrashIcon}>Delete</Dropdown.Item>
    </Dropdown.Menu>
</Dropdown>
```

## Navigation menus

Display a simple set of links in a dropdown menu.

```tsx
<Dropdown position="bottom" align="end">
    <Dropdown.Trigger>
        <Profile avatar="/img/demo/user.png" name="Olivia Martin" />
    </Dropdown.Trigger>

    <Dropdown.Menu>
        <Dropdown.Item href="#" icon={UserIcon}>Account</Dropdown.Item>
        <Dropdown.Item href="#" icon={BuildingStorefrontIcon}>Profile</Dropdown.Item>
        <Dropdown.Item href="#" icon={CreditCardIcon}>Billing</Dropdown.Item>
        <Dropdown.Item href="#" icon={ArrowRightStartOnRectangleIcon}>Logout</Dropdown.Item>
        <Dropdown.Item href="#" icon={TrashIcon} variant="danger">Delete</Dropdown.Item>
    </Dropdown.Menu>
</Dropdown>
```

## Positioning

Customize the position of the dropdown menu via the `position` and `align` props. You can first pass the base position: `top`, `bottom`, `left`, and `right`, then an alignment modifier like `start`, `center`, or `end`.

```tsx
<Dropdown position="top" align="start">

{/* More positions... */}
<Dropdown position="right" align="center">
<Dropdown position="bottom" align="center">
<Dropdown position="left" align="end">
```

## Offset & gap

Customize the offset/gap of the dropdown menu via the `offset` and `gap` props. These properties accept values in pixels.

```tsx
<Dropdown offset={-15} gap={2}>
```

## Keyboard hints

Add keyboard shortcut hints to menu items to teach users how to navigate your app faster.

```tsx
<Dropdown>
    <Dropdown.Trigger>
        <Button iconTrailing={ChevronDownIcon}>Options</Button>
    </Dropdown.Trigger>

    <Dropdown.Menu>
        <Dropdown.Item icon={PencilSquareIcon} kbd="⌘S">Save</Dropdown.Item>
        <Dropdown.Item icon={DocumentDuplicateIcon} kbd="⌘D">Duplicate</Dropdown.Item>
        <Dropdown.Item icon={TrashIcon} variant="danger" kbd="⌘⌫">Delete</Dropdown.Item>
    </Dropdown.Menu>
</Dropdown>
```

## Checkbox items

Select one or many menu options.

```tsx
<Dropdown>
    <Dropdown.Trigger>
        <Button iconTrailing={ChevronDownIcon}>Permissions</Button>
    </Dropdown.Trigger>

    <Dropdown.Menu>
        <Dropdown.Checkbox defaultChecked>Read</Dropdown.Checkbox>
        <Dropdown.Checkbox defaultChecked>Write</Dropdown.Checkbox>
        <Dropdown.Checkbox>Delete</Dropdown.Checkbox>
    </Dropdown.Menu>
</Dropdown>
```

## Radio items

Select a single menu option.

```tsx
<Dropdown>
    <Dropdown.Trigger>
        <Button iconTrailing={ChevronDownIcon}>Sort by</Button>
    </Dropdown.Trigger>

    <Dropdown.Menu>
        <Dropdown.RadioGroup defaultValue="latest">
            <Dropdown.Radio value="latest">Latest activity</Dropdown.Radio>
            <Dropdown.Radio value="created">Date created</Dropdown.Radio>
            <Dropdown.Radio value="popular">Most popular</Dropdown.Radio>
        </Dropdown.RadioGroup>
    </Dropdown.Menu>
</Dropdown>
```

## Groups

Visually group related menu items with a separator line.

```tsx
<Dropdown>
    <Dropdown.Trigger>
        <Button iconTrailing={ChevronDownIcon}>Options</Button>
    </Dropdown.Trigger>

    <Dropdown.Menu>
        <Dropdown.Item>View</Dropdown.Item>
        <Dropdown.Item>Transfer</Dropdown.Item>

        <Dropdown.Separator />

        <Dropdown.Item>Publish</Dropdown.Item>
        <Dropdown.Item>Share</Dropdown.Item>

        <Dropdown.Separator />

        <Dropdown.Item variant="danger">Delete</Dropdown.Item>
    </Dropdown.Menu>
</Dropdown>
```

## Groups with headings

Group options under headings to make them more discoverable.

```tsx
<Dropdown>
    <Dropdown.Trigger>
        <Button iconTrailing={ChevronDownIcon}>Options</Button>
    </Dropdown.Trigger>

    <Dropdown.Menu>
        <Dropdown.Group heading="Account">
            <Dropdown.Item>Profile</Dropdown.Item>
            <Dropdown.Item>Permissions</Dropdown.Item>
        </Dropdown.Group>

        <Dropdown.Group heading="Billing">
            <Dropdown.Item>Transactions</Dropdown.Item>
            <Dropdown.Item>Payouts</Dropdown.Item>
            <Dropdown.Item>Refunds</Dropdown.Item>
        </Dropdown.Group>

        <Dropdown.Item>Logout</Dropdown.Item>
    </Dropdown.Menu>
</Dropdown>
```

## Submenus

Nest submenus for more condensed menus.

```tsx
<Dropdown>
    <Dropdown.Trigger>
        <Button iconTrailing={ChevronDownIcon}>Options</Button>
    </Dropdown.Trigger>

    <Dropdown.Menu>
        <Dropdown.Submenu heading="Sort by">
            <Dropdown.RadioGroup defaultValue="name">
                <Dropdown.Radio value="name">Name</Dropdown.Radio>
                <Dropdown.Radio value="date">Date</Dropdown.Radio>
                <Dropdown.Radio value="popularity">Popularity</Dropdown.Radio>
            </Dropdown.RadioGroup>
        </Dropdown.Submenu>

        <Dropdown.Submenu heading="Filter">
            <Dropdown.Checkbox defaultChecked>Draft</Dropdown.Checkbox>
            <Dropdown.Checkbox defaultChecked>Published</Dropdown.Checkbox>
            <Dropdown.Checkbox>Archived</Dropdown.Checkbox>
        </Dropdown.Submenu>

        <Dropdown.Separator />

        <Dropdown.Item variant="danger">Delete</Dropdown.Item>
    </Dropdown.Menu>
</Dropdown>
```

## Keep open

Menus typically close when an item is clicked, however, you can add the `keepOpen` prop to the `Dropdown.Menu` component to keep it open.

```tsx
<Dropdown>
    <Dropdown.Trigger>
        <Button iconTrailing={ChevronDownIcon}>Filter</Button>
    </Dropdown.Trigger>

    <Dropdown.Menu keepOpen>
        <Dropdown.Checkbox defaultChecked>Draft</Dropdown.Checkbox>
        <Dropdown.Checkbox defaultChecked>Published</Dropdown.Checkbox>
        <Dropdown.Checkbox>Archived</Dropdown.Checkbox>
    </Dropdown.Menu>
</Dropdown>
```

If you only want the menu to stay open for specific items, add `keepOpen` directly to `Dropdown.Item`, `Dropdown.Checkbox`, `Dropdown.Radio`, `Dropdown.RadioGroup`, or `Dropdown.Submenu`.

```tsx
<Dropdown>
    <Dropdown.Trigger>
        <Button iconTrailing={ChevronDownIcon}>Filters</Button>
    </Dropdown.Trigger>

    <Dropdown.Menu>
        <Dropdown.Checkbox keepOpen defaultChecked>Draft</Dropdown.Checkbox>
        <Dropdown.Checkbox keepOpen defaultChecked>Published</Dropdown.Checkbox>
        <Dropdown.Checkbox keepOpen>Archived</Dropdown.Checkbox>
        <Dropdown.Separator />
        <Dropdown.Item variant="danger">Clear</Dropdown.Item>
    </Dropdown.Menu>
</Dropdown>
```

## Reference

### Dropdown

**Prop:**

- `position` - Position of the dropdown menu. Options: `top`, `right`, `bottom` (default), `left`.
- `align` - Alignment of the dropdown menu. Options: `start`, `center`, `end`. Default: `start`.
- `offset` - Offset in pixels from the trigger element. Default: `0`.
- `gap` - Gap in pixels between trigger and menu. Default: `5`.
- `hover` - If `true`, opens the menu on hover in addition to click. Default: `false`.
- `disabled` - If `true`, prevents the trigger from opening the menu.
- `open` - Controlled open state of the menu. Pair with `onOpenChange`.
- `defaultOpen` - Initial open state of the menu when uncontrolled.
- `onOpenChange` - Callback fired when the menu opens or closes, receiving the new open state and `eventDetails`, whose `reason` tells why.

**Attribute:**

- `data-flux-dropdown` - Applied to the root element for styling and identification.
- `data-open` - Applied to the dropdown and its trigger while the menu is open.

### Dropdown.Trigger

Wraps the element that opens the menu, such as a Flux `Button` or `Profile`, and merges the trigger's props into it. It adds no element of its own.

**Prop:**

- `children` - One element that opens the menu, such as a Flux `Button`. It must forward its props and ref, as every Flux component does.
- `render` - Base UI's form of the same, for those who prefer it: the element to render as the trigger, or a function that receives the props and returns one. With `render`, the children become that element's content.

### Dropdown.Menu

A complex menu component that supports keyboard navigation, submenus, checkboxes, and radio buttons.

**Prop:**

- `keepOpen` - Prevents the menu from closing when any item inside of it is clicked.

**Attribute:**

- `data-flux-menu` - Applied to the root element for styling and identification.

### Dropdown.Item

**Prop:**

- `href` - The URL to link to. When set, the item renders as an `a` tag. An in-app URL navigates through the link registered in `FluxProvider`, and with Inertia, a POST route such as `logout()` or `method="post"` renders a button.
- `target` - Where the link opens, such as `_blank`. Pass it with `href`, as you would `rel` and `download`. A `target` other than `_self` or a `download` renders a plain `a` tag.
- `render` - Element to render instead of the item (such as a router link with props `href` can't carry), with the item's props merged in.
- `icon` - Icon to display at the start of the item: a component, such as `PlusIcon` from `@fluxui-dev/react/icons`, or an element, such as `<img />`, rendered as is.
- `iconTrailing` - Icon to display at the end of the item: a component or an element.
- `iconVariant` - Variant of the icon. Options: `outline`, `solid`, `mini`, `micro`.
- `kbd` - Keyboard shortcut hint displayed at the end of the item.
- `suffix` - Text displayed at the end of the item.
- `variant` - Visual style of the item. Options: `default`, `danger`.
- `disabled` - If `true`, prevents interaction with the menu item.
- `keepOpen` - Prevents the menu from closing when this item is selected.

**Attribute:**

- `data-flux-menu-item` - Applied to the root element for styling and identification.
- `data-highlighted` - Applied when the item is highlighted by the pointer or keyboard.
- `data-disabled` - Applied when the item is disabled.

### Dropdown.Submenu

**Prop:**

- `heading` - Text displayed as the submenu heading.
- `icon` - Icon to display at the start of the submenu: a component or an element.
- `iconTrailing` - Icon to display at the end of the submenu, in place of the chevron: a component or an element.
- `iconVariant` - Variant of the icon. Options: `outline`, `solid`, `mini`, `micro`.
- `disabled` - If `true`, prevents the submenu from opening.
- `keepOpen` - Prevents the submenu from closing when any item inside of it is selected.

**CSS:**

- `className` - CSS classes applied to the submenu's menu (not its trigger item).

### Dropdown.Separator

A horizontal line that separates menu items.

### Dropdown.Group

Groups related menu items between separators, with an optional heading.

**Prop:**

- `heading` - Heading text displayed above the group's items.

### Dropdown.Heading

A label displayed above a set of menu items without wrapping them in a group.

### Dropdown.CheckboxGroup

Groups multiple `Dropdown.Checkbox` items into a single array of checked values.

**Prop:**

- `value` - Controlled array of checked checkbox values.
- `defaultValue` - Initial array of checked values when uncontrolled.
- `onValueChange` - Callback fired with the new array of values and the toggled checkbox's `eventDetails` when a checkbox is toggled.

### Dropdown.Checkbox

**Prop:**

- `checked` - Controlled checked state of the checkbox.
- `defaultChecked` - Initial checked state of the checkbox when uncontrolled.
- `onCheckedChange` - Callback fired with the new checked state and `eventDetails` when the checkbox is toggled.
- `value` - Value identifying the checkbox within a `Dropdown.CheckboxGroup`.
- `label` - Label text displayed when the checkbox has no children.
- `iconTrailing` - Icon to display at the end of the item: a component or an element.
- `iconVariant` - Variant of the check mark and the trailing icon: `outline`, `solid`, `mini`, or `micro`. Default: `mini`.
- `disabled` - If `true`, prevents interaction with the checkbox.
- `keepOpen` - Prevents the menu from closing when this checkbox is selected.

**Attribute:**

- `data-highlighted` - Applied when the checkbox is highlighted by the pointer or keyboard.
- `data-checked` - Applied when the checkbox is checked.

### Dropdown.RadioGroup

**Prop:**

- `value` - Controlled value of the selected radio item.
- `defaultValue` - Initial selected value when uncontrolled.
- `onValueChange` - Callback fired with the new value and `eventDetails` when a radio item is selected.
- `keepOpen` - Prevents the menu from closing when any radio button in this group is selected.

### Dropdown.Radio

**Prop:**

- `value` - Value this radio selects in its `Dropdown.RadioGroup`. Required.
- `label` - Label text displayed when the radio has no children.
- `iconTrailing` - Icon to display at the end of the item: a component or an element.
- `iconVariant` - Variant of the check mark and the trailing icon: `outline`, `solid`, `mini`, or `micro`. Default: `mini`.
- `disabled` - If `true`, prevents interaction with the radio button.
- `keepOpen` - Prevents the menu from closing when this radio button is selected.

**Attribute:**

- `data-highlighted` - Applied when the radio button is highlighted by the pointer or keyboard.
- `data-checked` - Applied when the radio button is selected.
