NavigationLinks
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 { NavigationLinks } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationLinksItem, NavigationLinksLink, NavigationLinksList, NavigationLinksRoot } from "#ui/navigation-links"
const NavigationLinksBasicMessage = 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 NavigationLinksBasic = Html.component({
name: "NavigationLinksBasic",
model: Schema.Struct({ path: Schema.String }),
message: NavigationLinksBasicMessage
}).make({
init: () => ({ path: "#overview" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) =>
NavigationLinks(h, {
view: (navigationLinks) =>
NavigationLinksRoot(h, navigationLinks, { label: "Main" }).children(
NavigationLinksList(h, navigationLinks).children(
pages.map((page) =>
NavigationLinksItem(h, navigationLinks, { key: page.path }).children(
NavigationLinksLink(h, navigationLinks, { href: page.path, active: page.path === model.path })
.on("click", NavigationLinksBasicMessage.ClickedLink({ path: page.path }), { preventDefault: "always" })
.children(page.label)
)
)
)
)
})
})
Installation
Once the project is set up (see Installation), add the component and every Component file it builds on:
pnpm dlx shadcn@4.23.1 add @effectcn/navigation-links
Set up the project
Once per project: create the app, then add the compiler options, Tailwind CSS and the import aliases, as Installation shows.
components.jsonis only for the CLI.Add the base
Once per project, what
@effectcn/baseinstalls with the CLI: runpnpm add tailwind-merge, then add these files.src/lib/cn.ts: the cn helper.src/styles/tokens.css: the theme tokens.src/styles/themes.css: the other twelve themes.src/styles/tailwind.css: replace yours with the Tailwind mapping: it imports Tailwind and both files, adds the dark variant and maps the tokens to colours.
Copy the component into your project
Save it as
src/ui/navigation-links.ts. 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-links.ts import type { Component } from "@effect/ui" import type { Html } from "@effect/ui/html" import type { NavigationLinks } from "@effect/ui-kit" import { cn } from "#lib/cn" /** What a `NavigationLinks`' `view` closure receives. */ export type NavigationLinksParts = Component.Headless.Parts<typeof NavigationLinks> interface ClassOption { readonly class?: string | undefined } export type NavigationLinksVariant = "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 NavigationLinksRoot = <Msg>( h: Html.H<Msg>, navigationLinks: NavigationLinksParts, options: ClassOption & { readonly label: string readonly orientation?: "horizontal" | "vertical" | undefined readonly variant?: NavigationLinksVariant | undefined } ) => navigationLinks.root( h.nav.class(cn("group/navigation-links 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 NavigationLinksList = <Msg>( h: Html.H<Msg>, navigationLinks: NavigationLinksParts, options: ClassOption = {} ) => navigationLinks.list( h.ul.class(cn( "m-0 flex list-none items-center gap-1 p-0", "group-data-[orientation=vertical]/navigation-links:flex-col group-data-[orientation=vertical]/navigation-links:items-stretch", "group-data-[orientation=vertical]/navigation-links:gap-0.5", "group-data-[variant=segmented]/navigation-links:rounded-lg group-data-[variant=segmented]/navigation-links:bg-muted", "group-data-[variant=segmented]/navigation-links:p-1", options.class )) ) /** The `<li>` around one link. */ export const NavigationLinksItem = <Msg>( h: Html.H<Msg>, navigationLinks: NavigationLinksParts, options: ClassOption & { readonly key: string } ) => navigationLinks.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 NavigationLinksLink = <Msg>( h: Html.H<Msg>, navigationLinks: NavigationLinksParts, options: ClassOption & { readonly href: string readonly active?: boolean | undefined readonly disabled?: boolean | undefined } ) => navigationLinks.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-brand-wash data-[active]:text-foreground", "aria-disabled:pointer-events-none aria-disabled:opacity-50", "group-data-[variant=segmented]/navigation-links:h-7 group-data-[variant=segmented]/navigation-links:rounded-sm", "group-data-[variant=segmented]/navigation-links:hover:bg-transparent", "group-data-[variant=segmented]/navigation-links:hover:text-foreground", "group-data-[variant=segmented]/navigation-links:data-[active]:bg-background", "group-data-[variant=segmented]/navigation-links:data-[active]:text-foreground", "group-data-[variant=segmented]/navigation-links: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 NavigationLinksTitle = <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 NavigationLinksBadge = <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 ))
Then import it:
import { NavigationLinksRoot, NavigationLinksList, NavigationLinksItem, NavigationLinksLink, NavigationLinksTitle, NavigationLinksBadge } from "#ui/navigation-links"
Usage
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { NavigationLinks } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationLinksItem, NavigationLinksLink, NavigationLinksList, NavigationLinksRoot } from "#ui/navigation-links"
const NavigationLinksBasicMessage = 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 NavigationLinksBasic = Html.component({
name: "NavigationLinksBasic",
model: Schema.Struct({ path: Schema.String }),
message: NavigationLinksBasicMessage
}).make({
init: () => ({ path: "#overview" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) =>
NavigationLinks(h, {
view: (navigationLinks) =>
NavigationLinksRoot(h, navigationLinks, { label: "Main" }).children(
NavigationLinksList(h, navigationLinks).children(
pages.map((page) =>
NavigationLinksItem(h, navigationLinks, { key: page.path }).children(
NavigationLinksLink(h, navigationLinks, { href: page.path, active: page.path === model.path })
.on("click", NavigationLinksBasicMessage.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 { NavigationLinks } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationLinksItem, NavigationLinksLink, NavigationLinksList, NavigationLinksRoot, NavigationLinksTitle } from "#ui/navigation-links"
const NavigationLinksVerticalMessage = 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 NavigationLinksVertical = Html.component({
name: "NavigationLinksVertical",
model: Schema.Struct({ path: Schema.String }),
message: NavigationLinksVerticalMessage
}).make({
init: () => ({ path: "#installation" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) =>
NavigationLinks(h, {
props: { orientation: "vertical" },
view: (navigationLinks) =>
NavigationLinksRoot(h, navigationLinks, { label: "Documentation", orientation: "vertical", class: "w-56" }).children(
NavigationLinksList(h, navigationLinks).children(
sections.flatMap((section) => [
NavigationLinksTitle(h, { key: section.title }).children(section.title),
...section.pages.map((page) =>
NavigationLinksItem(h, navigationLinks, { key: page.path }).children(
NavigationLinksLink(h, navigationLinks, { href: page.path, active: page.path === model.path })
.on("click", NavigationLinksVerticalMessage.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 { NavigationLinks } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationLinksBadge, NavigationLinksItem, NavigationLinksLink, NavigationLinksList, NavigationLinksRoot } from "#ui/navigation-links"
const NavigationLinksIconsMessage = 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 NavigationLinksIcons = Html.component({
name: "NavigationLinksIcons",
model: Schema.Struct({ path: Schema.String }),
message: NavigationLinksIconsMessage
}).make({
init: () => ({ path: "#inbox" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) =>
NavigationLinks(h, {
props: { orientation: "vertical" },
view: (navigationLinks) =>
NavigationLinksRoot(h, navigationLinks, { label: "Mailboxes", orientation: "vertical", class: "w-56" }).children(
NavigationLinksList(h, navigationLinks).children(
pages.map((page) =>
NavigationLinksItem(h, navigationLinks, { key: page.path }).children(
NavigationLinksLink(h, navigationLinks, { href: page.path, active: page.path === model.path })
.on("click", NavigationLinksIconsMessage.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 : NavigationLinksBadge(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 { NavigationLinks } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationLinksItem, NavigationLinksLink, NavigationLinksList, NavigationLinksRoot } from "#ui/navigation-links"
const NavigationLinksSegmentedMessage = 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 NavigationLinksSegmented = Html.component({
name: "NavigationLinksSegmented",
model: Schema.Struct({ path: Schema.String }),
message: NavigationLinksSegmentedMessage
}).make({
init: () => ({ path: "#issues" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) =>
NavigationLinks(h, {
view: (navigationLinks) =>
NavigationLinksRoot(h, navigationLinks, { label: "Repository", variant: "segmented" }).children(
NavigationLinksList(h, navigationLinks).children(
pages.map((page) =>
NavigationLinksItem(h, navigationLinks, { key: page.path }).children(
NavigationLinksLink(h, navigationLinks, {
href: page.path,
active: page.path === model.path,
disabled: page.disabled
})
.on(
"click",
() => page.disabled === true ? undefined : NavigationLinksSegmentedMessage.ClickedLink({ path: page.path }),
{ preventDefault: "always" }
)
.children(page.label)
)
)
)
)
})
})
Two lists in a header
Two lists 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 { NavigationLinks } from "@effect/ui-kit"
import { Schema } from "effect"
import { NavigationLinksItem, NavigationLinksLink, NavigationLinksList, NavigationLinksRoot } from "#ui/navigation-links"
const NavigationLinksHeaderMessage = 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 NavigationLinksHeader = Html.component({
name: "NavigationLinksHeader",
model: Schema.Struct({ path: Schema.String }),
message: NavigationLinksHeaderMessage
}).make({
init: () => ({ path: "#pricing" }),
update: {
ClickedLink: ($, { path }) => $.evolve({ path })
},
view: (h, model) => {
const links = (key: string, label: string, pages: ReadonlyArray<{ readonly path: string; readonly label: string }>) =>
NavigationLinks(h, {
key,
view: (navigationLinks) =>
NavigationLinksRoot(h, navigationLinks, { label }).children(
NavigationLinksList(h, navigationLinks).children(
pages.map((page) =>
NavigationLinksItem(h, navigationLinks, { key: page.path }).children(
NavigationLinksLink(h, navigationLinks, { href: page.path, active: page.path === model.path })
.on("click", NavigationLinksHeaderMessage.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"),
links("primary", "Main", primary),
h.div.class("ml-auto").children(links("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. |
Data attributes
| Attribute | Description |
|---|---|
data-active | On the active link, which also gets aria-current="page". |