Breadcrumbs (#296)

* add breadcrumbs

* rm space

* move breadcrumbs to ui
This commit is contained in:
tmshv authored and GitHub committed 2026-04-25 03:15:25 +03:00
1 parent ad9695127a
commit 98dcaaa237
11 files changed
+92 -12

No files matched your search

@@ -0,0 +1,34 @@
import { render, within } from "@testing-library/react"
import { describe, it, expect } from "vitest"
import { Breadcrumbs } from "."
describe("Breadcrumbs", () => {
const items = [
{ name: "Home", href: "/" },
{ name: "About", href: "/about" },
{ name: "Team", href: "/about/team" },
]
it("renders breadcrumb items with separators", () => {
const { container } = render(<Breadcrumbs items={items} path="/about/team" />)
const nav = within(container.querySelector("nav")!)
expect(nav.getByText("Home")).toBeInTheDocument()
expect(nav.getByText("About")).toBeInTheDocument()
expect(nav.getByText("Team")).toBeInTheDocument()
const separators = nav.getAllByText("/")
expect(separators).toHaveLength(2)
})
it("disables the button matching the current path", () => {
const { container } = render(<Breadcrumbs items={items} path="/about" />)
const nav = within(container.querySelector("nav")!)
const aboutLink = nav.getByText("About").closest("a")!
expect(aboutLink.className).toMatch(/disabled/)
const homeLink = nav.getByText("Home").closest("a")!
expect(homeLink.className).not.toMatch(/disabled/)
})
})
+46
View File
@@ -0,0 +1,46 @@
import s from "./styles.module.css"
import { Button } from "../Button"
import { insertBetween } from "@hudozka/utils"
export type Crumb = {
name: string
href: string
}
export type BreadcrumbsProps = {
style?: React.CSSProperties
items: Crumb[]
path: string
}
export const Breadcrumbs: React.FC<BreadcrumbsProps> = props => {
const items = insertBetween(props.items, () => "/")
return (
<nav className={s.breadcrumbs} style={props.style}>
{items.map((x, i) => {
if (typeof x === "string") {
return (
<li key={i}>
<span style={{ margin: "0 var(--size-xs)" }}>{x}</span>
</li>
)
}
return (
<li key={x.href}>
<Button
href={x.href}
theme={"ghost"}
size={"small"}
disabled={x.href === props.path}
>
{x.name}
</Button>
</li>
)
})}
</nav>
)
}
@@ -0,0 +1,17 @@
.breadcrumbs {
display: flex;
align-items: center;
flex-wrap: wrap;
list-style: none;
position: relative;
right: var(--size-s);
}
.breadcrumbs li {
margin-bottom: var(--size-xs);
}
.breadcrumbs a {
text-decoration: none;
}
+1
View File
@@ -1,4 +1,5 @@
export * from "./Box"
export * from "./Breadcrumbs"
export * from "./Button"
export * from "./Panel"
export * from "./Title"