Navigation
User Menu
The signed-in person's avatar, opening their account menu: who they are, their links and actions, and sign out.
The user menu is the account corner of an app: the signed-in person’s avatar in the top bar, opening a menu that starts with their name and email, lists their links and actions in groups, and ends with sign out. It is a menu button with that shape already given; for any other list of commands, use the menu button itself.
Preview
Section titled “Preview”Installation
Section titled “Installation”Install the package for a versioned dependency:
npm install @nana-tec/ui-componentsOr copy the source in and own it:
npx @nana-tec/ui add user-menu --framework web-componentsnpx @nana-tec/ui add user-menu --framework svelteLands in src/lib/components/ui/user-menu/ as NanaUserMenu.svelte, styled with Tailwind
classes that resolve to the design tokens. Requires Tailwind v4.
Give it the person’s name, and their email and a photo in src when you have them;
without a photo the avatar shows their initials. The menu’s contents are data: a list of
groups, each with an optional heading and its items. An item has an id and a label,
and can carry an href that makes it a link, a shortcut shown on the right, disabled
and destructive. Groups with no items are left out, and a rule separates the rest.
Sign out is always last, in a group of its own, with the id sign-out. Change its text
with signOut, or set it empty to leave it off.
<script lang="ts"> import { goto } from '$app/navigation'; import { NanaUserMenu, type UserMenuGroup } from '$lib/components/ui/user-menu';
const groups: UserMenuGroup[] = [ { items: [ { id: 'profile', label: 'Profile', href: '/profile' }, { id: 'settings', label: 'Settings', href: '/settings' }, ], }, { heading: 'Help', items: [{ id: 'support', label: 'Contact support' }] }, ];</script>
<NanaUserMenu name="Wanjiru Kamau" email="wanjiru@example.com" {groups} onSelect={(item) => { if (item.id === 'sign-out') goto('/sign-out'); }}/>onSelect receives every chosen item, links included. open is bindable.
Import the autoloader once, in your app's entry point. Every <nana-*> tag then works anywhere, and
each component downloads only the first time a page uses it:
import "@nana-tec/ui-components/autoloader";import "@nana-tec/ui-components/theme";The second line gives every component the Nana look and the design tokens behind it. Leave it out to style the components entirely yourself.
Import a component directly when it must be ready at once: on screen at first paint, or when you call its methods straight away. The two mix freely:
import "@nana-tec/ui-components/user-menu";
Copied in with npx @nana-tec/ui add user-menu? Import your project's own
@/lib/nana-ui/autoloader.js once instead; the CLI keeps it up to date.
<nana-user-menu name="Wanjiru Kamau" email="wanjiru@example.com"></nana-user-menu>
<script type="module"> const menu = document.querySelector('nana-user-menu'); menu.groups = [ { items: [ { id: 'profile', label: 'Profile', href: '/profile' }, { id: 'settings', label: 'Settings', href: '/settings' }, ], }, { heading: 'Help', items: [{ id: 'support', label: 'Contact support' }] }, ]; menu.addEventListener('nana-select', (event) => { if (event.detail.value === 'sign-out') location.assign('/sign-out'); });</script>groups is a property, not an attribute. nana-select fires for every chosen item, links
included, with its id as detail.value and the item as detail.item.
Showing the name
Section titled “Showing the name”The trigger shows only the avatar. Set show-name (showName in Svelte) to put the name
and an arrow beside it, for a wide top bar where there is room.
Keyboard
Section titled “Keyboard”| Key | Does |
|---|---|
| Enter, Space or Down arrow on the trigger | Opens the menu on the first item |
| Up arrow on the trigger | Opens the menu on the last item |
| Up and Down arrows | Move between items, skipping disabled ones |
| Home and End | Go to the first or last item |
| Enter or Space | Chooses the item |
| Escape | Closes the menu and returns to the trigger |
| Tab | Closes the menu and moves on |
A press outside the menu also closes it.
Accessibility
Section titled “Accessibility”- The trigger is named by
labeland the person’s name: “Account, Wanjiru Kamau”. The avatar and the name shown beside it are hidden from screen readers, so the name is not read twice. - The menu is named by
label, “Account” by default. - Linked items are links with the menu item role; a disabled one is not a link at all.
Properties
| Property | Attribute | Type | Default | Description |
|---|---|---|---|---|
email | email | string | '' | Shown under the name in the header |
groups | — | UserMenuGroup[] | [] | The menu's groups, each with an optional heading |
label | label | string | 'Account' | Names the menu, and the trigger together with name |
name | name | string | '' | Whose account it is: the trigger's name, the avatar's initials and the header |
open | open | boolean | false | Whether the panel is open. Set it to open or close the panel yourself. |
placement | placement | 'start' | 'end' | 'end' | Side the panel sits on when there is room |
showName | show-name | boolean | false | Show the name and an arrow beside the avatar |
signOut | sign-out | string | 'Sign out' | The sign-out item's text; empty leaves it off |
src | src | string | '' | The avatar's photograph |
Events
| Event | Description |
|---|---|
nana-select | An item was chosen, sign out included (detail.value is its id, detail.item the item) |
nana-open | After the menu opens |
nana-close | After the menu closes |
CSS parts
| Part | Description |
|---|---|
::part(trigger) | The button |
::part(panel) | The floating menu panel |
::part(identity) | What the trigger shows: the avatar, and the name when show-name is set |
::part(avatar) | An avatar, in the trigger or the header |
::part(name) | The name beside the avatar in the trigger |
::part(chevron) | The arrow after the name |
::part(header) | Who is signed in, at the top of the menu |
::part(header-name) | Their name |
::part(header-email) | Their email address |
::part(heading) | A group's heading |
::part(separator) | The rule between groups |
NanaUserMenu
| Prop | Type | Default |
|---|---|---|
name | string | '' |
email | string | '' |
src | string | '' |
label | string | 'Account' |
groups | UserMenuGroup[] | [] |
signOut | string | 'Sign out' |
showName | boolean | false |
align | DropdownAlign | 'end' |
openbind: | boolean | false |
onSelect | (item: UserMenuItem) => void |
Also accepts Omit<HTMLAttributes<HTMLDivElement>, 'class'>.