Skip to content
Nana UI

Navigation

User Menu

The signed-in person's avatar, opening their account menu: who they are, their links and actions, and sign out.

View .md Download

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.

Nana Insurance
Page content
Nana Insurance
Page content

Install the package for a versioned dependency:

npm install @nana-tec/ui-components

Or copy the source in and own it:

npx @nana-tec/ui add user-menu --framework web-components
npx @nana-tec/ui add user-menu --framework svelte

Lands 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.

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.

KeyDoes
Enter, Space or Down arrow on the triggerOpens the menu on the first item
Up arrow on the triggerOpens the menu on the last item
Up and Down arrowsMove between items, skipping disabled ones
Home and EndGo to the first or last item
Enter or SpaceChooses the item
EscapeCloses the menu and returns to the trigger
TabCloses the menu and moves on

A press outside the menu also closes it.

  • The trigger is named by label and 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

PropertyAttributeTypeDefaultDescription
emailemailstring''Shown under the name in the header
groups—UserMenuGroup[][]The menu's groups, each with an optional heading
labellabelstring'Account'Names the menu, and the trigger together with name
namenamestring''Whose account it is: the trigger's name, the avatar's initials and the header
openopenbooleanfalseWhether the panel is open. Set it to open or close the panel yourself.
placementplacement'start' | 'end''end'Side the panel sits on when there is room
showNameshow-namebooleanfalseShow the name and an arrow beside the avatar
signOutsign-outstring'Sign out'The sign-out item's text; empty leaves it off
srcsrcstring''The avatar's photograph

Events

EventDescription
nana-selectAn item was chosen, sign out included (detail.value is its id, detail.item the item)
nana-openAfter the menu opens
nana-closeAfter the menu closes

CSS parts

PartDescription
::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

PropTypeDefault
namestring''
emailstring''
srcstring''
labelstring'Account'
groupsUserMenuGroup[][]
signOutstring'Sign out'
showNamebooleanfalse
alignDropdownAlign'end'
openbind:booleanfalse
onSelect(item: UserMenuItem) => void

Also accepts Omit<HTMLAttributes<HTMLDivElement>, 'class'>.