Restructure the repo into npm workspaces so the component kit publishes as its own package, like MUI and shadcn/ui: - packages/ui: @openkhalix/tansy-ui (published), with components, stories, tests, tokens and the library build - packages/brand: private logo and catalogue data shared by both apps - apps/site: the landing page - apps/docs: Storybook The package: - MIT licence (copyright OpenKhalix) - per-component ES modules and type declarations - "use client" on component modules so they import into Next.js Server Components; helpers stay server-safe - tokens.css for Tailwind v4 apps, and a prebuilt styles.css for apps without Tailwind - lucide-react 1.x, whose types compile against current React 19 types The apps resolve the package through a "source" export condition, so they hot-reload component edits, while outside users get dist/. scripts/test-package.mjs packs the package and installs the tarball into Vite + Tailwind (strict types), plain React and Next.js apps; each must build and render correctly. Gitea workflows run CI on push and publish to npm when a vX.Y.Z tag matching the package version is pushed.
114 lines
4.0 KiB
TypeScript
114 lines
4.0 KiB
TypeScript
import { useEffect, useRef, useState, type ComponentProps, type ReactNode } from 'react'
|
|
import { ChevronRight } from 'lucide-react'
|
|
import { cn } from '../lib/cn'
|
|
import { focusRing } from '../lib/styles'
|
|
|
|
export type BreadcrumbItem = {
|
|
label: ReactNode
|
|
/** Link target. The last item is the current page and never a link. */
|
|
href?: string
|
|
/** Icon before the label. */
|
|
icon?: ReactNode
|
|
}
|
|
|
|
export type BreadcrumbProps = Omit<ComponentProps<'nav'>, 'children'> & {
|
|
items: BreadcrumbItem[]
|
|
/** Replaces the chevron between items. */
|
|
separator?: ReactNode
|
|
/** Collapse the middle items into "…" when there are more than this. */
|
|
maxItems?: number
|
|
/** Items kept before the "…" when collapsed. */
|
|
itemsBeforeCollapse?: number
|
|
/** Items kept after the "…" when collapsed. */
|
|
itemsAfterCollapse?: number
|
|
}
|
|
|
|
const linkClass =
|
|
'inline-flex items-center gap-1.5 rounded-sm text-fg-muted underline-offset-4 transition-colors hover:text-fg hover:underline [&_svg]:size-4 [&_svg]:shrink-0'
|
|
|
|
/** Shows where the current page sits in the site hierarchy. */
|
|
export function Breadcrumb({
|
|
items,
|
|
separator,
|
|
maxItems,
|
|
itemsBeforeCollapse = 1,
|
|
itemsAfterCollapse = 2,
|
|
className,
|
|
'aria-label': ariaLabel = 'Breadcrumb',
|
|
...props
|
|
}: BreadcrumbProps) {
|
|
const [expanded, setExpanded] = useState(false)
|
|
const listRef = useRef<HTMLOListElement>(null)
|
|
|
|
// After expanding, move focus to the first revealed item so it isn't lost with the "…" button.
|
|
useEffect(() => {
|
|
if (!expanded) return
|
|
const revealed = listRef.current?.children[itemsBeforeCollapse]
|
|
revealed?.querySelector<HTMLElement>('a, [aria-current]')?.focus()
|
|
}, [expanded, itemsBeforeCollapse])
|
|
const collapse =
|
|
!expanded && maxItems != null && items.length > maxItems && itemsBeforeCollapse + itemsAfterCollapse < items.length
|
|
|
|
type Entry = { item: BreadcrumbItem; index: number } | 'ellipsis'
|
|
const entries: Entry[] = items.map((item, index) => ({ item, index }))
|
|
const visible: Entry[] = collapse
|
|
? [...entries.slice(0, itemsBeforeCollapse), 'ellipsis', ...entries.slice(items.length - itemsAfterCollapse)]
|
|
: entries
|
|
|
|
const sep = (
|
|
<span aria-hidden="true" className="flex text-fg-subtle [&_svg]:size-3.5">
|
|
{separator ?? <ChevronRight />}
|
|
</span>
|
|
)
|
|
|
|
return (
|
|
<nav aria-label={ariaLabel} className={className} {...props}>
|
|
<ol ref={listRef} className="flex flex-wrap items-center gap-1.5 text-sm">
|
|
{visible.map((entry, i) => {
|
|
const last = i === visible.length - 1
|
|
if (entry === 'ellipsis') {
|
|
return (
|
|
<li key="ellipsis" className="flex items-center gap-1.5">
|
|
<button
|
|
type="button"
|
|
aria-label="Show all breadcrumbs"
|
|
onClick={() => setExpanded(true)}
|
|
className={cn(
|
|
'inline-flex h-6 min-w-6 items-center justify-center rounded-md px-1 text-fg-muted transition-colors hover:bg-surface-sunken hover:text-fg',
|
|
focusRing,
|
|
)}
|
|
>
|
|
…
|
|
</button>
|
|
{sep}
|
|
</li>
|
|
)
|
|
}
|
|
const { item, index } = entry
|
|
return (
|
|
<li key={index} className="flex min-w-0 items-center gap-1.5">
|
|
{last ? (
|
|
<span aria-current="page" className="inline-flex items-center gap-1.5 font-medium text-fg [&_svg]:size-4 [&_svg]:shrink-0">
|
|
{item.icon}
|
|
{item.label}
|
|
</span>
|
|
) : item.href != null ? (
|
|
<a href={item.href} className={cn(linkClass, focusRing)}>
|
|
{item.icon}
|
|
{item.label}
|
|
</a>
|
|
) : (
|
|
<span className="inline-flex items-center gap-1.5 text-fg-muted [&_svg]:size-4 [&_svg]:shrink-0">
|
|
{item.icon}
|
|
{item.label}
|
|
</span>
|
|
)}
|
|
{!last && sep}
|
|
</li>
|
|
)
|
|
})}
|
|
</ol>
|
|
</nav>
|
|
)
|
|
}
|