# Navigation Menu

> An app's top links, where some open a panel of pages with a line about each.

Page: https://uicontext.com/docs/components/navigation-menu

Group: Navigation.

## When to use

An app's top links, where some open a panel of pages with a line about each. Use [Sidebar](https://uicontext.com/docs/components/sidebar.md) instead when the links run down a sidebar.

## Usage

```ts
import { NavigationMenu } from "./ui/uicontext/components/NavigationMenu.ts"

export const Main = <M>(h: Html.H<M>, current: string) =>
	NavigationMenu(h, {
		props: {
			label: "Main",
			current,
			items: [
				{ _tag: "Link", label: "Home", href: "/" },
				{ _tag: "Group", value: "reports", label: "Reports", links: [{ label: "Revenue", href: "/reports/revenue", description: "MRR and churn" }] },
			],
		},
	})
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### An app's header

The example the page opens with: Navigation Menu in “An app's header”.

```ts title="src/demo.ts" source="www/src/examples/navigation-menu/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { NavigationMenu } from "./ui/uicontext/components/NavigationMenu.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	header: {
		display: "flex",
		alignItems: "center",
		gap: space.lg,
		inlineSize: "100%",
		minInlineSize: 0,
		paddingBlock: space.xs,
		paddingInline: space.md,
		borderWidth: stroke.hairline,
		borderStyle: "solid",
		borderColor: color.line,
		borderRadius: corner.card,
		backgroundColor: color.panel,
	},
	brand: { flexShrink: 0, fontSize: font.sm, fontWeight: 700, color: color.fg },
	nav: { flexGrow: 1, minInlineSize: 0 },
})

// an analytics app's header: Overview, and Reports and Data each open a panel of pages
export const AnalyticsHeader = <M>(h: Html.H<M>) =>
	h.header.attrs(sx(styles.header)).children(
		h.span.attrs(sx(styles.brand)).children("Northwind"),
		h.div.attrs(sx(styles.nav)).children(
			NavigationMenu(h, {
				props: {
					label: "Main",
					current: "#reports/revenue",
					items: [
						{ _tag: "Link", label: "Overview", href: "#overview" },
						{
							_tag: "Group",
							value: "reports",
							label: "Reports",
							links: [
								{ label: "Revenue", href: "#reports/revenue", description: "MRR, expansion and churn by month" },
								{ label: "Retention", href: "#reports/retention", description: "Cohorts by signup month" },
								{ label: "Funnels", href: "#reports/funnels", description: "Where trials drop off" },
								{ label: "Pipeline", href: "#reports/pipeline", description: "Open deals by stage and owner" },
								{ label: "Support", href: "#reports/support", description: "First reply and resolution times" },
								{ label: "Scheduled", href: "#reports/scheduled", description: "Reports emailed every Monday" },
							],
						},
						{
							_tag: "Group",
							value: "data",
							label: "Data",
							links: [
								{ label: "Sources", href: "#data/sources" },
								{ label: "Models", href: "#data/models" },
								{ label: "Sync history", href: "#data/syncs" },
							],
						},
						{ _tag: "Link", label: "Alerts", href: "#alerts" },
					],
				},
			}),
		),
	)
```

### Docs tabs

Links without descriptions make a short list. `columns: 2` splits a long one.

```ts title="src/docs.ts" source="www/src/examples/navigation-menu/docs.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { NavigationMenu } from "./ui/uicontext/components/NavigationMenu.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, stroke } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	bar: { inlineSize: "100%", borderBlockEndWidth: stroke.hairline, borderBlockEndStyle: "solid", borderBlockEndColor: color.line },
})

// a developer docs site's tabs: guides and the API reference open short lists, the page showing is marked
export const DocsTabs = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.bar)).children(
		NavigationMenu(h, {
			key: "docs",
			props: {
				label: "Docs",
				current: "#docs/webhooks",
				items: [
					{ _tag: "Link", label: "Quickstart", href: "#docs/quickstart" },
					{
						_tag: "Group",
						value: "guides",
						label: "Guides",
						links: [
							{ label: "Authentication", href: "#docs/auth" },
							{ label: "Webhooks", href: "#docs/webhooks" },
							{ label: "Pagination", href: "#docs/pagination" },
							{ label: "Errors and retries", href: "#docs/errors" },
						],
					},
					{
						_tag: "Group",
						value: "api",
						label: "API",
						columns: 2,
						links: [
							{ label: "Customers", href: "#api/customers" },
							{ label: "Invoices", href: "#api/invoices" },
							{ label: "Payments", href: "#api/payments" },
							{ label: "Refunds", href: "#api/refunds" },
							{ label: "Subscriptions", href: "#api/subscriptions" },
							{ label: "Events", href: "#api/events" },
						],
					},
					{ _tag: "Link", label: "Changelog", href: "#changelog" },
				],
			},
		}),
	)
```

## API reference

**Props**

| Name | Type | Description |
| --- | --- | --- |
| `label` (required) | `string` | The landmark's name: "Main", "Docs" |
| `items` (required) | `NavigationMenuItem[]` | — |
| `current` | `string` | The href of the page showing |

## Keyboard and accessibility

### Keys

| Keys | Does |
| --- | --- |
| Right arrow, Left arrow | Move along the bar |
| Down arrow, Enter | Open a link's panel |
| Tab | Walk into the open panel, then on past it |
| Escape | Close the panel and go back to its link |

On a Mac, Control is Command and Alt is Option. Left and Right trade places in right-to-left text.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/navigation-menu
```

With npm: `npx shadcn@latest add checkfu/uicontext/navigation-menu`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/navigation-menu`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/navigation-menu`.

It writes these files:

- `src/ui/uicontext/components/Motion.ts`
- `src/ui/uicontext/components/NavigationMenu.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/NavigationMenu.ts](https://github.com/checkfu/uicontext/blob/main/src/NavigationMenu.ts)

## Related

- [App Shell](https://uicontext.com/docs/components/app-shell.md) (`app-shell`): The frame of an app: a sidebar, the page and an optional side panel, on any screen size.
- [Tab Strip](https://uicontext.com/docs/components/tab-strip.md) (`tab-strip`): Document tabs over the page to open, close and drag to reorder.
- [Sidebar](https://uicontext.com/docs/components/sidebar.md) (`sidebar`): A navigation tree of workspaces, teams and pages.
- [Tree View](https://uicontext.com/docs/components/tree-view.md) (`tree-view`): Nested rows you open and close, such as files and folders. Children can load when opened.
- [Page Header](https://uicontext.com/docs/components/page-header.md) (`page-header`): A page's title, description, breadcrumb and actions.
- [Tabs](https://uicontext.com/docs/components/tabs.md) (`tabs`): Switch between panels, or between views of the same data.
