Components
Breadcrumb
Where you are, in DM Sans: each step back a muted link that turns White under the pointer, a small chevron between, the page itself in White.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add breadcrumb<script type="module" src="/spyglass-ds/elements/breadcrumb.js"></script>Usage
A creative inside a board, a setting inside a section: two or more levels deep.
Never
- A breadcrumb on a page one level deep
- Spectrum colors for the links
- Icons that move on hover
Adds with it: packages lucide-react
Props
The web component takes the same as attributes or properties (<spyglass-breadcrumb>); title is heading there.
<Breadcrumb>
| Prop | Type | Default |
|---|---|---|
items | Crumb[] The steps, the page last. | |
label | string The trail's name for assistive tech. | 'Breadcrumb' |
as | ElementType A router link component for the steps back. | |
onNavigate | (crumb: Crumb, event: MouseEvent) => void | |
className | string |
Examples
The same example in React and as web components; both render identically.
import { Breadcrumb } from '@/react/components/ui/Breadcrumb'
// A creative inside a board, with the way back.
export default function Board() {
return (
<Breadcrumb
className="max-w-full"
items={[
{ label: 'Boards', href: '#boards' },
{ label: 'Acme skincare', href: '#acme-skincare' },
{ label: 'Summer UGC, before and after hook' },
]}
/>
)
}<!-- A creative inside a board, with the way back. -->
<spyglass-breadcrumb class="max-w-full" items='[{"label":"Boards","href":"#boards"},{"label":"Acme skincare","href":"#acme-skincare"},{"label":"Summer UGC, before and after hook"}]'></spyglass-breadcrumb>Source
'use client'
import { Fragment, type ElementType, type MouseEvent } from 'react'
import { ChevronRight } from 'lucide-react'
import { cn } from '@/react/lib/cn'
// shadcn's breadcrumb in the brand: where you are, in DM Sans 14px. Each step back is a muted link that turns White
// under the pointer (160ms), a small chevron between them, and the page itself in White, the only step that truncates.
// `as` renders a router link component for the steps back (it gets `href` and `to`); `onNavigate` hears a click.
export interface Crumb {
label: string
href?: string
}
export interface BreadcrumbProps {
/** The steps, the page last. */
items: Crumb[]
/** The trail's name for assistive tech. */
label?: string
/** A router link component for the steps back. */
as?: ElementType
onNavigate?: (crumb: Crumb, event: MouseEvent) => void
className?: string
}
export function Breadcrumb({ items, label = 'Breadcrumb', as: Link = 'a', onNavigate, className }: BreadcrumbProps) {
return (
<nav data-ds="breadcrumb" className={cn('flex min-w-0 items-center gap-1.5 overflow-hidden type-ui', className)} aria-label={label}>
{items.map((crumb, i) => (
<Fragment key={i}>
{i > 0 && <ChevronRight size={14} strokeWidth={1.5} className="shrink-0 text-fg-faint rtl:-scale-x-100" aria-hidden="true" />}
{i < items.length - 1
? <Link href={crumb.href ?? '#'} to={Link === 'a' ? undefined : crumb.href} className="shrink-0 rounded-ds-sm text-fg-muted transition-[color] duration-160 ease-standard hover:text-fg" onClick={(event: MouseEvent) => onNavigate?.(crumb, event)}>{crumb.label}</Link>
: <span className="min-w-0 truncate text-fg" aria-current="page">{crumb.label}</span>}
</Fragment>
))}
</nav>
)
}
export default Breadcrumb// Joins class names, skipping falsy ones: cn('a', on && 'b', ['c', undefined]). It does not merge conflicting
// utilities (no tailwind-merge), as Vue's class binding does not: the library's classes are written not to compete,
// and a className from the caller is appended after them, as Vue appends a fallthrough class.
export type ClassValue = string | false | null | undefined | 0 | ClassValue[]
export function cn(...values: ClassValue[]): string {
const out: string[] = []
for (const v of values) {
if (!v) continue
if (Array.isArray(v)) {
const s = cn(...v)
if (s) out.push(s)
}
else out.push(v)
}
return out.join(' ')
}import { html } from 'lit'
import { ChevronRight } from 'lucide'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import { icon } from '@/elements/lib/icon'
import styles from 'virtual:tw'
// Breadcrumb.tsx as <spyglass-breadcrumb>: where you are, in DM Sans 14px. Each step back is a muted link that turns
// White under the pointer, a small chevron between them, and the page itself in White, the only step that truncates.
// `items` is a JSON attribute or the property. A click on a step back fires `navigate` (detail.crumb, detail.event:
// call detail.event.preventDefault() to route it yourself).
export interface Crumb { label: string, href?: string }
/**
* @summary Where you are: each step back a link, the page in White.
* @fires navigate - A step back was clicked (detail.crumb, detail.event).
*/
export class Breadcrumb extends DsElement {
static styles = [DsElement.styles, styles]
protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="breadcrumb"]'))
static properties = { items: { type: Array }, label: {} }
/** The steps, the page last: { label, href? }. */
items: Crumb[] = []
/** The trail's name for assistive tech. */
label = 'Breadcrumb'
render() {
const last = this.items.length - 1
return html`<nav data-ds="breadcrumb" class="flex min-w-0 items-center gap-1.5 overflow-hidden type-ui" aria-label=${this.label}>${this.items.map((crumb, i) => html`${i > 0 ? icon(ChevronRight, 'chevron-right', { size: 14, strokeWidth: 1.5, class: 'shrink-0 text-fg-faint rtl:-scale-x-100', attrs: { 'aria-hidden': 'true' } }) : ''}${i < last
? html`<a href=${crumb.href ?? '#'} class="shrink-0 rounded-ds-sm text-fg-muted transition-[color] duration-160 ease-standard hover:text-fg" @click=${(event: MouseEvent) => this.emit('navigate', { crumb, event })}>${crumb.label}</a>`
: html`<span class="min-w-0 truncate text-fg" aria-current="page">${crumb.label}</span>`}`)}</nav>`
}
}
define('breadcrumb', Breadcrumb)Reference: Derived from the guidelines: DM Sans for UI text, White on Black with neutral muted steps. shadcn's breadcrumb for anatomy and aria-current..