## Menu Button (Dropdown) [View live demo on CodeSandbox](https://codesandbox.io/s/headlessuivue-menu-example-forked-24f9d?file=/src/App.vue) 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. - [Installation](#installation) - [Basic example](#basic-example) - [Styling](#styling) - [Transitions](#transitions) - [Component API](#component-api) ## Installation Please note that **this library only supports Vue 3**. ```sh # npm npm install @headlessui/vue # Yarn yarn add @headlessui/vue ``` ### 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. ```vue ``` ### 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](https://v3.vuejs.org/guide/component-slots.html#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. ```vue ``` ### 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. ```vue ``` 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. ```vue ``` ### 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. ```vue ``` ### 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 `` element and the transition will be applied automatically. ```vue ``` ### 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. ```vue ``` 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. ```vue ``` To tell an element to render its children directly with no wrapper element, use `as="template"`. ```vue ``` ### Component API #### Menu ```vue More options ``` ##### Props | Prop | Type | Default | Description | | :--- | :------------------ | :-------------------------------- | :---------------------------------------------------- | | `as` | String \| Component | `template` _(no wrapper element_) | The element or component the `Menu` should render as. | ##### Slot props | Prop | Type | Description | | :----- | :------ | :------------------------------- | | `open` | Boolean | Whether or not the menu is open. | #### MenuButton ```vue More options ``` ##### Props | Prop | Type | Default | Description | | :--- | :------------------ | :------- | :---------------------------------------------------------- | | `as` | String \| Component | `button` | The element or component the `MenuButton` should render as. | ##### Slot props | Prop | Type | Description | | :----- | :------ | :------------------------------- | | `open` | Boolean | Whether or not the menu is open. | #### MenuItems ```vue ``` ##### Props | Prop | Type | Default | Description | | :-------- | :------------------ | :------ | :-------------------------------------------------------------------------------- | | `as` | String \| Component | `div` | The element or component the `MenuItems` should render as. | | `static` | Boolean | `false` | Whether the element should ignore the internally managed open/closed state. | | `unmount` | Boolean | `true` | Whether the element should be unmounted or hidden based on the open/closed state. | ##### Slot props | Prop | Type | Description | | :----- | :------ | :------------------------------- | | `open` | Boolean | Whether or not the menu is open. | #### MenuItem ```vue Settings ``` ##### Props | Prop | Type | Default | Description | | :--------- | :------------------ | :-------------------------------- | :------------------------------------------------------------------------------------ | | `as` | String \| Component | `template` _(no wrapper element)_ | The element or component the `MenuItem` should render as. | | `disabled` | Boolean | `false` | Whether or not the item should be disabled for keyboard navigation and ARIA purposes. | ##### Slot props | Prop | Type | Description | | :--------- | :------ | :--------------------------------------------------------------------------------- | | `active` | Boolean | Whether or not the item is the active/focused item in the list. | | `disabled` | Boolean | Whether or not the item is the disabled for keyboard navigation and ARIA purposes. |