Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. 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
      ))
    
  3. Import it

    import { NavigationMenuRoot, NavigationMenuList, NavigationMenuItem, NavigationMenuLink, NavigationMenuTitle, NavigationMenuBadge } from "#ui/navigation-menu"
    

Usage

Basic.ts
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.

Icons and counts

Links with an icon in front and a badge at the end.

Segmented, with a disabled link

The segmented variant puts the links on a muted track. The arrow keys skip a link with aria-disabled.

Two menus under one parent each take a key and their own aria-label, and keep their arrow keys to themselves.

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
rootThe <nav> landmark. Give it an aria-label when the page has more than one.
listThe <ul> inside the root. It carries the arrow keys.
itemAn <li> inside the list, around one link.
linkAn <a>, { element, active }. The href and the click are the consumer's.

API reference

Props

PropTypeDescription
orientation"horizontal" | "vertical"Right and Left move focus between the links by default; "vertical" uses Down and Up.

Events

EventDescription
(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

AttributeDescription
data-activeOn the active link, which also gets aria-current="page".

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.