Skip to content

✨ Add UserMenu navigation component#13

Merged
damienlagae merged 1 commit into
mainfrom
feat/user-menu-component
Apr 27, 2026
Merged

✨ Add UserMenu navigation component#13
damienlagae merged 1 commit into
mainfrom
feat/user-menu-component

Conversation

@damienlagae
Copy link
Copy Markdown
Member

Summary

Adds a new Enabel:Ux:UserMenu Twig component that encapsulates the common pattern of a navbar user menu (circular avatar + right-aligned dropdown), avoiding the need to drop back into raw HTML inside a Menu block.

Why

MenuItem supports an image for the dropdown toggle, but does not cover the full user-menu pattern that most Enabel apps need:

  • avatar fallback (colored initials when no profile picture is available)
  • dropdown header with the user display name
  • dropdown items conditioned on permissions
  • right alignment for the dropdown

Until now, projects had to hand-write the <li class="nav-item dropdown"> markup inside Menu's content block, duplicating boilerplate and a11y attributes. This component centralizes that pattern.

API

{{ component('Enabel:Ux:UserMenu', {
    name: app.user.displayName,            {# required, drives aria-label, header, default initials/color #}
    image: app.user.profilePictureDataUri, {# optional, photo takes precedence over initials #}
    initials: app.user.initials,           {# optional override of derived initials #}
    initialsColor: app.user.avatarColor,   {# optional override of derived background #}
    items: [
        {label: 'Profile'|trans, link: path('app_profile'), icon: 'bi:person-circle'},
        {label: 'Admin'|trans, link: path('admin_dashboard'), icon: 'bi:gear-fill', visible: is_granted('ROLE_ADMIN')},
        {divider: true},
        {label: 'Logout'|trans, link: path('app_logout'), icon: 'bi:box-arrow-right'},
    ],
}) }}

Initials derivation

  • Damien LagaeDL
  • John Ronald Reuel TolkienJT (first + last word only)
  • DamienDA (two first chars for single-word names)
  • éric DüboisÉD (multibyte-safe, uppercase)
  • Empty/whitespace name → empty string

Color derivation

Picked deterministically from a 10-color palette via CRC32 hash of name, so avatars stay visually stable across pages and sessions without any DB column.

visible flag on items

Items with visible: false are filtered out before rendering, which lets consumers express permission-gated entries inline without wrapping them in {% if %} blocks.

Test plan

  • PHPUnit (full suite green: 185 tests, 519 assertions — 27 new tests for UserMenu)
  • PHPStan level 8 — no errors
  • PHP-CS-Fixer (@Symfony + @Symfony:risky) — no violations
  • Visual smoke test in a consumer project (auto-derived initials, override path, image path, conditional admin item, dropdown alignment)

Notes

  • New component, no breaking change to existing MenuItem / Menu / Navbar.
  • Documentation added at docs/Navigation/userMenu.md and linked from docs/index.md.
  • The avatar <span> uses inline styles (width, height, background) because the color is dynamic per-user; happy to move the static parts to the Enabel Bootstrap Theme stylesheet in a follow-up if preferred.

Introduce a ready-to-use user menu component for the navbar, plus minor
tweaks to the Menu component to keep items vertically aligned when their
heights differ.

UserMenu features:
- Circular avatar (3rem / 48px) rendered from a profile picture or
  auto-derived colored initials when no image is provided
- Initials derived from the `name` parameter (first + last word, or
  two first chars for single-word names), uppercase, multibyte-safe
- Background color picked deterministically from a curated palette via
  CRC32 hash of `name`, so avatars stay stable across pages
- Right-aligned dropdown by default, with the user name as header
- Items support a `visible` flag to express conditional entries
  (e.g. permission-gated links) without surrounding `{% if %}` blocks
- Auto-detection of active state on items, consistent with MenuItem

Menu adjustment:
- Add `align-items-center` to the rendered `<ul>` so menu items of
  different heights (LocaleSwitcher, MenuItem, UserMenu avatar) stay
  vertically centered on the same row
@damienlagae damienlagae force-pushed the feat/user-menu-component branch from 405c907 to 9b000fc Compare April 27, 2026 18:45
@damienlagae damienlagae merged commit 8fe8659 into main Apr 27, 2026
7 checks passed
@damienlagae damienlagae deleted the feat/user-menu-component branch April 27, 2026 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant