Menu
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
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: [...] }]
labelis 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>
<script setup lang="ts">
import { menuItems } from "tenoncms/client";
const { data: menu } = await useTenon().getGlobal("mainMenu");
const items = computed(() => menuItems(menu.value?.items));
</script>
<template>
<nav aria-label="Main">
<ul>
<li v-for="item in items" :key="item.id">
<NuxtLink v-if="item.href" :to="item.href" :target="item.target">{{ item.label }}</NuxtLink>
<span v-else>{{ item.label }}</span>
<ul v-if="item.children.length">
<li v-for="child in item.children" :key="child.id">
<NuxtLink :to="child.href!" :target="child.target">{{ child.label }}</NuxtLink>
</li>
</ul>
</li>
</ul>
</nav>
</template>
import { createServerFn } from "@tanstack/react-start";
import { menuItems } from "tenoncms";
import cms from "../../tenon.config";
// Call it in a route's loader and pass the result to <Nav>.
export const getMenu = createServerFn().handler(async () =>
menuItems((await cms.getGlobal("mainMenu")).items),
);
export function Nav({ items }: { items: Awaited<ReturnType<typeof getMenu>> }) {
return (
<nav aria-label="Main">
<ul>
{items.map((item) => (
<li key={item.id}>
{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 key={child.id}>
<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 }),
| Item | In each language |
|---|---|
| The tree | The same. An item added in one language shows in all. |
| An empty label | The title of the linked page in that language. |
| A typed label | Its own text. |
| A link to a page | The 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
"menu".0 is flat (default), 1 allows one level, -1 has no limit. A heading counts as a level.link and heading are taken.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:
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()takesdepthandblocks, likemenuPreset().- Only a menu without a limit (
depth: -1) needsblocksindefineCms(). Its items contain themselves by name. A menu with a limit returns emptyblocks. - With
depth: -1, your own blocks get the prefixmenu, sobuttonis stored as the shared blockmenuButton. ItstypeinmenuItems()staysbutton.