-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add Breadcrumb component #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5f5f90f
feat: add Breadcrumb component
igorsalbr d8c01c9
fix: use composite key for breadcrumb items to avoid duplicate key is…
igorsalbr a44359c
fix: resolve pre-existing lint errors in icon files
igorsalbr 49f5c61
feat: add Breadcrumb Storybook stories
igorsalbr 788fb47
fix: format Breadcrumb stories to pass biome check
igorsalbr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import type { Meta, StoryObj } from "@storybook/react-vite"; | ||
| import { Breadcrumb } from "./Breadcrumb"; | ||
|
|
||
| const meta: Meta<typeof Breadcrumb> = { | ||
| title: "Components/Breadcrumb", | ||
| component: Breadcrumb, | ||
| parameters: { | ||
| jest: "Breadcrumb.test.tsx", | ||
| layout: "centered", | ||
| }, | ||
| tags: ["autodocs"], | ||
| }; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof Breadcrumb>; | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| items: [ | ||
| { label: "Home", href: "/" }, | ||
| { label: "Products", href: "/products" }, | ||
| { label: "Product Detail" }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| export const TwoItems: Story = { | ||
| args: { | ||
| items: [{ label: "Home", href: "/" }, { label: "Current Page" }], | ||
| }, | ||
| }; | ||
|
|
||
| export const WithOnClick: Story = { | ||
| args: { | ||
| items: [ | ||
| { label: "Home", onClick: () => alert("Home clicked") }, | ||
| { label: "Settings", onClick: () => alert("Settings clicked") }, | ||
| { label: "Profile" }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| export const SingleItem: Story = { | ||
| args: { | ||
| items: [{ label: "Home" }], | ||
| }, | ||
| }; | ||
|
|
||
| export const ManyItems: Story = { | ||
| args: { | ||
| items: [ | ||
| { label: "Home", href: "/" }, | ||
| { label: "Category", href: "/category" }, | ||
| { label: "Subcategory", href: "/category/sub" }, | ||
| { label: "Product", href: "/category/sub/product" }, | ||
| { label: "Details" }, | ||
| ], | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { render, screen } from "@/tests/app-test-utils"; | ||
| import { Breadcrumb } from "./Breadcrumb"; | ||
|
|
||
| describe("Breadcrumb", () => { | ||
| it("renders a single item as current page", () => { | ||
| render(<Breadcrumb items={[{ label: "Home" }]} />); | ||
| const text = screen.getByText("Home"); | ||
| expect(text).toHaveAttribute("aria-current", "page"); | ||
| expect(text).toHaveClass("text-gray-700"); | ||
| }); | ||
|
|
||
| it("renders multiple items with separators between them", () => { | ||
| render( | ||
| <Breadcrumb | ||
| items={[ | ||
| { label: "Home", href: "/" }, | ||
| { label: "Configs", href: "/configs" }, | ||
| { label: "Meta" }, | ||
| ]} | ||
| /> | ||
| ); | ||
| expect(screen.getByText("Home")).toBeVisible(); | ||
| expect(screen.getByText("Configs")).toBeVisible(); | ||
| expect(screen.getByText("Meta")).toBeVisible(); | ||
|
|
||
| const separators = document.querySelectorAll('[aria-hidden="true"]'); | ||
| expect(separators).toHaveLength(2); | ||
| }); | ||
|
|
||
| it("renders links for non-last items with href", () => { | ||
| render( | ||
| <Breadcrumb | ||
| items={[{ label: "Home", href: "/" }, { label: "Current" }]} | ||
| /> | ||
| ); | ||
| const link = screen.getByText("Home").closest("a"); | ||
| expect(link).toHaveAttribute("href", "/"); | ||
| }); | ||
|
|
||
| it("renders buttons for non-last items with onClick", () => { | ||
| const onClick = jest.fn(); | ||
| render( | ||
| <Breadcrumb items={[{ label: "Back", onClick }, { label: "Current" }]} /> | ||
| ); | ||
| const button = screen.getByRole("button", { name: "Back" }); | ||
| button.click(); | ||
| expect(onClick).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("does not render link or button for the last item even with href", () => { | ||
| render(<Breadcrumb items={[{ label: "Only", href: "/only" }]} />); | ||
| expect(screen.queryByRole("link")).not.toBeInTheDocument(); | ||
| expect(screen.queryByRole("button")).not.toBeInTheDocument(); | ||
| expect(screen.getByText("Only")).toHaveAttribute("aria-current", "page"); | ||
| }); | ||
|
|
||
| it("does not render button for the last item even with onClick", () => { | ||
| render(<Breadcrumb items={[{ label: "Only", onClick: jest.fn() }]} />); | ||
| expect(screen.queryByRole("button")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("uses renderLink for custom link rendering", () => { | ||
| const renderLink = (href: string, children: React.ReactNode) => ( | ||
| <a data-testid="custom-link" href={href}> | ||
| {children} | ||
| </a> | ||
| ); | ||
| render( | ||
| <Breadcrumb | ||
| items={[{ label: "Home", href: "/" }, { label: "Current" }]} | ||
| renderLink={renderLink} | ||
| /> | ||
| ); | ||
| const customLink = screen.getByTestId("custom-link"); | ||
| expect(customLink).toHaveAttribute("href", "/"); | ||
| }); | ||
|
|
||
| it("has proper nav element with aria-label", () => { | ||
| render(<Breadcrumb items={[{ label: "Home" }]} />); | ||
| expect( | ||
| screen.getByRole("navigation", { name: "Breadcrumb" }) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("uses an ordered list for semantic structure", () => { | ||
| render(<Breadcrumb items={[{ label: "A", href: "/" }, { label: "B" }]} />); | ||
| const list = document.querySelector('[data-slot="breadcrumb-list"]'); | ||
| expect(list?.tagName).toBe("OL"); | ||
| const listItems = list?.querySelectorAll("li"); | ||
| expect(listItems).toHaveLength(2); | ||
| }); | ||
|
|
||
| it("applies custom className", () => { | ||
| const { container } = render( | ||
| <Breadcrumb className="my-breadcrumb" items={[{ label: "Home" }]} /> | ||
| ); | ||
| const nav = container.querySelector('[data-slot="breadcrumb"]'); | ||
| expect(nav).toHaveClass("my-breadcrumb"); | ||
| }); | ||
|
|
||
| it("passes extra nav props through", () => { | ||
| const { container } = render( | ||
| <Breadcrumb data-testid="custom-breadcrumb" items={[{ label: "Home" }]} /> | ||
| ); | ||
| expect( | ||
| container.querySelector('[data-testid="custom-breadcrumb"]') | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders correctly with many items", () => { | ||
| const items = [ | ||
| { label: "Home", href: "/" }, | ||
| { label: "Section", href: "/section" }, | ||
| { label: "Subsection", href: "/section/sub" }, | ||
| { label: "Page", href: "/section/sub/page" }, | ||
| { label: "Current" }, | ||
| ]; | ||
| render(<Breadcrumb items={items} />); | ||
|
|
||
| for (const item of items) { | ||
| expect(screen.getByText(item.label)).toBeVisible(); | ||
| } | ||
| const separators = document.querySelectorAll('[aria-hidden="true"]'); | ||
| expect(separators).toHaveLength(4); | ||
|
|
||
| expect(screen.getByText("Current")).toHaveAttribute("aria-current", "page"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| "use client"; | ||
|
|
||
| import type { ComponentProps, ReactNode } from "react"; | ||
| import { ChevronRightIcon } from "@/components/icons"; | ||
| import { cn } from "@/lib/utils"; | ||
|
|
||
| export type BreadcrumbItem = { | ||
| label: string; | ||
| href?: string; | ||
| onClick?: () => void; | ||
| }; | ||
|
|
||
| export type BreadcrumbProps = { | ||
| items: BreadcrumbItem[]; | ||
| /** | ||
| * Custom link renderer for framework-specific routing (e.g. Next.js Link). | ||
| * Receives the href and children; should return a link element. | ||
| * Falls back to a plain `<a>` tag when not provided. | ||
| */ | ||
| renderLink?: (href: string, children: ReactNode) => ReactNode; | ||
| className?: string; | ||
| } & Omit<ComponentProps<"nav">, "children">; | ||
|
|
||
| function Breadcrumb({ | ||
| items, | ||
| renderLink, | ||
| className, | ||
| ...props | ||
| }: BreadcrumbProps) { | ||
| return ( | ||
| <nav | ||
| aria-label="Breadcrumb" | ||
| className={cn("flex items-center gap-1 text-sm", className)} | ||
| data-slot="breadcrumb" | ||
| {...props} | ||
| > | ||
| <ol className="flex items-center gap-1" data-slot="breadcrumb-list"> | ||
| {items.map((item, index) => { | ||
| const isLast = index === items.length - 1; | ||
|
|
||
| const renderItem = () => { | ||
| if (!isLast && item.onClick) { | ||
| return ( | ||
| <button | ||
| className="cursor-pointer text-gray-500 hover:text-gray-700" | ||
| data-slot="breadcrumb-button" | ||
| onClick={item.onClick} | ||
| type="button" | ||
| > | ||
| {item.label} | ||
| </button> | ||
| ); | ||
| } | ||
|
|
||
| if (!isLast && item.href) { | ||
| const linkContent = ( | ||
| <span className="text-gray-500 hover:text-gray-700"> | ||
| {item.label} | ||
| </span> | ||
| ); | ||
|
|
||
| if (renderLink) { | ||
| return renderLink(item.href, linkContent); | ||
| } | ||
|
|
||
| return ( | ||
| <a | ||
| className="text-gray-500 hover:text-gray-700" | ||
| data-slot="breadcrumb-link" | ||
| href={item.href} | ||
| > | ||
| {item.label} | ||
| </a> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <span | ||
| aria-current={isLast ? "page" : undefined} | ||
| className={isLast ? "text-gray-700" : "text-gray-500"} | ||
| data-slot="breadcrumb-text" | ||
| > | ||
| {item.label} | ||
| </span> | ||
| ); | ||
| }; | ||
|
|
||
| return ( | ||
| <li | ||
| className="flex items-center gap-1" | ||
| data-slot="breadcrumb-item" | ||
| key={`${index}-${item.label}`} | ||
| > | ||
| {index > 0 && ( | ||
| <ChevronRightIcon | ||
| aria-hidden="true" | ||
| className="text-gray-400" | ||
| size="sm" | ||
| /> | ||
| )} | ||
| {renderItem()} | ||
| </li> | ||
| ); | ||
| })} | ||
| </ol> | ||
| </nav> | ||
| ); | ||
| } | ||
|
|
||
| Breadcrumb.displayName = "Breadcrumb"; | ||
|
|
||
| export { Breadcrumb }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export type { BreadcrumbItem, BreadcrumbProps } from "./Breadcrumb"; | ||
| export { Breadcrumb } from "./Breadcrumb"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.