Skip to content

Dropdown

Introduction

The Dropdown component displays a floating menu anchored to a trigger element. It supports both structured item arrays and flexible children composition, making it ideal for actions, navigation, filtering, and contextual menus. Dropdowns can be triggered by click, hover, or context menu, and support icons, dividers, disabled items, custom placement, and more.

Import

ts
import { Dropdown } from '@odyssee/components';

LiveCodeEditor examples

Basic Dropdown

Code Éditable
Résultat
Code Éditable
Résultat
Code Éditable
Résultat

Placement Variants

Code Éditable
Résultat

Trigger Types

Code Éditable
Résultat

Disabled Items

Code Éditable
Résultat

User Account Menu Example

Code Éditable
Résultat

Props

PropTypeDefaultDescription
triggerHTMLElement | stringThe element or label that triggers the dropdown.
triggerClassNamestringAdditional CSS classes for the trigger element.
itemsDropdown.Item[]Array of menu items (see below for shape).
childrenHTMLElement | HTMLElement[]Alternative to items: custom dropdown content (use with Dropdown.Item and Dropdown.Divider).
placement"top" | "top-start" | "top-end" | "bottom" | "bottom-start" | "bottom-end"
"left" | "left-start" | "left-end" | "right" | "right-start" | "right-end" | "auto"
"bottom"Position of the dropdown relative to the trigger.
strategy"fixed" | "absolute""fixed"Positioning strategy for the dropdown menu.
offsetnumber10Offset (in px) between the trigger and the dropdown.
flipbooleantrueWhether to flip the dropdown to stay in the viewport.
scope"parent" | "window""parent"Positioning scope for the dropdown.
triggerType"click" | "hover" | "contextmenu""click"How the dropdown is triggered.
autoCloseboolean | "inside" | "outside"trueControls auto-close behavior: on any click, only inside, or only outside.
closeOnSelectbooleantrueWhether to close the dropdown when an item is selected.
hasAutofocusbooleantrueWhether the dropdown menu should autofocus when opened.
isOpenboolean | Signal<boolean>Controlled open state (optional).
menuClassNamestringAdditional CSS classes for the dropdown menu container.
onOpen() => voidCallback fired when the dropdown opens.
onClose() => voidCallback fired when the dropdown closes.
onSelect(value: string | number) => voidCallback fired when an item with a value is selected.
classNamestringAdditional CSS classes for the dropdown container.
idstringauto-generatedID for the dropdown container.
...restAny other props are spread to the root container.
  • label: string | HTMLElement — The text or element for the menu item.
  • value: string | number (optional) — Value passed to onSelect.
  • icon: HTMLElement | string (optional) — Icon to display before the label.
  • href: string (optional) — If provided, renders as a link.
  • disabled: boolean (optional) — If true, the item is disabled.
  • isDivider: boolean (optional) — If true, renders a divider instead of an item.
  • onClick: () => void (optional) — Click handler for the item.
  • className: string (optional) — Additional classes for the item.

Children composition

You can also use the Dropdown.Item and Dropdown.Divider subcomponents as children for full flexibility.

tsx
<Dropdown trigger="Menu">
  <Dropdown.Item>Newsletter</Dropdown.Item>
  <Dropdown.Item href="/purchases">Purchases</Dropdown.Item>
  <Dropdown.Divider />
  <Dropdown.Item icon={<Icon />}>Downloads</Dropdown.Item>
</Dropdown>

Implementation notes

  • Dropdown supports both a structured items array and flexible children composition.
  • The menu is rendered with Preline and Floating UI for positioning and accessibility.
  • Supports custom triggers (button, avatar, etc.) and custom menu content.
  • Placement, offset, flipping, and auto-close are fully configurable.
  • Menu items can be links, buttons, disabled, have icons, or be dividers.
  • The dropdown closes automatically on outside click, on select (if enabled), or Escape key.
  • Keyboard navigation and ARIA roles are handled for accessibility.

Accessibility

  • The dropdown menu uses role="menu" and proper ARIA attributes for assistive technologies.
  • Keyboard navigation is supported: arrow keys to move, Enter/Space to select, Escape to close.
  • The trigger element should be focusable (button, link, etc.).
  • Menu closes on outside click or Escape key.
  • Ensure all interactive items are accessible via keyboard and screen readers.

Best practices

  • Use concise, clear labels for menu items.
  • Group related actions with dividers for clarity.
  • Use icons to visually distinguish actions when appropriate.
  • Avoid placing destructive actions (like "Delete") next to frequent actions.
  • For navigation, use href for menu items to ensure proper link semantics.
  • Keep menus short and relevant to the context.

Released under the MIT License.