User menu
An avatar button that opens an account menu with the user's name, email, links and sign out.
- Version 1.0.0
- Bootstrap 5.3+
- Requires Bootstrap CSS + Bootstrap JS bundle
- Source on GitHub
html
<div class="dropdown bootcn-dropdown bootcn-user-menu">
<button
class="btn bootcn-user-menu__trigger"
type="button"
id="bootcn-user-menu-trigger"
data-bs-toggle="dropdown"
aria-expanded="false"
>
<span class="bootcn-avatar bootcn-avatar--sm" aria-hidden="true"
><span class="bootcn-avatar__fallback">MP</span></span
>
<span class="bootcn-user-menu__name">Maria Papadopoulou</span>
<svg
class="bootcn-user-menu__chevron"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="M6 9l6 6 6-6" />
</svg>
</button>
<ul
class="dropdown-menu dropdown-menu-end bootcn-dropdown__menu"
aria-labelledby="bootcn-user-menu-trigger"
>
<li>
<p class="dropdown-header bootcn-user-menu__header">
<span class="bootcn-user-menu__header-name">Maria Papadopoulou</span>
<span class="bootcn-user-menu__email">maria@example.com</span>
</p>
</li>
<li><hr class="dropdown-divider bootcn-dropdown__divider" /></li>
<li><a class="dropdown-item bootcn-dropdown__item" href="#profile">Profile</a></li>
<li><a class="dropdown-item bootcn-dropdown__item" href="#billing">Billing</a></li>
<li><a class="dropdown-item bootcn-dropdown__item" href="#settings">Settings</a></li>
<li><hr class="dropdown-divider bootcn-dropdown__divider" /></li>
<li>
<button
class="dropdown-item bootcn-dropdown__item bootcn-dropdown__item--destructive"
type="button"
>
Sign out
</button>
</li>
</ul>
</div>Installation
bash
npx bootcn add user-menubash
pnpm dlx bootcn add user-menubash
yarn dlx bootcn add user-menubash
bunx --bun bootcn add user-menuThe CLI also installs the design tokens and these blocks and components: dropdown, avatar. It prints the <link> and <script> tags to add to your layout.
- Load Bootstrap 5.3 (Bootstrap CSS + Bootstrap JS bundle) and the design tokens.
- Install the blocks and components it uses:
dropdown,avatar. - Copy the files from Source into your project. With the default
bootcn.jsonthey go here:
| File | Default location |
|---|---|
user-menu.html | components/ui/user-menu.html |
user-menu.css | assets/css/user-menu.css |
Usage
Put it at the end of your navbar or sidebar. Replace the initials (or use an <img> avatar, see the Avatar component), the name, the email and the links.
The menu is Bootstrap's dropdown, so it needs the Bootstrap JS bundle.
Accessibility
- The trigger's accessible name is the user's name. On phones the name is hidden visually but still read by screen readers.
- Bootstrap handles
aria-expanded, arrow keys and Escape in the menu. - Sign out is a
<button>, because it performs an action; wrap it in a<form method="post">in your app.
Dependencies
- Bootstrap 5.3+ CSS and the JS bundle.
themes/tokens/tokens.css.- The
dropdowncomponent. - The
avatarcomponent.
Source
The exact files the CLI installs (user-menu@1.0.0). View on GitHub · Report an issue
html
<div class="dropdown bootcn-dropdown bootcn-user-menu">
<button
class="btn bootcn-user-menu__trigger"
type="button"
id="bootcn-user-menu-trigger"
data-bs-toggle="dropdown"
aria-expanded="false"
>
<span class="bootcn-avatar bootcn-avatar--sm" aria-hidden="true"
><span class="bootcn-avatar__fallback">MP</span></span
>
<span class="bootcn-user-menu__name">Maria Papadopoulou</span>
<svg
class="bootcn-user-menu__chevron"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="M6 9l6 6 6-6" />
</svg>
</button>
<ul
class="dropdown-menu dropdown-menu-end bootcn-dropdown__menu"
aria-labelledby="bootcn-user-menu-trigger"
>
<li>
<p class="dropdown-header bootcn-user-menu__header">
<span class="bootcn-user-menu__header-name">Maria Papadopoulou</span>
<span class="bootcn-user-menu__email">maria@example.com</span>
</p>
</li>
<li><hr class="dropdown-divider bootcn-dropdown__divider" /></li>
<li><a class="dropdown-item bootcn-dropdown__item" href="#profile">Profile</a></li>
<li><a class="dropdown-item bootcn-dropdown__item" href="#billing">Billing</a></li>
<li><a class="dropdown-item bootcn-dropdown__item" href="#settings">Settings</a></li>
<li><hr class="dropdown-divider bootcn-dropdown__divider" /></li>
<li>
<button
class="dropdown-item bootcn-dropdown__item bootcn-dropdown__item--destructive"
type="button"
>
Sign out
</button>
</li>
</ul>
</div>css
.bootcn-user-menu__trigger {
display: inline-flex;
align-items: center;
gap: 0.5rem;
min-height: var(--bootcn-control-height);
padding: 0.25rem 0.5rem 0.25rem 0.25rem;
border: var(--bootcn-border-width) solid transparent;
border-radius: var(--bootcn-radius-pill);
color: var(--bs-body-color);
font-weight: 500;
}
.bootcn-user-menu__trigger:hover,
.bootcn-user-menu__trigger[aria-expanded='true'] {
background-color: var(--bs-secondary-bg);
}
.bootcn-user-menu__trigger:focus-visible {
outline: 0;
box-shadow: var(--bootcn-focus-ring);
}
.bootcn-user-menu__chevron {
color: var(--bs-secondary-color);
}
.bootcn-user-menu__header {
display: flex;
flex-direction: column;
margin: 0;
white-space: normal;
}
.bootcn-user-menu__header-name {
color: var(--bs-emphasis-color);
font-weight: 600;
}
.bootcn-user-menu__email {
color: var(--bs-secondary-color);
font-size: 0.8125rem;
}
/* On narrow screens show only the avatar; the name stays the button's
accessible name. */
@media (max-width: 575.98px) {
.bootcn-user-menu__name {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
}
}
