NavigationMenu
A navigation landmark with a list of links, where the link to the page being shown is marked as current.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { NavigationMenu } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuRoot } from "#ui/navigation-menu"
const NavigationMenuBasicMessage = UiSchema.TaggedEnum({
ClickedLink: { path: Schema.String }
})
const pages = [
{ path: "#overview", label: "Overview" },
{ path: "#customers", label: "Customers" },
{ path: "#products", label: "Products" },
{ path: "#settings", label: "Settings" }
]
export const NavigationMenuBasic = Html.component({
name: "NavigationMenuBasic",
model: Schema.Struct({ path: Schema.String }),
message: NavigationMenuBasicMessage
}).make({
init: () => ({ path: "#overview" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) =>
NavigationMenu(h, {
view: (navigationMenu) =>
NavigationMenuRoot(h, navigationMenu, { label: "Main" }).children(
NavigationMenuList(h, navigationMenu).children(
pages.map((page) =>
NavigationMenuItem(h, navigationMenu, { key: page.path }).children(
// In an app the current page comes from the route, and the click is the router's.
NavigationMenuLink(h, navigationMenu, { href: page.path, active: page.path === model.path })
.on("click", NavigationMenuBasicMessage.ClickedLink({ path: page.path }), { preventDefault: "always" })
.children(page.label)
)
)
)
)
})
})
Installation
Set up the project
Once per project: the packages, Tailwind CSS, the theme tokens and the
cnhelper. Follow Installation.Copy the component into your project
The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.
src/ui/navigation-menu.ts import type { Component } from "@effect/ui" import type { Html } from "@effect/ui/html" import type { NavigationMenu } from "@effect/ui-kit" import { cn } from "#lib/cn" type NavigationMenuParts = Component.Headless.Parts<typeof NavigationMenu> interface ClassOption { readonly class?: string | undefined } export type NavigationMenuVariant = "default" | "segmented" /** * The `<nav>` landmark. `orientation` lays the links out in a row or a column * (pass the same one to the kit's props, for the arrow keys), and `variant` * `"segmented"` puts them on a muted track. */ export const NavigationMenuRoot = <Msg>( h: Html.H<Msg>, navigationMenu: Pick<NavigationMenuParts, "root">, options: ClassOption & { readonly label: string readonly orientation?: "horizontal" | "vertical" | undefined readonly variant?: NavigationMenuVariant | undefined } ) => navigationMenu.root( h.nav.class(cn("group/navigation-menu text-[13px]", options.class)).attrs({ "aria-label": options.label, "data-orientation": options.orientation ?? "horizontal", "data-variant": options.variant ?? "default" }) ) /** The `<ul>` of the links. */ export const NavigationMenuList = <Msg>( h: Html.H<Msg>, navigationMenu: Pick<NavigationMenuParts, "list">, options: ClassOption = {} ) => navigationMenu.list( h.ul.class(cn( "m-0 flex list-none items-center gap-1 p-0", "group-data-[orientation=vertical]/navigation-menu:flex-col group-data-[orientation=vertical]/navigation-menu:items-stretch", "group-data-[orientation=vertical]/navigation-menu:gap-0.5", "group-data-[variant=segmented]/navigation-menu:rounded-lg group-data-[variant=segmented]/navigation-menu:bg-muted", "group-data-[variant=segmented]/navigation-menu:p-1", options.class )) ) /** The `<li>` around one link. */ export const NavigationMenuItem = <Msg>( h: Html.H<Msg>, navigationMenu: Pick<NavigationMenuParts, "item">, options: ClassOption & { readonly key: string } ) => navigationMenu.item(h.li.key(options.key).class(cn(options.class))) /** * One link. `active` marks the page being shown with a filled background, and * `disabled` sets `aria-disabled`, which the arrow keys skip. Chain the click * handler and the children on the result. */ export const NavigationMenuLink = <Msg>( h: Html.H<Msg>, navigationMenu: Pick<NavigationMenuParts, "link">, options: ClassOption & { readonly href: string readonly active?: boolean | undefined readonly disabled?: boolean | undefined } ) => navigationMenu.link({ element: h.a .class(cn( "flex h-8 items-center gap-2 rounded-md px-3 font-medium whitespace-nowrap text-muted-foreground no-underline", "transition-colors duration-150 hover:bg-accent hover:text-accent-foreground", "data-[active]:bg-accent data-[active]:text-accent-foreground", "aria-disabled:pointer-events-none aria-disabled:opacity-50", "group-data-[variant=segmented]/navigation-menu:h-7 group-data-[variant=segmented]/navigation-menu:rounded-sm", "group-data-[variant=segmented]/navigation-menu:hover:bg-transparent", "group-data-[variant=segmented]/navigation-menu:hover:text-foreground", "group-data-[variant=segmented]/navigation-menu:data-[active]:bg-background", "group-data-[variant=segmented]/navigation-menu:data-[active]:text-foreground", "group-data-[variant=segmented]/navigation-menu:data-[active]:shadow-sm", "[&_svg]:size-[15px] [&_svg]:shrink-0", options.class )) .attrs({ href: options.href, "aria-disabled": options.disabled === true ? "true" : undefined }), active: options.active }) /** A heading between the links of a vertical list. It is an `<li>` the arrow keys pass over. */ export const NavigationMenuTitle = <Msg>(h: Html.H<Msg>, options: ClassOption & { readonly key: string }) => h.li.key(options.key).attr("role", "presentation").class(cn( "px-3 pt-4 pb-1 text-xs font-medium text-muted-foreground first:pt-1", options.class )) /** A count at the end of a link. */ export const NavigationMenuBadge = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) => h.span.class(cn( "ml-auto rounded-full bg-brand-wash px-1.5 text-[11px] leading-[18px] font-medium text-foreground tabular-nums", options.class ))Import it
import { NavigationMenuRoot, NavigationMenuList, NavigationMenuItem, NavigationMenuLink, NavigationMenuTitle, NavigationMenuBadge } from "#ui/navigation-menu"
Usage
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { NavigationMenu } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuRoot } from "#ui/navigation-menu"
const NavigationMenuBasicMessage = UiSchema.TaggedEnum({
ClickedLink: { path: Schema.String }
})
const pages = [
{ path: "#overview", label: "Overview" },
{ path: "#customers", label: "Customers" },
{ path: "#products", label: "Products" },
{ path: "#settings", label: "Settings" }
]
export const NavigationMenuBasic = Html.component({
name: "NavigationMenuBasic",
model: Schema.Struct({ path: Schema.String }),
message: NavigationMenuBasicMessage
}).make({
init: () => ({ path: "#overview" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) =>
NavigationMenu(h, {
view: (navigationMenu) =>
NavigationMenuRoot(h, navigationMenu, { label: "Main" }).children(
NavigationMenuList(h, navigationMenu).children(
pages.map((page) =>
NavigationMenuItem(h, navigationMenu, { key: page.path }).children(
// In an app the current page comes from the route, and the click is the router's.
NavigationMenuLink(h, navigationMenu, { href: page.path, active: page.path === model.path })
.on("click", NavigationMenuBasicMessage.ClickedLink({ path: page.path }), { preventDefault: "always" })
.children(page.label)
)
)
)
)
})
})
Examples
Vertical, with sections
A sidebar: one list with titles between the links, moved through with ArrowDown and ArrowUp.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { NavigationMenu } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuRoot, NavigationMenuTitle } from "#ui/navigation-menu"
const NavigationMenuVerticalMessage = UiSchema.TaggedEnum({
ClickedLink: { path: Schema.String }
})
const sections = [
{
title: "Getting started",
pages: [
{ path: "#introduction", label: "Introduction" },
{ path: "#installation", label: "Installation" },
{ path: "#theming", label: "Theming" }
]
},
{
title: "Components",
pages: [
{ path: "#button", label: "Button" },
{ path: "#dialog", label: "Dialog" },
{ path: "#menu", label: "Menu" }
]
}
]
export const NavigationMenuVertical = Html.component({
name: "NavigationMenuVertical",
model: Schema.Struct({ path: Schema.String }),
message: NavigationMenuVerticalMessage
}).make({
init: () => ({ path: "#installation" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) =>
NavigationMenu(h, {
props: { orientation: "vertical" },
view: (navigationMenu) =>
NavigationMenuRoot(h, navigationMenu, { label: "Documentation", orientation: "vertical", class: "w-56" }).children(
// One list, so ArrowDown and ArrowUp cross the section titles.
NavigationMenuList(h, navigationMenu).children(
sections.flatMap((section) => [
NavigationMenuTitle(h, { key: section.title }).children(section.title),
...section.pages.map((page) =>
NavigationMenuItem(h, navigationMenu, { key: page.path }).children(
NavigationMenuLink(h, navigationMenu, { href: page.path, active: page.path === model.path })
.on("click", NavigationMenuVerticalMessage.ClickedLink({ path: page.path }), { preventDefault: "always" })
.children(page.label)
)
)
])
)
)
})
})
Icons and counts
Links with an icon in front and a badge at the end.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { NavigationMenu } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationMenuBadge, NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuRoot } from "#ui/navigation-menu"
const NavigationMenuIconsMessage = UiSchema.TaggedEnum({
ClickedLink: { path: Schema.String }
})
const pages = [
{ path: "#inbox", label: "Inbox", count: 12, icon: "M3 13h5l1.5 3h5L16 13h5M5 5h14l2 8v6H3v-6Z" },
{ path: "#drafts", label: "Drafts", count: 3, icon: "M6 3h9l4 4v14H6zM14 3v5h5" },
{ path: "#sent", label: "Sent", count: 0, icon: "M21 3 10 14M21 3l-7 18-4-7-7-4Z" },
{ path: "#archive", label: "Archive", count: 0, icon: "M3 4h18v4H3zM5 8v12h14V8M10 12h4" }
]
export const NavigationMenuIcons = Html.component({
name: "NavigationMenuIcons",
model: Schema.Struct({ path: Schema.String }),
message: NavigationMenuIconsMessage
}).make({
init: () => ({ path: "#inbox" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) =>
NavigationMenu(h, {
props: { orientation: "vertical" },
view: (navigationMenu) =>
NavigationMenuRoot(h, navigationMenu, { label: "Mailboxes", orientation: "vertical", class: "w-56" }).children(
NavigationMenuList(h, navigationMenu).children(
pages.map((page) =>
NavigationMenuItem(h, navigationMenu, { key: page.path }).children(
NavigationMenuLink(h, navigationMenu, { href: page.path, active: page.path === model.path })
.on("click", NavigationMenuIconsMessage.ClickedLink({ path: page.path }), { preventDefault: "always" })
.children(
h.el("svg")
.attrs({
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
"stroke-width": "2",
"stroke-linecap": "round",
"stroke-linejoin": "round",
"aria-hidden": "true"
})
.children(h.el("path").attr("d", page.icon)),
page.label,
page.count === 0 ? undefined : NavigationMenuBadge(h).children(String(page.count))
)
)
)
)
)
})
})
Segmented, with a disabled link
The segmented variant puts the links on a muted track. The arrow keys skip a link with aria-disabled.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { NavigationMenu } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuRoot } from "#ui/navigation-menu"
const NavigationMenuSegmentedMessage = UiSchema.TaggedEnum({
ClickedLink: { path: Schema.String }
})
const pages = [
{ path: "#code", label: "Code" },
{ path: "#issues", label: "Issues" },
{ path: "#pull-requests", label: "Pull requests" },
{ path: "#insights", label: "Insights", disabled: true },
{ path: "#settings", label: "Settings" }
]
export const NavigationMenuSegmented = Html.component({
name: "NavigationMenuSegmented",
model: Schema.Struct({ path: Schema.String }),
message: NavigationMenuSegmentedMessage
}).make({
init: () => ({ path: "#issues" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) =>
NavigationMenu(h, {
view: (navigationMenu) =>
NavigationMenuRoot(h, navigationMenu, { label: "Repository", variant: "segmented" }).children(
NavigationMenuList(h, navigationMenu).children(
pages.map((page) =>
NavigationMenuItem(h, navigationMenu, { key: page.path }).children(
NavigationMenuLink(h, navigationMenu, {
href: page.path,
active: page.path === model.path,
disabled: page.disabled
})
.on(
"click",
() => page.disabled === true ? undefined : NavigationMenuSegmentedMessage.ClickedLink({ path: page.path }),
{ preventDefault: "always" }
)
.children(page.label)
)
)
)
)
})
})
Two menus in a header
Two menus under one parent each take a key and their own aria-label, and keep their arrow keys to themselves.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { NavigationMenu } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuRoot } from "#ui/navigation-menu"
const NavigationMenuHeaderMessage = UiSchema.TaggedEnum({
ClickedLink: { path: Schema.String }
})
const primary = [
{ path: "#product", label: "Product" },
{ path: "#pricing", label: "Pricing" },
{ path: "#changelog", label: "Changelog" }
]
const account = [
{ path: "#sign-in", label: "Sign in" },
{ path: "#sign-up", label: "Get started" }
]
export const NavigationMenuHeader = Html.component({
name: "NavigationMenuHeader",
model: Schema.Struct({ path: Schema.String }),
message: NavigationMenuHeaderMessage
}).make({
init: () => ({ path: "#pricing" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) => {
// Two menus under one parent each take a key, and each landmark its own label.
const menu = (key: string, label: string, pages: ReadonlyArray<{ readonly path: string; readonly label: string }>) =>
NavigationMenu(h, {
key,
view: (navigationMenu) =>
NavigationMenuRoot(h, navigationMenu, { label }).children(
NavigationMenuList(h, navigationMenu).children(
pages.map((page) =>
NavigationMenuItem(h, navigationMenu, { key: page.path }).children(
NavigationMenuLink(h, navigationMenu, { href: page.path, active: page.path === model.path })
.on("click", NavigationMenuHeaderMessage.ClickedLink({ path: page.path }), { preventDefault: "always" })
.children(page.label)
)
)
)
)
})
return h.header.class("flex w-full max-w-2xl items-center gap-4 rounded-lg border bg-card px-4 py-2").children(
h.span.class("mr-2 text-sm font-semibold").children("Acme"),
menu("primary", "Main", primary),
h.div.class("ml-auto").children(menu("account", "Account", account))
)
}
})
Anatomy
The parts the view closure receives. Apply each one to an element of your own.
| Part | Description |
|---|---|
root | The <nav> landmark. Give it an aria-label when the page has more than one. |
list | The <ul> inside the root. It carries the arrow keys. |
item | An <li> inside the list, around one link. |
link | An <a>, { element, active }. The href and the click are the consumer's. |
API reference
Props
| Prop | Type | Description |
|---|---|---|
orientation | "horizontal" | "vertical" | Right and Left move focus between the links by default; "vertical" uses Down and Up. |
Events
| Event | Description |
|---|---|
(none) | It emits nothing: following a link is the browser's and the router's. Which link is active is the consumer's to say, usually from the route. |
Data attributes
| Attribute | Description |
|---|---|
data-active | On the active link, which also gets aria-current="page". |