Skip to content

Latest commit

 

History

History
1925 lines (1755 loc) · 124 KB

File metadata and controls

1925 lines (1755 loc) · 124 KB
layout docs
title Tailwind CSS Dropdown - Flowbite
description 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
group components
toc true
requires_js true
previous Drawer
previousLink components/drawer/
next Footer
nextLink components/footer/

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 focusing outside of the triggering element.

Make sure to include <a href="{{< ref "getting-started/quickstart" >}}">Flowbite's JavaScript file inside your project to enable dropdowns.

Dropdown example

If you want to show a dropdown menu when clicking on an element make sure that you add the data attribute data-dropdown-toggle="dropdownId" to the element (ie. a button) that will trigger the dropdown menu.

The dropdownId is the id of the dropdown menu element.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="300" >}} Dropdown button

{{< /example >}}

Dropdown hover

Use the data-dropdown-trigger="{hover|click}" data attribute options to set whether the dropdown should be shown when hovering or clicking on the trigger element (ie. button).

There's a 300ms default delay when showing or hiding the dropdown due to UI/UX reasons and how it may affect the interaction with other components on the page. Generally, we recommend using the click method.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="300" >}} Dropdown button

{{< /example >}}

Delay duration

You can use the data-dropdown-delay={milliseconds} data attribute to set they delay on when to show or hide the dropdown menu when using hover. You may want to use this depending on how the users interact with your interface. In this example we add 500 milliseconds instead of the default 300.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="300" >}} Dropdown button

{{< /example >}}

Dropdown divider

You can use the divide-y divide-gray-100 classes to add separate elements inside the dropdown menu.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="320" >}} Dropdown button

{{< /example >}}

Dropdown header

Use this example to show extra information outside of the list of menu items inside the dropdown.

{{< example class="flex justify-center " github="components/dropdowns.md" show_dark=true iframeHeight="520" >}} Dropdown button

{{< /example >}}

Multi-level dropdown

Use this example to enable multi-level dropdown menus by adding stacked elements inside of each other.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="350" >}} Dropdown button

{{< /example >}}

Dropdown with checkbox

Add multiple checkbox elements inside your dropdown menu to enable more advanced input interaction.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="290" >}} Dropdown button

  • Default checkbox
  • Checked state
  • Default checkbox
{{< /example >}}

Background hover

Use this example to update the background color of a menu item when using a list of checkbox elements.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="300" >}} Dropdown button

  • Default checkbox
  • Checked state
  • Default checkbox
{{< /example >}}

Helper text

Add an extra helper text to each checkbox element inside the dropdown menu list with this example.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="380" >}} Dropdown button

  • Enable notifications

    Some helpful instruction goes over here.

  • Enable 2FA auth

    Some helpful instruction goes over here.

  • Subscribe newsletter

    Some helpful instruction goes over here.

{{< /example >}}

Dropdown with radio

Enable more advanced interaction with your users by adding radio input elements inside the dropdown menu.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="250" >}} Dropdown radio

  • Default radio
  • Checked radio
  • Default radio
{{< /example >}}

Background hover

Use this example to update the background color when hovering over a menu item when using radio elements.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="280" >}} Dropdown button

  • Default radio
  • Checked radio
  • Default radio
{{< /example >}}

Helper text

This example can be used to add an extra helper text inside of each radio element from the dropdown menu.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="380" >}}

Dropdown button
  • Individual

    Some helpful instruction goes over here.

  • Company

    Some helpful instruction goes over here.

  • Non profit

    Some helpful instruction goes over here.

{{< /example >}}

Dropdown with toggle switch

Show a list of toggle switch elements inside the dropdown menu to enable a yes or no type of choice.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="280" >}} Dropdown button

  • Enable notifications
  • Enable 2FA authentication
  • Subscribe to newsletter

{{< /example >}}

Dropdown with scrolling

This example can be used when you want to show a long list of items that won't affect the height of the dropdown menu by enabling a scrolling behaviour.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="380" >}} Dropdown button

{{< /example >}}

Scrollable checkboxes

Use this example to show multiple dropdown items in a scrollable container with checkboxes.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="380" >}} Dropdown button

  • Jese image Jese Leos
  • Robert image Robert Gough
  • Bonnie image Bonnie Green
  • Leslie image Leslie Livingston
  • Michael image Michael Gough
  • Joseph image Joseph Mcfall
  • Roberta image Roberta Casas
  • Neil image Neil Sims
Delete user
{{< /example >}}

Dropdown with search

Use this example if you want to add a search bar inside the dropdown menu to be able to filter through a long list of menu items with scrolling behaviour.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="440" >}} Dropdown button

Search
  • Jese image Jese Leos
  • Robert image Robert Gough
  • Bonnie image Bonnie Green
  • Leslie image Leslie Livingston
  • Michael image Michael Gough
  • Joseph image Joseph Mcfall
  • Roberta image Roberta Casas
  • Neil image Neil Sims
Delete user
{{< /example >}}

Menu icon

Use the menu icon trigger element on components such as cards as an alternative element to the button.

{{< example class="flex justify-center space-x-4 rtl:space-x-reverse" github="components/dropdowns.md" show_dark=true iframeHeight="320" >}}

{{< /example >}}

Notification bell

Use this example to show a list of notifications inside your application by providing more detailed information such as the user avatar, content and time of notification triggered by a notification bell icon.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="660" >}}

{{< /example >}}

User avatar

This example can be used to show a list of menu items and options when a user is logged into your application.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="520" >}} Open user menu user photo

{{< /example >}}

Avatar with name

Use this example to also show the name or email of the user next to the avatar for the dropdown menu.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="520" >}} Open user menu user photo Joseph McFall

{{< /example >}}

Dropdown navbar

You can also use the dropdown element inside a navigation bar and add a second level of navigation hierarchy, but make sure to use a button element.

{{< example bodyClass="!p-0" github="components/dropdowns.md" show_dark=true iframeHeight="380" >}}

{{< /example >}}

Dropdown datepicker

Use this example to show a date range picker inside a dropdown menu. Use the data-dropdown-ignore-click-outside-class={class} option to keep the dropdown menu open when interacting with the datepicker component by setting the element's parent class name.

{{< example class="flex justify-center" github="components/dropdowns.md" show_dark=true iframeHeight="520" >}} 31 Nov

- 31 Dev

to
{{< /example >}}

Sizes

The dropdown menus work with buttons of all sizes including smaller or larger ones.

{{< example github="components/dropdowns.md" class="space-x-2 space-y-2" show_dark=true iframeHeight="380" >}} Small dropdown

Large dropdown {{< /example >}}

Placement

You can also use the data-dropdown-placement={top|right|bottom|left} data attribute options to choose the placement of the dropdown menu. By default the positioning is set to the bottom side of the button.

{{< example class="flex flex-wrap space-x-4 justify-center py-48" github="components/dropdowns.md" show_dark=true >}} Dropdown top

Dropdown right Dropdown bottom Dropdown left {{< /example >}}

Double placement

You can combine the placement options by using the top|right|bottom|left-{start|end} values. For example, left-end will position the dropdown on the left bottom, whereas right-end will position it on the right bottom side.

{{< example class="flex flex-wrap justify-center space-x-4" github="components/dropdowns.md" iframeHeight="240" show_dark=true >}} Dropdown left end

Dropdown right end {{< /example >}}

Dropdown offset

Use the offset options on the dropdown component to set the distance and skidding of the menu relative to the trigger element.

Distance

Use the data-dropdown-offset-distance={pixels} data attribute to set the number of pixels you want the dropdown menu to be offset from the trigger element.

{{< example class="flex flex-wrap justify-center" github="components/dropdowns.md" iframeHeight="320" show_dark=true >}} Dropdown button

{{< /example >}}

Skidding

The data-dropdown-offset-skidding={pixels} data attribute can be used to move up or down (or left and right) the dropdown menu along and relative to the trigger element.

{{< example class="flex flex-wrap justify-center" github="components/dropdowns.md" iframeHeight="320" show_dark=true >}} Dropdown button

{{< /example >}}

More examples

For more dropdown examples you can check out the dropdown filter components from Flowbite Blocks.

JavaScript behaviour

The Dropdown class from Flowbite can be used to create an object that will show a dropdown menu relative to the main trigger element (eg. a button) based on the parameters, options, and methods that you provide.

Object parameters

Initialize a Dropdown object with the object parameters such as the main target dropdown menu, a trigger element (eg. a button) and options to set the placement relative to the trigger element.

Parameter Type Required Description
targetElement Element Required Apply the main dropdown menu element as the first parameter of the Dropdown object.
triggerElement Element Required Apply the trigger element, such as a button, which is required to position the dropdown menu and set a click event.
options Object Optional Use the options parameter to set the positioning of the dropdown menu.
instanceOptions Object Optional Object of options that allows you to set a custom ID for the instance that is being added to the Instance Manager and whether to override or not an existing instance.

Options

Use the following options as the third parameter for the Dropdown class to set the placement of the dropdown menu.

Option Type Description
placement String Set the position of the dropdown menu relative to the trigger element choosing from top|right|bottom|left.
triggerType String Set the event type that will trigger the dropdown menu choosing between hover|click|none.
offsetDistance String Set the amount of pixels the dropdown menu should be offset relative to the trigger element on the X horizontal axis.
offsetSkidding String Set the number of pixels the dropdown menu should be offset relative to the trigger element on the Y horizontal axis.
delay Number Set the milliseconds for which the showing or hiding of the dropdown will be delayed for when using the hover trigger type.
ignoreClickOutsideClass String Set a class for one or more elements that when they are clicked should ignore closing the dropdown (ie. offcanvas datepicker).
onHide Function Set a callback function when the dropdown has been hidden.
onShow Function Set a callback function when the dropdown has been shown.
onToggle Function Set a callback function when the dropdown visibility has been toggled.

Methods

Use the methods from the Dropdown object to programmatically show or hide the dropdown menu directly from JavaScript.

Method Description
show() Use this method on the Dropdown object to show the dropdown menu.
hide() Use this method on the Dropdown object to hide the dropdown menu.
toggle() Use this method on the Dropdown object to toggle the visibility of the dropdown menu.
isVisible() Returns true or false based on the visibility of the dropdown.
updateOnShow(callback) Use this method to set a callback function when the dropdown has been shown.
updateOnHide(callback) Use this method to set a callback function when the dropdown has been hidden.
updateOnToggle(callback) Use this method to set a callback function when the dropdown visibility has been changed.

Example

Check out the following JavaScript example to learn how to initialize, set the options, and use the methods for the Dropdown object.

First of all, you need to set the main target element which will be the dropdown menu and the trigger element which will can be a button or a text.

After that, you can also optionally set an options object to set the placement of the dropdown menu and callback functions.

{{< code lang="javascript" file="dropdown.js" icon="file" >}} // set the dropdown menu element const $targetEl = document.getElementById('dropdownMenu');

// set the element that trigger the dropdown menu on click const $triggerEl = document.getElementById('dropdownButton');

// options with default values const options = { placement: 'bottom', triggerType: 'click', offsetSkidding: 0, offsetDistance: 10, delay: 300, ignoreClickOutsideClass: false, onHide: () => { console.log('dropdown has been hidden'); }, onShow: () => { console.log('dropdown has been shown'); }, onToggle: () => { console.log('dropdown has been toggled'); }, };

// instance options object const instanceOptions = { id: 'dropdownMenu', override: true }; {{< /code >}}

Create a new Dropdown object based on the options above.

{{< code lang="javascript" file="dropdown.js" icon="file" >}} import { Dropdown } from 'flowbite';

/*

  • $targetEl: required
  • $triggerEl: required
  • options: optional
  • instanceOptions: optional */ const dropdown = new Dropdown($targetEl, $triggerEl, options, instanceOptions); {{< /code >}}

Use the show and hide methods on the Dropdown object to programmatically show or hide the dropdown menu directly from JavaScript.

{{< code lang="javascript" file="dropdown.js" icon="file" >}} // show the dropdown menu dropdown.show();

// hide the dropdown menu dropdown.hide();

// toggle the dropdown menu dropdown.toggle();

// check if dropdown is visible/open dropdown.isVisible(); {{< /code >}}

HTML Markup

Use the following HTML code for the JavaScript example above.

{{< code lang="html" file="dropdown.html" icon="file" >}} Dropdown button

{{< /code >}}

TypeScript

If you're using the <a href="{{< ref "getting-started/typescript" >}}">TypeScript configuration from Flowbite then you can import the types for the Dropdown class, parameters and its options.

Here's an example that applies the types from Flowbite to the code above:

{{< code lang="typescript" file="dropdown.ts" icon="file" >}} import { Dropdown } from 'flowbite'; import type { DropdownOptions, DropdownInterface } from 'flowbite'; import type { InstanceOptions } from 'flowbite';

// set the dropdown menu element const $targetEl: HTMLElement = document.getElementById('dropdownMenu');

// set the element that trigger the dropdown menu on click const $triggerEl: HTMLElement = document.getElementById('dropdownButton');

// options with default values const options: DropdownOptions = { placement: 'bottom', triggerType: 'click', offsetSkidding: 0, offsetDistance: 10, delay: 300, onHide: () => { console.log('dropdown has been hidden'); }, onShow: () => { console.log('dropdown has been shown'); }, onToggle: () => { console.log('dropdown has been toggled'); }, };

// instance options object const instanceOptions: InstanceOptions = { id: 'dropdownMenu', override: true };

/*

  • targetEl: required
  • triggerEl: required
  • options: optional
  • instanceOptions: optional */ const dropdown: DropdownInterface = new Dropdown( $targetEl, $triggerEl, options, instanceOptions );

// show the dropdown dropdown.show(); {{< /code >}}