Native Web Components playground

Nova v3.38.0

Preview

Web Components

Navigation that changes at runtime

Add, remove and reorder items and sub-items after the sidebar has loaded.

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

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 the DOM#

Add and remove your own elements with the ordinary methods: append, prepend, before, after and remove. Create sub-items as plain nv-sidebarnavsubitem elements and add them to the nav item. The item assigns them to its sub-menu.

tsconst item = document.createElement('nv-sidebarnavsubitem');
const link = document.createElement('a');
link.href = '/projects/beacon';
link.textContent = 'Beacon';
item.append(link);

document.querySelector('#projects').append(item);
Change only what you added

The sidebar components render their own markup next to your elements. replaceChildren, innerHTML and textContent on a sidebar component remove that markup too and leave the component empty. To swap all sub-items, remove the old ones and append the new ones.

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.