aiellie

Search

Find a component, template or page

GitHub
Design

User Menu

Who is signed in, at the foot of a sidebar: a small picture and their name, opening a menu headed by them and their plan, with usage, invites, settings, help and logging out. Signed out, it's a Sign in button that opens the login form.

fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/user-menu"

shadcn needs the @aiellie registry in components.json first. npx aiellie add user-menu adds it on its first run, then installs the same way.

User menu is the row at the foot of a sidebar with the person's avatar and name, and the account menu it opens: usage, inviting a friend, settings, help and logging out. Settings opens the Settings dialog, which comes with it, so the row works on its own. With no one signed in, the row is a Sign in button that opens the Login form in a dialog.

Getting started

  1. Put it in the sidebar's footer

    It is a SidebarMenu, so it goes inside a Sidebar under a SidebarProvider. Pass who is signed in as user.

    components/app-sidebar.tsx
    "use client"
    
    import { UserMenu } from "@/components/ui/user-menu"
    import {
      Sidebar,
      SidebarContent,
      SidebarFooter,
    } from "@/components/ui/sidebar"
    
    export function AppSidebar() {
      return (
        <Sidebar>
          <SidebarContent>{/* chats */}</SidebarContent>
          <SidebarFooter>
            <UserMenu
              user={{ name: "Ada Lovelace", email: "ada@example.com", plan: "Pro" }}
              usage="72% left"
              onUsage={() => {}}
              onInvite={() => {}}
              onLogOut={() => signOut()}
            />
          </SidebarFooter>
        </Sidebar>
      )
    }
  2. Save what settings change

    settings is what the dialog starts from each time it opens, and onSettingsSave gets everything in it when saved.

    <UserMenu
      user={user}
      settings={{ theme, providers, providerKeys }}
      onSettingsSave={(values) => saveSettings(values)}
    />

Anatomy

<ul data-slot="user-menu"> {/* SidebarMenu */}
  <li>
    <button data-slot="sidebar-menu-button"> {/* the menu's trigger */}
      <span data-slot="avatar" /> {/* their picture, or their initials */}
      <span>{user.name}</span>
    </button>
  </li>
</ul>

<div data-slot="menu-content"> {/* as wide as the row, opening upward */}
  <div>{/* avatar, name, plan badge */}</div>
  <div data-slot="menu-separator" />
  <div data-slot="menu-item" /> {/* Usage, with `usage` at its end */}
  <div data-slot="menu-item" /> {/* Invite a friend */}
  <div data-slot="menu-item" /> {/* Settings, opening the dialog */}
  <div data-slot="menu-item" /> {/* Help */}
  <div data-slot="menu-separator" />
  <div data-slot="menu-item" /> {/* Log out */}
</div>

<div data-slot="dialog-content" /> {/* SettingsDialog */}

The row keeps its sidebar-accent fill while the menu is open. The menu opens upward at the row's width, and repeats the name with the plan as a badge, so the account is named where its actions are. Without an avatar, the first letters of the first two words of the name stand in.

Examples

Signed out

Leave out user and the row offers to sign in. onSignIn gets the email to send a code to; return a promise and the dialog stays open, its button working, until it settles. A rejection's message shows in the form. Log out to see it again.

Settings that save

Change the name in Settings and save: onSettingsSave passes it back and the row follows.

Open Settings, change the name, and save.

Open settings from somewhere else

Control the dialog with settingsOpen and onSettingsOpenChange, so a shortcut or another button can open it too.

const [settingsOpen, setSettingsOpen] = React.useState(false)

<UserMenu
  user={user}
  settingsOpen={settingsOpen}
  onSettingsOpenChange={setSettingsOpen}
/>

API reference

UserMenu

PropTypeDefaultDescription
userUserWho is signed in. Without one, the row is a Sign in button.
usagestringHow much of their plan is left, like 72% left, shown at the end of Usage.
onUsage() => voidRuns when Usage is chosen.
onInvite() => voidRuns when Invite a friend is chosen.
onSettings() => voidRuns when Settings is chosen, as the dialog opens.
settings{ theme, apiKeys, providers, providerKeys, defaultSection }What the settings dialog starts from each time it opens.
onSettingsSave(values: SettingsValues) => voidCalled with the dialog's values when saved.
settingsOpen / onSettingsOpenChangeboolean / (open: boolean) => voidControls the settings dialog.
onHelp() => voidRuns when Help is chosen.
onLogOut() => voidRuns when Log out is chosen.
onSignIn(values: { email: string }) => void | Promise<void>Called with the email from the login form, to send it a code. The dialog closes once it resolves.
login{ providers, onProvider, emailDescription, title, description }Passed on to the login dialog.

User

FieldTypeDescription
namestringShown in the row and the menu, and the source of the initials.
emailstringFilled into the settings dialog.
avatarstringA picture of them. Without one, their initials stand in.
planstringThe plan they are on, like Pro, badged beside their name.