-
Notifications
You must be signed in to change notification settings - Fork 2
feat(breadcrumb): add molecule component #89
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
2 commits
Select commit
Hold shift + click to select a range
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
119 changes: 119 additions & 0 deletions
119
src/components/molecules/breadcrumb/Breadcrumb.stories.tsx
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,119 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react'; | ||
| import { DynamicIcon } from 'lucide-react/dynamic.js'; | ||
| import Breadcrumb from './Breadcrumb'; | ||
| import type { BreadcrumbItem } from './types'; | ||
|
|
||
| /** | ||
| * ## Description | ||
| * Breadcrumb displays the current navigation path and keeps the current page non-interactive. | ||
| * Link hover stays on each item, while collapsed items use an accessible trigger. | ||
| */ | ||
| const meta: Meta<typeof Breadcrumb> = { | ||
| title: 'Molecules/Breadcrumb', | ||
| component: Breadcrumb, | ||
| parameters: { | ||
| docs: { | ||
| autodocs: true | ||
| } | ||
| }, | ||
| tags: ['autodocs'] | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof Breadcrumb>; | ||
|
|
||
| const items: BreadcrumbItem[] = [ | ||
| { title: 'Home', href: '/' }, | ||
| { title: 'Components', href: '/components' }, | ||
| { title: 'Breadcrumb', href: '/components/breadcrumb' } | ||
| ]; | ||
|
|
||
| const itemsWithContent: BreadcrumbItem[] = [ | ||
| { title: 'Home', href: '/', startContent: 'house' }, | ||
| { title: 'Library', href: '/library', endContent: 'chevron-right' }, | ||
| { | ||
| title: 'Documentation', | ||
| href: '/docs', | ||
| startContent: <DynamicIcon name='folder-open' size={14} aria-hidden='true' />, | ||
| endContent: <DynamicIcon name='external-link' size={14} aria-hidden='true' /> | ||
| }, | ||
| { title: 'Breadcrumb' } | ||
| ]; | ||
|
|
||
| const collapsedItems: BreadcrumbItem[] = [ | ||
| { title: 'Home', href: '/' }, | ||
| { title: 'Library', href: '/library' }, | ||
| { title: 'Components', href: '/components' }, | ||
| { title: 'Navigation', href: '/components/navigation' }, | ||
| { title: 'Breadcrumb' } | ||
| ]; | ||
|
|
||
| /** | ||
| * Default breadcrumb with the transparent, borderless container style. | ||
| */ | ||
| export const Default: Story = { | ||
| args: { | ||
| items | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Size variants for the breadcrumb container and current item. | ||
| */ | ||
| export const Sizes: Story = { | ||
| render: () => ( | ||
| <div className='flex w-fit flex-col gap-3'> | ||
| <Breadcrumb items={items} size='sm' /> | ||
| <Breadcrumb items={items} size='md' /> | ||
| <Breadcrumb items={items} size='lg' /> | ||
| </div> | ||
| ) | ||
| }; | ||
|
|
||
| /** | ||
| * Available visual variants for the breadcrumb container. | ||
| */ | ||
| export const Variants: Story = { | ||
| render: () => ( | ||
| <div className='flex w-fit flex-col gap-3'> | ||
| <Breadcrumb items={items} variant='regular' /> | ||
| <Breadcrumb items={items} variant='bordered' /> | ||
| <Breadcrumb items={items} variant='underlined' /> | ||
| <Breadcrumb items={items} variant='line' /> | ||
| </div> | ||
| ) | ||
| }; | ||
|
|
||
| /** | ||
| * Start and end content stay inside the same clickable breadcrumb item. | ||
| */ | ||
| export const WithItemContent: Story = { | ||
| render: () => ( | ||
| <Breadcrumb | ||
| items={itemsWithContent} | ||
| separator={<DynamicIcon name='chevron-right' size={14} aria-hidden='true' />} | ||
| /> | ||
| ) | ||
| }; | ||
|
|
||
| /** | ||
| * The last item stays non-interactive even when link styles are customized. | ||
| */ | ||
| export const CurrentItem: Story = { | ||
| render: () => <Breadcrumb items={items} /> | ||
| }; | ||
|
|
||
| /** | ||
| * Collapsed items use an accessible trigger with tooltip support. | ||
| */ | ||
| export const Collapsed: Story = { | ||
| args: { | ||
| items: collapsedItems, | ||
| maxItems: 3, | ||
| itemsBeforeCollapse: 1, | ||
| itemsAfterCollapse: 1, | ||
| separator: <DynamicIcon name='chevron-right' size={14} aria-hidden='true' />, | ||
| showTooltip: true | ||
| } | ||
| }; |
168 changes: 168 additions & 0 deletions
168
src/components/molecules/breadcrumb/Breadcrumb.test.tsx
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,168 @@ | ||
| import { render, renderHook, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('lucide-react/dynamic.js', () => ({ | ||
| // biome-ignore lint/style/useNamingConvention: must match library export name | ||
| DynamicIcon: ({ name, size }: { name: string; size: number }) => ( | ||
| <svg data-name={name} data-size={String(size)} data-testid='breadcrumb-icon' /> | ||
| ) | ||
| })); | ||
|
|
||
| import Breadcrumb from './Breadcrumb'; | ||
| import { Breadcrumb as BreadcrumbFromIndex } from './index'; | ||
| import type { BreadcrumbItem } from './types'; | ||
| import { breadcrumbBase } from './types'; | ||
| import { useBreadcrumb } from './useBreadcrumb'; | ||
|
|
||
| const items: BreadcrumbItem[] = [ | ||
| { title: 'Home', href: '#home' }, | ||
| { title: 'Components', href: '#components' }, | ||
| { title: 'Navigation', href: '#components-navigation' }, | ||
| { title: 'Breadcrumb' } | ||
| ]; | ||
|
|
||
| describe('useBreadcrumb — logic', () => { | ||
| it('does not collapse when the collapsed trigger would exceed maxItems', () => { | ||
| const { result } = renderHook(() => | ||
| useBreadcrumb({ | ||
| items, | ||
| maxItems: 3, | ||
| itemsBeforeCollapse: 2, | ||
| itemsAfterCollapse: 1 | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current.processedItems).toHaveLength(items.length); | ||
| expect(result.current.processedItems.every((entry) => entry.type === 'item')).toBe(true); | ||
| }); | ||
|
|
||
| it('tracks original indexes across collapsed entries', () => { | ||
| const { result } = renderHook(() => | ||
| useBreadcrumb({ | ||
| items, | ||
| maxItems: 3, | ||
| itemsBeforeCollapse: 1, | ||
| itemsAfterCollapse: 1 | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current.processedItems).toMatchObject([ | ||
| { type: 'item', originalIndex: 0 }, | ||
| { type: 'collapsed', hiddenItemIndexes: [1, 2] }, | ||
| { type: 'item', originalIndex: 3, isLast: true } | ||
| ]); | ||
| }); | ||
|
|
||
| it('does not duplicate trailing items when itemsAfterCollapse is zero', () => { | ||
| const { result } = renderHook(() => | ||
| useBreadcrumb({ | ||
| items, | ||
| maxItems: 3, | ||
| itemsBeforeCollapse: 1, | ||
| itemsAfterCollapse: 0 | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current.processedItems).toMatchObject([ | ||
| { type: 'item', originalIndex: 0 }, | ||
| { type: 'collapsed', hiddenItemIndexes: [1, 2, 3], isLast: true } | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Breadcrumb — component behavior', () => { | ||
| it('is exported from the molecule barrel', () => { | ||
| expect(BreadcrumbFromIndex).toBe(Breadcrumb); | ||
| }); | ||
|
|
||
| it('marks the last item as the current page and keeps it non-interactive', () => { | ||
| render(<Breadcrumb items={items} />); | ||
|
|
||
| expect(screen.getByText('Breadcrumb').closest('[aria-current="page"]')).toBeInTheDocument(); | ||
| expect(screen.queryByRole('link', { name: 'Breadcrumb' })).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('calls onItemClick with the original index for visible links', async () => { | ||
| const user = userEvent.setup(); | ||
| const handleItemClick = vi.fn(); | ||
|
|
||
| render(<Breadcrumb items={items} onItemClick={handleItemClick} />); | ||
|
|
||
| await user.click(screen.getByRole('link', { name: 'Components' })); | ||
|
|
||
| expect(handleItemClick).toHaveBeenCalledWith(items[1], 1); | ||
| }); | ||
|
|
||
| it('calls onItemClick with the original index for hidden collapsed links', async () => { | ||
| const user = userEvent.setup(); | ||
| const handleItemClick = vi.fn(); | ||
| const duplicatedItems: BreadcrumbItem[] = [ | ||
| { title: 'Home', href: '#home' }, | ||
| { title: 'Docs', href: '#docs' }, | ||
| { title: 'Docs', href: '#docs-api' }, | ||
| { title: 'API', href: '#api' }, | ||
| { title: 'Current' } | ||
| ]; | ||
|
|
||
| render( | ||
| <Breadcrumb | ||
| items={duplicatedItems} | ||
| maxItems={3} | ||
| itemsBeforeCollapse={1} | ||
| itemsAfterCollapse={1} | ||
| onItemClick={handleItemClick} | ||
| /> | ||
| ); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'Show 3 hidden breadcrumb items' })); | ||
|
|
||
| const hiddenLink = screen.getByRole('link', { name: 'API' }); | ||
| expect(hiddenLink).toHaveClass('min-w-0'); | ||
| await user.click(hiddenLink); | ||
|
|
||
| expect(handleItemClick).toHaveBeenCalledWith(duplicatedItems[3], 3); | ||
| }); | ||
|
|
||
| it('renders string separators as text instead of icon names', () => { | ||
| render(<Breadcrumb items={items} separator='→' />); | ||
|
|
||
| expect(screen.getAllByText('→')).toHaveLength(3); | ||
| expect(screen.queryByTestId('breadcrumb-icon')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('wraps a custom collapsed element with accessible trigger behavior', async () => { | ||
| const user = userEvent.setup(); | ||
| const handleCollapsedClick = vi.fn(); | ||
|
|
||
| render( | ||
| <Breadcrumb | ||
| items={items} | ||
| maxItems={3} | ||
| itemsBeforeCollapse={1} | ||
| itemsAfterCollapse={1} | ||
| collapsedElement={<span>More</span>} | ||
| onCollapsedClick={handleCollapsedClick} | ||
| /> | ||
| ); | ||
|
|
||
| const trigger = screen.getByRole('button', { name: 'Show 2 hidden breadcrumb items' }); | ||
| await user.click(trigger); | ||
|
|
||
| expect(handleCollapsedClick).toHaveBeenCalledWith([items[1], items[2]]); | ||
| expect(document.getElementById(trigger.getAttribute('aria-controls') ?? '')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('allows breadcrumb labels to shrink before truncating', () => { | ||
| render(<Breadcrumb items={items} />); | ||
|
|
||
| expect(screen.getByRole('link', { name: 'Components' })).toHaveClass('min-w-0', 'max-w-full'); | ||
| expect(screen.getByText('Breadcrumb').closest('li')).toHaveClass('min-w-0'); | ||
| }); | ||
|
|
||
| it('uses the established design-system classes for sizing and important rounded variants', () => { | ||
| expect(breadcrumbBase({ size: 'xs' })).toContain('fs-xs'); | ||
| expect(breadcrumbBase({ size: 'md' })).toContain('fs-base'); | ||
| expect(breadcrumbBase({ variant: 'underlined' })).toContain('rounded-none!'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.