import { ChevronRight } from "lucide-react"
import * as React from "react"
import { textVariants } from "~/components/ui/text"
import { cn } from "~/lib/utils"
/** Where the page sits in the app: a labelled navigation landmark holding a `BreadcrumbList`. */
function Breadcrumb({ "aria-label": label = "Breadcrumb", ...props }: React.ComponentProps<"nav">) {
return <nav data-slot="breadcrumb" aria-label={label} {...props} />
}
type BreadcrumbListProps = React.ComponentProps<"ol"> & {
/**
* What a trail too long for its space does. `wrap` breaks onto more lines. `scroll` keeps one
* line that scrolls sideways, starting at the page you're on, and fades where there's more.
*/
overflow?: "wrap" | "scroll"
}
/** The trail, in order. */
function BreadcrumbList({ overflow = "wrap", className, ...props }: BreadcrumbListProps) {
const ref = React.useRef<HTMLOListElement>(null)
const [more, setMore] = React.useState({ before: false, after: false })
React.useLayoutEffect(() => {
const list = ref.current
if (overflow !== "scroll" || list === null) return undefined
const measure = () =>
setMore({
before: list.scrollLeft > 1,
after: list.scrollLeft + list.clientWidth < list.scrollWidth - 1,
})
// Start at the end, the page you're on, and stay there as the steps change size (fonts
// load, the space changes) until you scroll the trail yourself.
let pinned = true
const pin = () => {
if (pinned) list.scrollLeft = list.scrollWidth
measure()
}
const unpin = () => {
pinned = false
}
pin()
list.addEventListener("scroll", measure, { passive: true })
list.addEventListener("pointerdown", unpin)
list.addEventListener("wheel", unpin, { passive: true })
list.addEventListener("focusin", unpin)
const resized = new ResizeObserver(pin)
resized.observe(list)
for (const step of list.children) resized.observe(step)
return () => {
list.removeEventListener("scroll", measure)
list.removeEventListener("pointerdown", unpin)
list.removeEventListener("wheel", unpin)
list.removeEventListener("focusin", unpin)
resized.disconnect()
}
}, [overflow])
return (
<ol
ref={ref}
data-slot="breadcrumb-list"
data-overflow={overflow}
data-more-before={more.before || undefined}
data-more-after={more.after || undefined}
className={cn(
textVariants({ size: "sm", tone: "muted" }),
"flex flex-wrap items-center gap-xs wrap-break-word sm:gap-md",
"data-[overflow=scroll]:[scrollbar-width:none] data-[overflow=scroll]:flex-nowrap data-[overflow=scroll]:overflow-x-auto data-[overflow=scroll]:whitespace-nowrap",
// Fades out at an end with more past it.
"data-more-after:mask-r-from-85% data-more-before:mask-l-from-85%",
className,
)}
{...props}
/>
)
}
/** One step: a `BreadcrumbLink`, or the `BreadcrumbPage` you're on. */
function BreadcrumbItem({ className, ...props }: React.ComponentProps<"li">) {
return (
<li
data-slot="breadcrumb-item"
className={cn("inline-flex items-center gap-xs", className)}
{...props}
/>
)
}
/** A step you can go back to. */
function BreadcrumbLink({ className, children, ...props }: React.ComponentProps<"a">) {
return (
<a
data-slot="breadcrumb-link"
className={cn(
"transition-colors outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/30",
className,
)}
{...props}
>
{children}
</a>
)
}
/** The page you're on. */
function BreadcrumbPage({ className, ...props }: React.ComponentProps<"span">) {
return (
<span
data-slot="breadcrumb-page"
aria-current="page"
className={cn(textVariants({ weight: "normal" }), "text-foreground", className)}
{...props}
/>
)
}
/** The mark between steps, a chevron unless you pass your own. */
function BreadcrumbSeparator({ children, className, ...props }: React.ComponentProps<"li">) {
return (
<li
data-slot="breadcrumb-separator"
role="presentation"
aria-hidden="true"
className={cn("[&>svg]:size-3.5", className)}
{...props}
>
{children ?? <ChevronRight />}
</li>
)
}
export type { BreadcrumbListProps }
export {
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbList,
BreadcrumbPage,
BreadcrumbSeparator,
}