Nav highlight
The shared highlight that glides between tabs, segments and nav rows.
Render NavHighlight inside the current item only, give that item relative isolate, and give the highlight its fill and radius with className. Wrap each set in NavHighlightGroup.
Between rows
"use client"
import * as React from "react"
import { NavHighlight, NavHighlightGroup } from "@/components/ui/nav-highlight"
const pages = ["Overview", "Tasks", "Invoices", "Releases"]
export default function NavHighlightBetweenRows() {
const [current, setCurrent] = React.useState("Tasks")
return (
<nav aria-label="Project pages" className="w-full max-w-48">
<NavHighlightGroup>
<ul className="flex flex-col gap-px">
{pages.map((page) => (
<li key={page}>
<button
type="button"
aria-current={page === current ? "page" : undefined}
onClick={() => setCurrent(page)}
className="relative isolate flex h-8 w-full items-center rounded-md px-2 text-left text-base text-muted-foreground outline-none focus-visible:ring-2 focus-visible:ring-ring aria-[current=page]:text-foreground"
>
{page}
{page === current ? <NavHighlight className="rounded-md bg-muted" /> : null}
</button>
</li>
))}
</ul>
</NavHighlightGroup>
</nav>
)
}
Two sets side by side
"use client"
import * as React from "react"
import { NavHighlight, NavHighlightGroup } from "@/components/ui/nav-highlight"
function Segments({ label, options }: { label: string; options: string[] }) {
const [selected, setSelected] = React.useState(options[0])
return (
<NavHighlightGroup>
<div role="group" aria-label={label} className="flex rounded-control bg-muted p-[3px]">
{options.map((option) => (
<button
key={option}
type="button"
aria-pressed={option === selected}
onClick={() => setSelected(option)}
className="relative isolate h-6 rounded-md px-2.5 text-sm text-muted-foreground outline-none focus-visible:ring-2 focus-visible:ring-ring aria-pressed:text-foreground"
>
{option}
{option === selected ? <NavHighlight className="rounded-md bg-background" /> : null}
</button>
))}
</div>
</NavHighlightGroup>
)
}
export default function NavHighlightTwoSetsSideBySide() {
return (
<div className="flex flex-wrap items-center justify-center gap-4">
<Segments label="Period" options={["Week", "Month", "Year"]} />
<Segments label="Currency" options={["EUR", "USD"]} />
</div>
)
}
Installation
npx shadcn@latest add @khaeli/nav-highlightbunx --bun shadcn@latest add @khaeli/nav-highlightInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- motion ^12.23.12
Accessible name
- The highlight is decoration and is hidden from assistive technology with
aria-hidden. It has no keyboard behaviour of its own. - The item that holds it must say it is current:
aria-current="page"on a nav row,aria-pressedon a segment,aria-selectedon a tab. @khaeli/tabs does this itself.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/nav-highlight.json