Presets

Menu

A navigation menu that editors arrange as a tree.

menuPreset() adds a global with a menu that editors arrange as a tree. It shows on the Settings page and is probably the closest built-in thing to menus on Wordpress or similar CMSs.

Example

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

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

The menu holds items. Each item is a link with an optional label, or, with depth of 1 or more, a heading that groups links.

menuItems() from tenoncms turns a read into entries with label, href, target and children:

import { menuItems } from "tenoncms";

const menu = await cms.getGlobal("mainMenu");
const items = menuItems(menu.items);
// [{ type: "link", label: "About", href: "/about", 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.

Render it

---
import { menuItems } from "tenoncms";

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

<nav aria-label="Main">
  <ul>
    {items.map((item) => (
      <li>
        {item.href ? <a href={item.href} target={item.target}>{item.label}</a> : <span>{item.label}</span>}
        {item.children.length > 0 && (
          <ul>
            {item.children.map((child) => (
              <li><a href={child.href} target={child.target}>{child.label}</a></li>
            ))}
          </ul>
        )}
      </li>
    ))}
  </ul>
</nav>

See Menus for a menu of any depth.

More than one menu

Give each menu its own name:

presets: [
  menuPreset({ name: "mainMenu", label: "Main menu", depth: 1 }),
  menuPreset({ name: "footerMenu", label: "Footer menu" }),
],

A menu per language

With localized: true, all languages share one tree by default. Editors translate the labels, and links point to the page in each language:

menuPreset({ name: "mainMenu", localized: true }),
ItemIn each language
The treeThe same. An item added in one language shows in all.
An empty labelThe title of the linked page in that language.
A typed labelIts own text.
A link to a pageThe page's URL in that language. Left out when there is none.

Read it in the page's locale:

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

To give each language its own tree, choose Separate per language in the menu's … menu, or set syncStructure: false on the global. See Localization.

Own item types

blocks adds your own item types, like a button. They can go wherever a link goes:

menuPreset({
  name: "mainMenu",
  blocks: {
    button: {
      label: "Button",
      fields: {
        label: { type: "text", required: true },
        link: { type: "link", required: true },
        style: { type: "select", options: ["primary", "secondary"] },
      },
    },
  },
}),

menuItems() keeps the item's own fields and sets type to its key:

for (const item of menuItems(menu.items)) {
  if (item.type === "button") console.log(item.label, item.href, item.style);
}

Extra fields

fields adds fields after the menu items, like a call to action next to the menu:

menuPreset({
  name: "mainMenu",
  fields: {
    cta: { type: "link", label: "Call to action" },
  },
}),

Options

The global name. Default "menu".The levels below the top. 0 is flat (default), 1 allows one level, -1 has no limit. A heading counts as a level.Your own item types. They can go wherever a link goes. The keys link and heading are taken.More fields, after the menu items.

All other options of a global pass through, like label, localized or access.

Build your own

menu() returns the menu fields and the blocks they need. Use it to put a menu into your own global:

tenon.config.ts
import { defineCms, defineGlobal } from "tenoncms";
import { menu } from "tenoncms/presets";

const mainMenu = menu({ depth: -1 });

export default defineCms({
  // ...
  blocks: mainMenu.blocks,
  globals: {
    header: defineGlobal({
      label: "Header",
      fields: {
        logo: { type: "image" },
        ...mainMenu.fields,
      },
    }),
  },
});
  • menu() takes depth and blocks, like menuPreset().
  • Only a menu without a limit (depth: -1) needs blocks in defineCms(). Its items contain themselves by name. A menu with a limit returns empty blocks.
  • With depth: -1, your own blocks get the prefix menu, so button is stored as the shared block menuButton. Its type in menuItems() stays button.