# User Dropdown

An account trigger that unfolds into a user menu. The card shows the avatar, name, email and a status pill; open it and the menu grows down from the card in one motion. Rows carry key-cap shortcuts, badges, swatches or external-link marks, and submenus slide in at the side for status, accounts or spaces. Click the card, or focus it and press Enter, then use the arrow keys, the number keys, or type a label to jump. The demo walks the menu on its own until you touch it.

<demo-user-dropdown>



</demo-user-dropdown>

## Installation

```bash
npx shadcn-vue@latest add "https://nxui.geoql.in/r/user-dropdown.json"
```

## Usage

```vue
<script setup lang="ts">
  import { computed, ref } from 'vue';
  import UserDropdown from '~/components/ui/user-dropdown/UserDropdown.vue';
  import type {
    UserDropdownItem,
    UserDropdownSection,
    UserDropdownSubItem,
    UserDropdownUser,
  } from '~/components/ui/user-dropdown/types';

  const open = ref(false);
  const darkMode = ref(false);

  const user: UserDropdownUser = {
    name: 'Noa Park',
    email: 'noa@nxui.geoql.in',
    avatar: '/avatars/noa.jpg',
    badge: 'Online',
  };

  const sections = computed<UserDropdownSection[]>(() => [
    {
      id: 'account',
      items: [
        {
          id: 'profile',
          label: 'Your profile',
          icon: 'lucide:circle-user-round',
          shortcut: '1',
        },
        {
          id: 'dark',
          label: 'Dark mode',
          icon: 'lucide:moon',
          kind: 'switch',
          checked: darkMode.value,
        },
        {
          id: 'upgrade',
          label: 'Upgrade to Pro',
          icon: 'lucide:star',
          badge: '20% off',
        },
        {
          id: 'help',
          label: 'Get help?',
          icon: 'lucide:circle-help',
          href: '/help',
          external: true,
        },
      ],
    },
    {
      id: 'switch',
      items: [
        {
          id: 'switch-account',
          label: 'Switch account',
          icon: 'lucide:repeat-2',
          kind: 'submenu',
          shortcut: 'S',
          submenuTitle: 'Switch account',
          submenuSide: 'left',
          children: [
            { id: 'noa', label: 'Noa Park', checked: true },
            { id: 'rui', label: 'Rui Tanaka', checked: false },
            {
              id: 'add',
              label: 'Add new account',
              icon: 'lucide:plus',
              action: true,
            },
          ],
        },
      ],
    },
    {
      id: 'footer',
      footer: true,
      items: [
        {
          id: 'logout',
          label: 'Log out',
          icon: 'lucide:log-out',
          danger: true,
        },
      ],
    },
  ]);

  function onSelect(item: UserDropdownItem, child?: UserDropdownSubItem) {
    console.log(item.id, child?.id);
  }

  function onToggle(item: UserDropdownItem, value: boolean) {
    if (item.id === 'dark') darkMode.value = value;
  }
</script>

<template>
  <UserDropdown
    v-model:open="open"
    :user="user"
    :sections="sections"
    @select="onSelect"
    @toggle="onToggle"
  />
</template>
```

The menu is controlled by its data. Update `checked`, `label` or `swatch` in your sections when `select`, `toggle` or `submit` fires and the menu follows.

## Props

<table>
<thead>
  <tr>
    <th>
      Prop
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        user
      </code>
    </td>
    
    <td>
      <code>
        UserDropdownUser
      </code>
    </td>
    
    <td>
      —
    </td>
    
    <td>
      The account shown in the trigger. See the user options below.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        sections
      </code>
    </td>
    
    <td>
      <code>
        UserDropdownSection[]
      </code>
    </td>
    
    <td>
      —
    </td>
    
    <td>
      Groups of rows. Each group is split from the next by a hairline.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        open
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      <code>
        false
      </code>
    </td>
    
    <td>
      <code>
        v-model:open
      </code>
      
      . Whether the menu is open.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        close-on-select
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      <code>
        true
      </code>
    </td>
    
    <td>
      Close the menu after a plain item is chosen.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        accent
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        '#3d63dd'
      </code>
    </td>
    
    <td>
      Colour of the keyboard focus ring and of an “on” switch.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Account menu for <name>'
      </code>
    </td>
    
    <td>
      Accessible label for the trigger and the menu.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        class
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        ''
      </code>
    </td>
    
    <td>
      Extra classes for the root element. The menu is <code>
        w-68
      </code>
      
       (272px) wide.
    </td>
  </tr>
</tbody>
</table>

## User options

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        name
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Display name.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        email
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Second line under the name.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        avatar
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Avatar image URL.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        initials
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Shown when there is no image. Defaults from the name.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        avatarBackground
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      CSS background for the avatar, e.g. a gradient.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        badge
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Pill next to the name, e.g. <code>
        Online
      </code>
      
       or <code>
        Pro
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        badgeTone
      </code>
    </td>
    
    <td>
      <code>
        'success' | 'accent' | 'neutral'
      </code>
    </td>
    
    <td>
      Pill colour. Defaults to <code>
        success
      </code>
      
      .
    </td>
  </tr>
</tbody>
</table>

## Item options

Each section is `{ id, items, footer? }`. Set `footer: true` to draw the section on the recessed base under the card.

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        id
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Unique id.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Row text. Also used for type-to-jump.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        icon
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Iconify name, e.g. <code>
        lucide:settings
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        iconClass
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Extra classes for the icon, e.g. a colour.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        kind
      </code>
    </td>
    
    <td>
      <code>
        'item' | 'switch' | 'submenu'
      </code>
    </td>
    
    <td>
      Plain action, on/off switch, or a row that opens a side submenu.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        shortcut
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      One key. Shown as a key cap; pressing it while open runs the row.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        badge
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Pill on the right, e.g. <code>
        20% off
      </code>
      
      . <code>
        badgeTone
      </code>
      
       sets its colour.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        swatch
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      CSS background drawn as a small square on the right.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        href
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Renders the row as a link. <code>
        external
      </code>
      
       opens it in a new tab.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        danger
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      Red text for destructive actions.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        muted
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      Muted text, e.g. an unset status line.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        disabled
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      Skipped by the keyboard and not clickable.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        checked
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      Switch state for <code>
        kind: 'switch'
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        children
      </code>
    </td>
    
    <td>
      <code>
        UserDropdownSubItem[]
      </code>
    </td>
    
    <td>
      Submenu rows: <code>
        id
      </code>
      
      , <code>
        label
      </code>
      
      , <code>
        icon
      </code>
      
      , <code>
        avatar
      </code>
      
      , <code>
        avatarBackground
      </code>
      
      , <code>
        square
      </code>
      
      , <code>
        shortcut
      </code>
      
      , <code>
        checked
      </code>
      
      , <code>
        action
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        submenuTitle
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Small heading at the top of the submenu.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        submenuSide
      </code>
    </td>
    
    <td>
      <code>
        'left' | 'right'
      </code>
    </td>
    
    <td>
      Side the submenu opens on. It flips, or drops below the row, when there is no room.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        submenuInput
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Adds a text field with this placeholder. Enter fires <code>
        submit
      </code>
      
      .
    </td>
  </tr>
</tbody>
</table>

## Events

<table>
<thead>
  <tr>
    <th>
      Event
    </th>
    
    <th>
      Payload
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        update:open
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      The menu opened or closed.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        select
      </code>
    </td>
    
    <td>
      <code>
        (item: UserDropdownItem, child?: UserDropdownSubItem)
      </code>
    </td>
    
    <td>
      A row was chosen. <code>
        child
      </code>
      
       is set for submenu rows.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        toggle
      </code>
    </td>
    
    <td>
      <code>
        (item: UserDropdownItem, value: boolean)
      </code>
    </td>
    
    <td>
      A switch row was flipped. <code>
        value
      </code>
      
       is the new state.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        submit
      </code>
    </td>
    
    <td>
      <code>
        (item: UserDropdownItem, value: string)
      </code>
    </td>
    
    <td>
      Enter was pressed in a submenu text field.
    </td>
  </tr>
</tbody>
</table>

## Accessibility

- The trigger is a button with `aria-haspopup="menu"`, `aria-expanded` and `aria-controls`.
- The list is `role="menu"`. Rows are `menuitem`, switches are `menuitemcheckbox` with `aria-checked`, and submenu rows with `checked` are `menuitemradio`.
- Enter, Space or ArrowDown on the trigger opens the menu on the first row; ArrowUp opens it on the last.
- ArrowUp and ArrowDown move between rows, Home and End jump to the ends, and typing letters jumps to a matching label. A row's shortcut key runs it.
- ArrowRight or ArrowLeft opens a submenu; Escape or the opposite arrow closes it and returns to its row.
- Escape closes the menu and returns focus to the trigger. Tab or a click outside closes it.
- The keyboard row shows a visible focus ring in the `accent` colour.
- With reduced motion turned on, the menu and submenus open without animation.
