Skip to content
On this page

Dropdown

A dropdown component based on HeadlessUI. It is used to display a list of options that can be selected or menus. You can also leverage this dropdown to create new components, we currently provide 2 dropdowns, one based on HeadlessUI and another totally done from scratch that provides way more control over the dropdown. The API is pretty similar between both.

Preview & Playground 🖼️

Here you may find a preview of the component, with error & possible variants.

🏄 Click to expand the code
vue
<script setup lang="ts">
import { Button, Dropdown, DropdownMenu, DropdownOption } from '@flavorly/vanilla-components'
import { ref } from 'vue'
import CheckCircleIcon from '~icons/heroicons/check-circle-solid'
import DocumentDuplicateIcon from '~icons/heroicons/document-duplicate-solid'

const value = ref(false)
const valueDisabled = ref(false)
const value2 = ref(false)
</script>

<template>
  <PreviewWrapper>
    <div class="h-[200px]">
      <div class="flex flex-col items-center justify-center mx-auto space-y-3 ">
        <DropdownMenu
          v-model="value"
          text="Options"
        >
          <!-- Option -->
          <DropdownOption>
            <DocumentDuplicateIcon class="w-5 h-5 mr-2" />
            <span>Copy Stuff</span>
          </DropdownOption>
          <!-- Option -->
          <DropdownOption>
            <CheckCircleIcon class="w-5 h-5 mr-2" />
            <span>Mark as Done</span>
          </DropdownOption>
        </DropdownMenu>

        <DropdownMenu
          v-model="value2"
          overlay
          text="Dropdown with Overlay"
        >
          <!-- Option -->
          <DropdownOption>
            <DocumentDuplicateIcon class="w-5 h-5 mr-2" />
            <span>Copy Stuff</span>
          </DropdownOption>
          <!-- Option -->
          <DropdownOption>
            <CheckCircleIcon class="w-5 h-5 mr-2" />
            <span>Mark as Done</span>
          </DropdownOption>
        </DropdownMenu>

        <Dropdown :teleport="true">
          <template #trigger>
            <Button class="btn">
              Click here
            </Button>
          </template>
          <div class="py-2 px-2 grid grid-cols-1 gap-2">
            <Button>Im a button inside this</Button>
            <Button>Im a button inside this</Button>
          </div>
        </Dropdown>

        <DropdownMenu
          v-model="value"
          :disabled="true"
          text="Disabled"
        >
          <!-- Option -->
          <DropdownOption>
            <DocumentDuplicateIcon class="w-5 h-5 mr-2" />
            <span>Copy Stuff</span>
          </DropdownOption>
          <!-- Option -->
          <DropdownOption>
            <CheckCircleIcon class="w-5 h-5 mr-2" />
            <span>Mark as Done</span>
          </DropdownOption>
        </DropdownMenu>
      </div>
    </div>
  </PreviewWrapper>
</template>

Props 📥

Props available for this component extending the default variant & global props.

Below are the props for the Dropdown Menu component based on Headless UI

PropDescriptionAccepted ValuesDefault
modelValueThe value for the dropdowntrue, falsefalse
textThe label for the buttonStringundefined
buttonVariantThe variant for the buttonStringundefined
teleportIf we should teleport the dropdownBooleantrue
teleportToElement to teleportstringbody
tagNameThe tag for the trigger buttonstringdiv
dropdownTagNameThe tag for the trigger buttonstringdiv
disabledDisable or enable the triggerBooleanfalse
toggleOnFocusToggle the dropdown on FocusBooleantrue
toggleOnClickToggle the dropdown on ClickBooleantrue
toggleOnHoverToggle the dropdown on HoverBooleanfalse
placementThe placement of the dropdown ( Popper )stringundefined
usePopperIf we should use Popper to place the dropdownBooleantrue
overlayFocus the dropdown by adding a overlayBooleanfalse
spacedItemsSpace the items inside the dropdownBooleantrue
showArrowShow the dropdown arrow relative to the triggerBooleanfalse
sizeThe size of the dropdownstringdefault
positionThe alignment/position of the triggerstringcenter
trapFocusIf we should trap the focus inside the dropdownBooleantrue

Below are the props for the Dropdown component, totally vanilla implementation

PropDescriptionAccepted ValuesDefault
modelValueThe value for the dropdowntrue, falsefalse
textThe label for the buttonStringundefined
teleportIf we should teleport the dropdownBooleantrue
teleportToElement to teleportstringbody
tagNameThe tag for the trigger buttonstringdiv
dropdownTagNameThe tag for the trigger buttonstringdiv
dropdownAttributesExtra attributes for the dropdownObjectundefined
disabledDisable or enable the triggerBooleanfalse
toggleOnFocusToggle the dropdown on FocusBooleantrue
toggleOnClickToggle the dropdown on ClickBooleantrue
toggleOnHoverToggle the dropdown on HoverBooleanfalse
closeOnClickAwayIf we should close the dropdown on clicking awayBooleantrue
placementThe placement of the dropdown ( Popper )stringundefined
showIf the dropdown should start as shownBooleanfalse
popperOptionsAdditional raw popper optionsObjectundefined
trapFocusIf we should trap the focus inside the dropdownBooleantrue

Slots 🧬

Current slots available for this component are the following:

Slot default

Actual content to show when the trigger is clicked / hovered. This is the actual content of the dropdown

Slot trigger

Slots usually used for the dropdown trigger button or any other element of your choice.

Events 🚇

Here you may find the events emitted by this component.

EventDescriptionValue
update:modelValueWhen the value changesany

| update:show | Toggle for open | Boolean | | focus | On Focus | FocusEvent | | blur | On Blur | FocusEvent | | blurOnChild | On Blur to child elements | FocusEvent | | click | On Click | MouseEvent | | mouseover | On Mouse Hover | MouseEvent | | mouseleave | On Mouse Leave | MouseEvent | | touchstart | On Touch start | TouchEvent | | shown | When dropdown is shown | Boolean | | hidden | When dropdown is hidden | Boolean | | beforeShow | Before dropdown show | Boolean | | beforeHide | Before dropdown hide | Boolean |

Released under the MIT License.