Svelte Dropdown - Bitblue

Get started with the dropdown component to show a list of menu items when clicking on the trigger element based on multiple layouts, styles, and placements

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) before Dropdown. With simple, the previous sibling is used as the trigger; otherwise set triggeredBy to a CSS selector.
  • Structure menus with DropdownHeader, DropdownGroup, DropdownDivider, and DropdownItem.
  • DropdownItem renders as a link when href is set, a button when onclick is set, otherwise a plain item.

Props

DropdownProps (extends PopperProps)

PropTypeDefaultDescription
simpleboolean—Auto-bind previous sibling as trigger
activeUrlstring—Highlight matching item href
isOpenbooleanfalseBindable open state
triggeredBystring—CSS selector for trigger
triggerhover | clickclick (dropdown)Open behavior
triggerDelaynumber—Hover delay (ms)
placementFloating UI Placement—Menu position
strategyFloating UI Strategy—absolute / fixed
offsetnumber—Distance from trigger
arrowboolean—Show arrow
arrowClassstring—Arrow classes
referencestring—Alternate reference selector
middlewaresMiddleware[]—Floating UI middleware
yOnlyboolean—Constrain movement to Y
transitionTransitionFunc—Open/close transition
transitionParamsParamsType—Transition params
onbeforetoggle(ev) => void—Before open/close
ontoggle(ev) => void—After open/close
childrenSnippet—Menu content
classClassValue—Root classes

DropdownItemProps

PropTypeDefaultDescription
childrenSnippet—Item content
hrefstring—Render as anchor
onclickFunction—Render as button
aClassClassValue—Anchor/button classes
activeClassClassValue—Active item classes
liClassClassValue—Wrapping <li> classes
classClassValue—Item classes
…restanchor / button / div attrs—Forwarded to the interactive element

DropdownHeaderProps

PropTypeDefaultDescription
childrenSnippet—Header content
…restdiv attrs—Forwarded to the header

DropdownGroupProps

PropTypeDefaultDescription
childrenSnippet—Grouped items
…restul attrs—Forwarded to the list

DropdownDividerProps

PropTypeDefaultDescription
…restdiv attrs—Horizontal rule separator

Events

EventDescription
onbeforetogglePopper lifecycle before toggle (TriggeredToggleEvent)
ontogglePopper lifecycle after toggle

© 2026 Bitblue is a registered trademark. All Rights Reserved.