Menus
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:
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:
depth | Menu |
|---|---|
0 | A flat list of links. The default. |
1 | Links with one level of children. |
-1 | Any 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: [...] }]
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.
menuItems()makes no queries. Read the menu withpopulate, 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>
<script setup lang="ts">
type Item = { id: string; label: string; href: string | null; target?: string; children: Item[] };
defineProps<{ items: Item[] }>();
</script>
<template>
<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>
<MenuList v-if="item.children.length" :items="item.children" />
</li>
</ul>
</template>
import { useLocation } from "@tanstack/react-router";
type Item = { id: string; label: string; href: string | null; target?: string; children: Item[] };
export function MenuList({ items }: { items: Item[] }) {
const current = useLocation().pathname;
return (
<ul>
{items.map((item) => (
<li key={item.id}>
{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 && <MenuList 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>
<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">
<MenuList :items="items" />
</nav>
</template>
import { createRootRoute } from "@tanstack/react-router";
import { createServerFn } from "@tanstack/react-start";
import { menuItems } from "tenoncms";
import cms from "../../tenon.config";
import { MenuList } from "../components/MenuList";
const getMenu = createServerFn().handler(async () =>
menuItems((await cms.getGlobal("mainMenu")).items),
);
export const Route = createRootRoute({
loader: () => getMenu(),
component: Root,
});
function Root() {
const items = Route.useLoaderData();
return (
<nav aria-label="Main">
<MenuList items={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:
- More than one menu, like a footer menu.
- A menu per language.
- Own item types, like a button.
- Extra fields next to the items, like a call to action.
- A menu inside your own global, like a header with a logo.
Menus in more than one language
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.
Related resources
- Menu preset: all options of
menuPreset()andmenu(). - Link field: what a menu link can point to.
- Pages and SEO: the pages a menu links to.