The dropdown component can be used to show a list of menu items when clicking on an element such as a button and hiding it when clicking outside of the triggering element.
Setup
<script>
import { Dropdown, DropdownItem, DropdownDivider, DropdownHeader } from '.';
</script> Examples
If you want to show a dropdown menu when clicking on an element add the Dropdown and DropdownItem components.
<script>
import { Button, Dropdown, DropdownItem } from '.';
</script>
<Button>Dropdown button</Button>
<Dropdown simple>
<DropdownItem>Dashboard</DropdownItem>
<DropdownItem>Settings</DropdownItem>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Sign out</DropdownItem>
</Dropdown> Adding links and active class
Utilize the href prop within the DropdownItem component to incorporate a hyperlink. To initiate the application of the active class, include the activeUrl prop within the Dropdown component:
<script>
import { Button, Dropdown, DropdownItem } from '.';
import { page } from '$app/state';
let activeUrl = $derived(page.url.pathname);
</script>
<Button>Dropdown button</Button>
<Dropdown {activeUrl} simple>
<DropdownItem href="/">Home</DropdownItem>
<DropdownItem href="/docs/components/dropdown">Dropdown</DropdownItem>
<DropdownItem href="/docs/components/accordion">Accordion</DropdownItem>
<DropdownItem href="/docs/components/alert">Alert</DropdownItem>
</Dropdown> To change the active class, use the activeClass prop:
<script>
import { Button, Dropdown, DropdownItem } from '.';
import { page } from '$app/state';
let activeUrl = $derived(page.url.pathname);
let activeClass =
'text-green-500 dark:text-green-300 hover:text-green-700 dark:hover:text-green-500';
let routes = [
{title: "Home", path: "/"},
{title: "Dropdown", path: "/docs/components/dropdown"}
{title: "Avatar", path: "/docs/components/avatar"}
{title: "Alert", path: "/docs/components/alert"}
]
</script>
<Button>Dropdown button</Button>
<Dropdown simple>
{#each routes as { path, title }}
<DropdownItem activeClass={activeUrl === path && activeClass} href={path}>{title}</DropdownItem>
{/each}
</Dropdown> Dropdown divider
You can use the DropdownDivider component to add separate elements inside the dropdown menu. Note that you have a natural divider between main content of the Dropdown and its header and footer.
<script>
import { Button, Dropdown, DropdownItem, DropdownDivider } from '.';
</script>
<Button>Dropdown button</Button>
<Dropdown simple>
<DropdownItem>Dashboard</DropdownItem>
<DropdownDivider />
<DropdownItem>Settings</DropdownItem>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Separated link</DropdownItem>
</Dropdown> Dropdown header
Use this example to show extra information outside of the list of menu items inside the dropdown.
<script>
import { Button, Dropdown, DropdownItem, DropdownGroup, DropdownHeader } from '.';
</script>
<Button>Dropdown button</Button>
<Dropdown>
<DropdownHeader>
<span class="block text-sm text-gray-900 dark:text-white">Bonnie Green</span>
<span class="block truncate text-sm font-medium">name@flowbite.com</span>
</DropdownHeader>
<DropdownGroup>
<DropdownItem>Dashboard</DropdownItem>
<DropdownItem>Settings</DropdownItem>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Sign out</DropdownItem>
</DropdownGroup>
</Dropdown> Multi-level dropdown
Use this example to enable multi-level dropdown menus by adding stacked elements inside of each other.
<script>
import { Button, Dropdown, DropdownItem } from '.';
</script>
<Button>Dropdown button</Button>
<Dropdown simple>
<DropdownItem>Dashboard</DropdownItem>
<DropdownItem class="flex items-center justify-between" classes={{ anchor: 'flex' }}>
Dropdown<iconify-icon
icon="iconamoon:arrow-right-2-bold"
class="text-lg text-primary-700 dark:text-white"
></iconify-icon>
</DropdownItem>
<Dropdown simple placement="right-start">
<DropdownItem>Overview</DropdownItem>
<DropdownItem>My downloads</DropdownItem>
<DropdownItem>Billing</DropdownItem>
</Dropdown>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Sign out</DropdownItem>
</Dropdown> A11y
Use Tab and Shift+Tab to navigate between buttons or links in the dropdown. Press ESC to close it.
Programatic open/close
When you want to control your dropdown open status via javascript code you can bind to open property.
Current dropdown state: Closed
<script lang="ts">
import { Button, Dropdown, DropdownItem } from '.';
let isOpen = $state(false);
</script>
<p>Current dropdown state: {isOpen ? 'Open' : 'Closed'}</p>
<Button onclick={() => (isOpen = !isOpen)}>Toggle Btn</Button>
<Dropdown bind:isOpen simple>
<DropdownItem>Dashboard</DropdownItem>
<DropdownItem>Settings</DropdownItem>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Sign out</DropdownItem>
</Dropdown> Menu icon
Use the menu icon trigger element on components such as cards as an alternative element to the button.
<script>
import { Dropdown, DropdownItem } from '.';
import { DotsHorizontalOutline, DotsVerticalOutline } from '.';
</script>
<iconify-icon icon="garden:overflow-vertical-fill-16" class="dot-menu"></iconify-icon>
<Dropdown simple triggeredBy=".dots-menu">
<DropdownItem>Dashboard</DropdownItem>
<DropdownItem>Settings</DropdownItem>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Sign out</DropdownItem>
</Dropdown> Placement
You can also use the placement={top|right|bottom|left} options to choose the placement of the dropdown menu. By default the positioning is set to the bottom side of the button.
<script>
import { Button, Dropdown, DropdownItem } from '.';
</script>
<Dropdown simple placement="top" triggeredBy="#top-dd">
<DropdownItem>Dashboard</DropdownItem>
<DropdownItem>Settings</DropdownItem>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Sign out</DropdownItem>
</Dropdown>
<Dropdown simple placement="bottom" triggeredBy="#bottom-dd">
<DropdownItem>Dashboard</DropdownItem>
<DropdownItem>Settings</DropdownItem>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Sign out</DropdownItem>
</Dropdown>
<Dropdown simple placement="right" triggeredBy="#right-dd">
<DropdownItem>Dashboard</DropdownItem>
<DropdownItem>Settings</DropdownItem>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Sign out</DropdownItem>
</Dropdown>
<Dropdown simple placement="left" triggeredBy="#left-dd">
<DropdownItem>Dashboard</DropdownItem>
<DropdownItem>Settings</DropdownItem>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Sign out</DropdownItem>
</Dropdown>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div id="placements" class="my-8 flex h-96 flex-col items-center justify-center gap-2">
<Button id="top-dd">Dropdown top</Button>
<div class="flex space-x-2 rtl:space-x-reverse">
<Button id="left-dd">Dropdown left</Button>
<Button id="right-dd">Dropdown right</Button>
</div>
<Button id="bottom-dd">Dropdown bottom</Button>
</div> Transition
Since the Dropdown component extends Popper, it also supports the transition and transitionParams props for customizing animations.
<script>
import { Button, Dropdown, DropdownItem } from '.';
import { scale, blur } from 'svelte/transition';
</script>
<Button>Scale</Button>
<Dropdown simple transition={scale} transitionParams={{ duration: 800 }}>
<DropdownItem>Dashboard</DropdownItem>
<DropdownItem>Settings</DropdownItem>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Sign out</DropdownItem>
</Dropdown>
<Button>Blur</Button>
<Dropdown simple transition={blur} transitionParams={{ duration: 800 }}>
<DropdownItem>Dashboard</DropdownItem>
<DropdownItem>Settings</DropdownItem>
<DropdownItem>Earnings</DropdownItem>
<DropdownItem>Sign out</DropdownItem>
</Dropdown> Composition notes
- Place a trigger (often
Button) beforeDropdown. Withsimple, the previous sibling is used as the trigger; otherwise settriggeredByto a CSS selector. - Structure menus with
DropdownHeader,DropdownGroup,DropdownDivider, andDropdownItem. DropdownItemrenders as a link whenhrefis set, a button whenonclickis set, otherwise a plain item.
Props
DropdownProps (extends PopperProps)
| Prop | Type | Default | Description |
|---|---|---|---|
simple | boolean | — | Auto-bind previous sibling as trigger |
activeUrl | string | — | Highlight matching item href |
isOpen | boolean | false | Bindable open state |
triggeredBy | string | — | CSS selector for trigger |
trigger | hover | click | click (dropdown) | Open behavior |
triggerDelay | number | — | Hover delay (ms) |
placement | Floating UI Placement | — | Menu position |
strategy | Floating UI Strategy | — | absolute / fixed |
offset | number | — | Distance from trigger |
arrow | boolean | — | Show arrow |
arrowClass | string | — | Arrow classes |
reference | string | — | Alternate reference selector |
middlewares | Middleware[] | — | Floating UI middleware |
yOnly | boolean | — | Constrain movement to Y |
transition | TransitionFunc | — | Open/close transition |
transitionParams | ParamsType | — | Transition params |
onbeforetoggle | (ev) => void | — | Before open/close |
ontoggle | (ev) => void | — | After open/close |
children | Snippet | — | Menu content |
class | ClassValue | — | Root classes |
DropdownItemProps
| Prop | Type | Default | Description |
|---|---|---|---|
children | Snippet | — | Item content |
href | string | — | Render as anchor |
onclick | Function | — | Render as button |
aClass | ClassValue | — | Anchor/button classes |
activeClass | ClassValue | — | Active item classes |
liClass | ClassValue | — | Wrapping <li> classes |
class | ClassValue | — | Item classes |
…rest | anchor / button / div attrs | — | Forwarded to the interactive element |
DropdownHeaderProps
| Prop | Type | Default | Description |
|---|---|---|---|
children | Snippet | — | Header content |
…rest | div attrs | — | Forwarded to the header |
DropdownGroupProps
| Prop | Type | Default | Description |
|---|---|---|---|
children | Snippet | — | Grouped items |
…rest | ul attrs | — | Forwarded to the list |
DropdownDividerProps
| Prop | Type | Default | Description |
|---|---|---|---|
…rest | div attrs | — | Horizontal rule separator |
Events
| Event | Description |
|---|---|
onbeforetoggle | Popper lifecycle before toggle (TriggeredToggleEvent) |
ontoggle | Popper lifecycle after toggle |