Directory
- Welcome
Dropdown Component
The Dropdown component creates interactive menus that expand and collapse with smooth animations. It’s perfect for navigation menus, settings panels, and any interface that needs to show and hide content on demand.
Key Features
-
Smooth Animations: Polished expand/collapse transitions
-
Flexible Content: Supports both simple lists and custom content
-
Click Outside: Optional automatic closing when clicking outside
-
Keyboard Support: Full keyboard navigation and accessibility
-
Customizable: Multiple styling and behavior options
Basic Usage
Create a simple dropdown menu with a list of items:
<Dropdown items={[ { label: "Docs", href: "/docs/" }, { label: "Blog", href: "/blog/" }, { label: "Home", href: "/" } ]}> Menu</Dropdown>
Selection State
The dropdown maintains selection state and supports pre-selected values and disabled options.
<Dropdown items={[ { label: 'Default Option', value: '1', active: true }, { label: 'Disabled Option', value: '2', disabled: true }, { label: 'Regular Option', value: '3' }, ]}> With States</Dropdown>
Overlay
The dropdown can be displayed as an overlay or menu inline with the trigger button.
<Dropdown overlay={false} class="mb-8" items={[{ label: 'Profile' }, { label: 'Settings' }, { label: 'Logout' }]}> Menu</Dropdown>
Custom Event Listeners
The component emits custom events for selection changes:
Waiting for events to be emitted… (Click on the menu to see the events)
// Listen for selection changesdocument.querySelector('dropdown-wrapper') .addEventListener(DropdownEventNames.Selected, (event) => { const { dropdownId, active } = event.detail; // Handle selection change });
Dropdown Props API
Prop | Type | Default | Description |
---|---|---|---|
buttonProps | ButtonProps | Props for the trigger button | |
clickOutsideToClose | boolean | true | Close dropdown when clicking outside |
contentClass | string | Additional classes for dropdown content | |
itemProps | ButtonProps | Props applied to each menu item | |
items | Array<Item> | [] | List of dropdown menu items |
open | boolean | false | Control initial open state |
overlay | boolean | true | Display as floating menu |
preferOnScreen | boolean | true | Shows the dropdown above the trigger button if it would extend beyond the viewport |
showIcon | boolean | true | Show/hide the chevron icon |
showLines | boolean | false | Show vertical lines for visual hierarchy |
interface Item { label?: string; href?: string; id?: string; disabled?: boolean; active?: boolean;}
Accessibility Features
Keyboard Navigation
- Enter/Space to toggle
- Tab for focus management
- Escape to close
ARIA Attributes
aria-expanded
statearia-controls
relationships- Proper focus management
Common Applications
Limited Time Launch Sale
Build professional dropdown menus with our pre-built component. Get the AstroJS starter template now and create engaging user interfaces instantly.
GET 60% OFF!