Building Your Site

Menus

Navigation menus that editors arrange as a tree.

A menu is a global that holds a tree of links. Editors arrange it in the admin. Your site reads it and renders a <nav>. This page shows how to set up a menu, render it at any depth, and mark the current page.

Set it up

menuPreset() adds a menu global to the Settings page of the admin:

tenon.config.ts
import { defineCms } from "tenoncms";
import { menuPreset } from "tenoncms/presets";

export default defineCms({
  // ...
  presets: [menuPreset({ name: "mainMenu", label: "Main menu", depth: -1 })],
});

depth is the number of levels below the top:

depthMenu
0A flat list of links. The default.
1Links with one level of children.
-1Any depth.

Each item is a link with an optional label. A menu with depth of 1 or more also has headings: items without a link that group the links below them.

Edit it

Editors open the menu from Settings in the admin. It shows the items as a tree:

  • Drag a row to move or nest it. Move to in the row's menu does the same with the keyboard.
  • A click on a row opens the item next to the tree.
  • The link input searches pages and other collections with a permalink. To link to another site, type its URL.
  • Leave the label empty to use the title of the linked page. The label then follows when the page title changes.

Render it

menuItems() turns the read into entries for your template:

import { menuItems } from "tenoncms";

const menu = await cms.getGlobal("mainMenu");
const items = menuItems(menu.items);
// [{ id, type: "link", label: "About", href: "/about", target: undefined, children: [...] }]
  • label is the item's label, else the title of the linked page.
  • A heading has href: null.
  • Links to deleted or unpublished pages are left out, with their children.
  • menuItems() makes no queries. Read the menu with populate, the default.

A component that renders its own children works for every depth:

---
const { items } = Astro.props;
const current = Astro.url.pathname;
---

<ul>
  {items.map((item) => (
    <li>
      {item.href ? (
        <a href={item.href} target={item.target} aria-current={item.href === current ? "page" : undefined}>
          {item.label}
        </a>
      ) : (
        <span>{item.label}</span>
      )}
      {item.children.length > 0 && <Astro.self items={item.children} />}
    </li>
  ))}
</ul>

Then read the menu where the navigation renders:

---
import { menuItems } from "tenoncms";
import MenuList from "./MenuList.astro";

const menu = await Astro.locals.tenon.getGlobal("mainMenu");
---

<nav aria-label="Main">
  <MenuList items={menuItems(menu.items)} />
</nav>

In Nuxt, NuxtLink sets aria-current="page" on the link to the current page by itself.

Customize it

The Menu preset has the options for these tasks:

With localized: true, all languages share one tree, and editors translate the labels. Read the menu in the page's language:

const menu = await cms.getGlobal("mainMenu", { locale: "de" });

A menu per language explains labels, links and a separate tree per language.