1.7.21 • Published 2 days ago

@headlessui/vue v1.7.21

Weekly downloads
997
License
MIT
Repository
github
Last release
2 days ago

Installation

Please note that this library only supports Vue 3.

# npm
npm install @headlessui/vue

# Yarn
yarn add @headlessui/vue

Components

This project is still in early development. New components will be added regularly over the coming months.

Roadmap

This project is still in early development, but the plan is to build out all of the primitives we need to provide interactive Vue examples of all of the components included in Tailwind UI, the commercial component directory that helps us fund the development of our open-source work like Tailwind CSS.

This includes things like:

  • Listboxes
  • Toggles
  • Modals
  • Tabs
  • Slide-overs
  • Mobile menus
  • Accordions

...and more in the future.

We'll be continuing to develop new components on an on-going basis, with a goal of reaching a pretty fleshed out v1.0 by the end of the year.

Menu Button (Dropdown)

View complete demo on CodeSandbox

The Menu component and related child components are used to quickly build custom dropdown components that are fully accessible out of the box, including correct ARIA attribute management and robust keyboard navigation support.

Basic example

Menu Buttons are built using the Menu, MenuButton, MenuItems, and MenuItem components.

The MenuButton will automatically open/close the MenuItems when clicked, and when the menu is open, the list of items receives focus and is automatically navigable via the keyboard.

<template>
  <Menu>
    <MenuButton>
      More
    </MenuButton>
    <MenuItems>
      <MenuItem v-slot="{ active }">
        <a :class="{ 'bg-blue-500': active }" href="/account-settings">
          Account settings
        </a>
      </MenuItem>
      <MenuItem v-slot="{ active }">
        <a :class="{ 'bg-blue-500': active }" href="/account-settings">
          Documentation
        </a>
      </MenuItem>
      <MenuItem v-slot="{ active }" disabled>
        <span :class="{ 'bg-blue-500': active }">
          Invite a friend (coming soon!)
        </span>
      </MenuItem>
    </MenuItems>
  </Menu>
</template>

<script>
import { Menu, MenuButton, MenuItems, MenuItem } from '@headlessui/vue'

export default {
  components: {
    Menu,
    MenuButton,
    MenuItems,
    MenuItem,
  },
}
</script>

Styling the active item

This is a headless component so there are no styles included by default. Instead, the components expose useful information via scoped slots that you can use to apply the styles you'd like to apply yourself.

To style the active MenuItem you can read the active slot prop, which tells you whether or not that menu item is the item that is currently focused via the mouse or keyboard.

You can use this state to conditionally apply whatever active/focus styles you like, for instance a blue background like is typical in most operating systems.

<template>
  <Menu>
    <MenuButton>
      More
    </MenuButton>
    <MenuItems>
      <!-- Use the `active` state to conditionally style the active item. -->
      <MenuItem v-slot="{ active }">
        <a href="/settings" :class="active ? 'bg-blue-500 text-white' : 'bg-white text-black'">
          Settings
        </a>
      </MenuItem>
      <!-- ... -->
    </MenuItems>
  </Menu>
</template>

Showing/hiding the menu

By default, your MenuItems instance will be shown/hidden automatically based on the internal open state tracked within the Menu component itself.

<template>
  <Menu>
    <MenuButton>
      More
    </MenuButton>

    <!-- By default, this will automatically show/hide when the MenuButton is pressed. -->
    <MenuItems>
      <MenuItem v-slot="{ active }">
        <a :class="{ 'bg-blue-500': active }" href="/account-settings">
          Account settings
        </a>
      </MenuItem>
      <!-- ... -->
    </MenuItems>
  </Menu>
</template>

If you'd rather handle this yourself (perhaps because you need to add an extra wrapper element for one reason or another), you can add a static prop to the MenuItems instance to tell it to always render, and inspect the open slot prop provided by the Menu to control which element is shown/hidden yourself.

<template>
  <Menu v-slot="{ open }">
    <MenuButton>
      More
    </MenuButton>

    <div v-show="open">
      <!-- Using `static`, `MenuItems` is always rendered and ignores the `open` state. -->
      <MenuItems static>
        <MenuItem v-slot="{ active }">
          <a :class="{ 'bg-blue-500': active }" href="/account-settings">
            Account settings
          </a>
        </MenuItem>
        <!-- ... -->
      </MenuItems>
    </div>
  </Menu>
</template>

Disabling an item

Use the disabled prop to disable a MenuItem. This will make it unselectable via keyboard navigation, and it will be skipped when pressing the up/down arrows.

<template>
  <Menu>
    <MenuButton>
      More
    </MenuButton>
    <MenuItems>
      <MenuItem disabled>
        <span class="opacity-75">Invite a friend (coming soon!)</span>
      </MenuItem>
      <!-- ... -->
    </MenuItems>
  </Menu>
</template>

Transitions

To animate the opening/closing of the menu panel, use Vue's built-in transition component. All you need to do is wrap your MenuItems instance in a <transition> element and the transition will be applied automatically.

<template>
  <Menu>
    <MenuButton>
      More
    </MenuButton>
    <transition
      enter-active-class="transition duration-100 ease-out"
      enter-from-class="transform scale-95 opacity-0"
      enter-to-class="transform scale-100 opacity-100"
      leave-active-class="transition duration-75 ease-out"
      leave-from-class="transform scale-100 opacity-100"
      leave-to-class="transform scale-95 opacity-0"
    >
      <MenuItems>
        <MenuItem v-slot="{ active }">
          <a :class="{ 'bg-blue-500': active }" href="/account-settings">
            Account settings
          </a>
        </MenuItem>
        <!-- ... -->
      </MenuItems>
    </transition>
  </Menu>
</template>

Rendering additional content

The Menu component is not limited to rendering only its related subcomponents. You can render anything you like within a menu, which gives you complete control over exactly what you are building.

For example, if you'd like to add a little header section to the menu with some extra information in it, just render an extra div with your content in it.

<template>
  <Menu>
    <MenuButton>
      More
    </MenuButton>
    <MenuItems>
      <div class="px-4 py-3">
        <p class="text-sm leading-5">Signed in as</p>
        <p class="text-sm font-medium leading-5 text-gray-900 truncate">tom@example.com</p>
      </div>
      <MenuItem v-slot="{ active }">
        <a :class="{ 'bg-blue-500': active }" href="/account-settings">
          Account settings
        </a>
      </MenuItem>
      <!-- ... -->
    </MenuItems>
  </Menu>
</template>

Note that only MenuItem instances will be navigable via the keyboard.

Rendering a different element for a component

By default, the Menu and its subcomponents each render a default element that is sensible for that component.

For example, MenuButton renders a button by default, and MenuItems renders a div. Menu and MenuItem interestingly do not render an extra element, and instead render their children directly by default.

This is easy to change using the as prop, which exists on every component.

<template>
  <!-- Render a `div` instead of no wrapper element -->
  <Menu as="div">
    <MenuButton>
      More
    </MenuButton>
    <!-- Render a `ul` instead of a `div` -->
    <MenuItems as="ul">
      <!-- Render an `li` instead of no wrapper element -->
      <MenuItem as="li" v-slot="{ active }">
        <a href="/account-settings" :class="{ 'bg-blue-500': active }">
          Account settings
        </a>
      </MenuItem>
      <!-- ... -->
    </MenuItems>
  </Menu>
</template>

To tell an element to render its children directly with no wrapper element, use as="template".

<template>
  <Menu>
    <!-- Render no wrapper, instead pass in a button manually -->
    <MenuButton as="template">
      <button>
        More
      </button>
    </MenuButton>
    <MenuItems>
      <MenuItem v-slot="{ active }">
        <a href="/account-settings" :class="{ 'bg-blue-500': active }">
          Account settings
        </a>
      </MenuItem>
      <!-- ... -->
    </MenuItems>
  </Menu>
</template>

Component API

Menu

<Menu v-slot="{ open }">
  <MenuButton>More options</MenuButton>
  <MenuItems>
    <MenuItem><!-- ... --></MenuItem>
    <!-- ... -->
  </MenuItems>
</Menu>
Props
PropTypeDefaultDescription
asString | Componenttemplate (no wrapper element)The element or component the MenuItems should render as.
Slot props
PropTypeDescription
openBooleanWhether or not the menu is open.

MenuButton

<MenuButton v-slot="{ open }">
  <span>More options</span>
  <ChevronRightIcon :class="open ? 'transform rotate-90' : ''" />
</MenuButton>
Props
PropTypeDefaultDescription
asString | ComponentbuttonThe element or component the MenuItems should render as.
Slot props
PropTypeDescription
openBooleanWhether or not the menu is open.

MenuItems

<MenuItems>
  <MenuItem><!-- ... --></MenuItem>
  <!-- ... -->
</MenuItem>
Props
PropTypeDefaultDescription
asString | ComponentdivThe element or component the MenuItems should render as.
staticBooleanfalseWhether the element should ignore the internally managed open/closed state.
Slot props
PropTypeDescription
openBooleanWhether or not the menu is open.

MenuItem

<MenuItem v-slot="{ active }">
  <a href="/settings" :class="active ? 'bg-blue-500 text-white' : 'bg-white text-black'">
    Settings
  </a>
</MenuItem>
Props
PropTypeDefaultDescription
asString | Componenttemplate (no wrapper element)The element or component the MenuItem should render as.
disabledBooleanfalseWhether or not the item should be disabled for keyboard navigation and ARIA purposes.
Slot props
PropTypeDescription
activeBooleanWhether or not the item is the active/focused item in the list.
disabledBooleanWhether or not the item is the disabled for keyboard navigation and ARIA purposes.
@myevery/nuxt-ui@pixelstream/auth-imagepegadaian-components@bms13/vcl@prefix/studio-editorjarvis-components-ts@skhrvg/fvs-uikitpongo-components-vuechistan-flow@indigit/vanilla-componentsflighthub-vue@stacktrace/storefrontcategory-filtersblueblocksui-navigationblueblocksui-product-listsblueblocksui-tailheaderblueblocksui-category-filterblueblocksui-paginationsoclite-monitor@everything-registry/sub-chunk-389hi-comp@hrubysi/design-systemleazy_editor@zenphp/doczilla@nethserver/vue-tailwind-lib@dcrall/design-viterevdojo-vue-componentsshuttle-vue3sid-components-jsonsid-vue-componentssid-componentsshared-vuesuperlive-client-sdk-vuejsstellar-uircp-shared-componentrd-material-kitrd-tailwind-componentportabilis-uiprioritas-web-componentspswd-clientselect-front-formatssprintify-uiti-packagetie-design-system-libscientiam-componentssui-dapp-kit-vuetasty-bistrotatweerui-vuesunwell-uitchdstestlib-calemansuteki-uibrickwork-vueducky-uiembeded-vuedripform-admindripformftn-uifroggy-uiga-poc-componentsgame-contestfrappe-uifohn-uifinansavisen-componentsgigcodes-nuxtgigcodes-uilukebui-com-admin-frontendlaravuewindlg-floor-plan-konvalabrada-componentsladministration-vue-hookhive-vuemagma-design-system-testmega3uimajestic-uiniftylaunch-frontendleazy-nuxt-layerlfe-componentshalcyon-vuegrownuikairos-nidjarvis-componentsnuxt-ui-custompapito-shared-vuenuxt-headlessuinuxt-headlessui-floatnxt-uinuxtlabs-ui-vuenuxt-templatenuxt-ui-vuenuxt-vrnhlbnotion-taposp-sidebar-layoutodyssey-task-platformnovel-vuepimelon-uipleg-uitt-ui-comp-lib@headlessui-float/nuxt@hempworks/pilgrim
1.7.21

2 days ago

1.7.20

13 days ago

1.7.19

3 months ago

1.7.18

3 months ago

1.7.17

4 months ago

1.7.15

9 months ago

1.7.16

9 months ago

1.7.14

11 months ago

1.7.13

1 year ago

1.7.9

1 year ago

1.7.8

1 year ago

1.7.10

1 year ago

1.7.11

1 year ago

1.7.12

1 year ago

1.7.7

1 year ago

1.7.6

1 year ago

1.7.5

1 year ago

1.7.4

1 year ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.7

2 years ago

1.6.6

2 years ago

1.6.5

2 years ago

1.6.4

2 years ago

1.6.3

2 years ago

1.6.2

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

0.0.0-aa1a48c

3 years ago

0.0.0-91b436a

3 years ago

0.0.0-33c5c6e

3 years ago

0.0.0-3f14839

3 years ago

1.4.1

3 years ago

0.0.0-b59c091

3 years ago

0.0.0-d60d2a5

3 years ago

1.4.0

3 years ago

0.0.0-112270d

3 years ago

0.0.0-ba1bd52

3 years ago

0.0.0-0cc9728

3 years ago

1.3.0

3 years ago

1.2.0-105068

3 years ago

1.2.0-ece9e87

3 years ago

1.2.0

3 years ago

1.2.0-21157da

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.2.0-2279cd9

3 years ago

1.0.0-ef31bbe

3 years ago

1.1.1-ab92811

3 years ago

1.0.0-43d5270

3 years ago

1.0.0-ce8d7f5

3 years ago

1.0.0-be833ba

3 years ago

1.0.0

3 years ago

0.3.1-4942928

3 years ago

0.3.1-1f5c95e

3 years ago

0.3.1-6497229

3 years ago

0.3.1-6fa6c45

3 years ago

0.3.1-d950146

3 years ago

0.3.1-acbc4d7

3 years ago

0.3.1-2aa95f2

3 years ago

0.3.1-cdfeeac

3 years ago

0.3.1-a02c818

3 years ago

0.3.1-8feec8c

3 years ago

0.3.1-035f9b0

3 years ago

0.3.1-5ff5225

3 years ago

0.3.1

3 years ago

0.3.0-96fa0cf

3 years ago

0.3.0-4d86cfd

3 years ago

0.3.0-b7fe69a

3 years ago

0.3.0-b309699

3 years ago

0.3.0-6dd6636

3 years ago

0.3.0-285724f

3 years ago

0.3.0-d5c9dce

3 years ago

0.3.0-e8e2770

3 years ago

0.3.0-4648332

3 years ago

0.3.0-e73d09b

3 years ago

0.3.0

3 years ago

0.2.0-da179ca

3 years ago

0.2.0-4459689

3 years ago

0.2.0-80402e7

3 years ago

0.2.0

4 years ago

0.0.0-fc42dde

4 years ago

0.0.0-7d402c4

4 years ago

0.1.4-alpha.1

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.0-alpha.1

4 years ago

0.0.0

4 years ago