Menus often depend on something that arrives late or changes: the user's permissions, a list of projects from a server, a feature flag. Render the navigation from that data like any other list. The sidebar follows the changes.
Dynamic source
What the sidebar keeps up with#
- Items, groups and sections that are added or removed. A new item takes on the sidebar's state at once: the collapsed look, the notification style and the active page.
- Sub-items that are added, removed or reordered in an existing parent. They join the open sub-menu in the order you give them, and the menu grows and shrinks with them.
- The first and the last sub-item. A parent that had none gets its sub-menu when the first one arrives, and loses it with the last one.
- Links that change. When a link gets another
href, or the link itself is replaced, the active page is matched again. - Moving and re-attaching. A sidebar that is taken out of the page and put back, and an item that is moved to another sidebar, pick up the state of where they are now.
Nothing here needs a delay, a refresh call or a new activePath.
With Angular#
Use @for and @if on items and sub-items. The parent is not recreated when
its children change, so an open sub-menu stays open.
<nv-sidebarnavitem icon="folder" [collapsible]="true" [open]="true">
<a routerLink="/projects">Projects</a>
@for (project of projects(); track project.id) {
<nv-sidebarnavsubitem>
<a [routerLink]="project.path">{{ project.name }}</a>
</nv-sidebarnavsubitem>
}
</nv-sidebarnavitem>
A sub-item that a list adds at its end joins the end of the sub-menu. If your template has fixed sub-items after the list, the new one appears after them. Render entries that have to stay last from the same list.
Good to know#
- A parent only shows sub-items while it is
collapsible. Set it from the start on items that may get sub-items later. - A sub-item that becomes the current page inside a closed parent highlights the parent. See The active page.
- An item added to a collapsed sidebar arrives closed, like the others.
- Sub-items that you wrap in an element of your own stay inside that element. Put them directly in the nav item to have them in its sub-menu.