Skip to content
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

feat: add breadcrumb component #79

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions packages/core/lib/components/Breadcrumb.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React from 'react';
import { Label } from './Label';

interface BreadcrumbItem {
label: string;
onClick: () => void;
}

interface BreadcrumbProps {
items: BreadcrumbItem[];
maxLength?: number;
}

const ChevronIcon: React.FC = () => (
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
aria-hidden="true"
>
<path
d="M6 4L10 8L6 12"
stroke="currentColor"
strokeWidth="1"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
);

const EllipsisIcon: React.FC = () => (
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
aria-hidden="true"
>
<circle cx="3" cy="8" r="1" fill="currentColor" />
<circle cx="8" cy="8" r="1" fill="currentColor" />
<circle cx="13" cy="8" r="1" fill="currentColor" />
</svg>
);

export const Breadcrumb: React.FC<BreadcrumbProps> = ({
items,
maxLength = 3,
}) => {
const maxItems = Math.max(2, maxLength);
const renderItems = () => {
if (items.length <= maxItems) {
return items;
}

const visibleItems = [];
visibleItems.push(items[0]);

if (maxItems > 2) {
for (let i = 1; i < maxItems - 1; i++) {
visibleItems.push(items[items.length - maxItems + i]);
}
}

visibleItems.push(items[items.length - 1]);

return visibleItems;
};

return (
<nav aria-label="브레드크럼" className="py-2">
<ol className="flex items-center">
{renderItems().map((item, index) => (
<li key={index} className="flex items-center m-0">
{index > 0 && <ChevronIcon />}
{index === 1 && items.length > maxItems && (
<>
<EllipsisIcon />
<ChevronIcon />
</>
)}
<Label
onClick={item.onClick}
color={'gray-90'}
size="xs"
className="hover:underline focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary cursor-pointer"
>
{item.label}
</Label>
</li>
))}
</ol>
</nav>
);
};
12 changes: 11 additions & 1 deletion packages/core/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ import { Spinner } from './components/Spinner';
import { Badge } from './components/Badge';
import { TextInput } from './components/TextInput';
import { TextArea } from './components/TextArea';
import { Breadcrumb } from './components/Breadcrumb';

export { Display, Heading, Title, Body, Detail, Label, Link, colors };
export { Button, LinkButton, Tag, Spinner, Badge, TextInput, TextArea };
export {
Button,
LinkButton,
Tag,
Spinner,
Badge,
TextInput,
TextArea,
Breadcrumb,
};
63 changes: 63 additions & 0 deletions stories/core/Breadcrumb.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { Meta, StoryObj } from '@storybook/react';
import { fn } from '@storybook/test';
import { Breadcrumb } from '../../packages/core/lib';

const meta = {
title: 'Components/Breadcrumb',
component: Breadcrumb,
parameters: {
layout: 'centered',
},
tags: ['autodocs'],
argTypes: {},
} satisfies Meta<typeof Breadcrumb>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {
items: [
{ label: '홈', onClick: () => console.log('홈') },
{ label: '문서', onClick: () => console.log('문서') },
{ label: '서적', onClick: () => console.log('서적') },
{ label: '서적 서적', onClick: () => console.log('서적 서적') },
],
},
};

export const MaxItems: Story = {
args: {
items: [
{ label: '홈', onClick: () => console.log('홈') },
{ label: '문서', onClick: () => console.log('문서') },
{ label: '서적', onClick: () => console.log('서적') },
{ label: '서적 서적', onClick: () => console.log('서적 서적') },
],
maxLength: 2,
},
};

export const WithoutEllipsis: Story = {
args: {
items: [
{ label: '홈', onClick: () => console.log('홈') },
{ label: '문서', onClick: () => console.log('문서') },
{ label: '서적', onClick: () => console.log('서적') },
{ label: '서적 서적', onClick: () => console.log('서적 서적') },
],
maxLength: 5,
},
};

export const maxLengthIsLessThan2: Story = {
args: {
items: [
{ label: '홈', onClick: () => console.log('홈') },
{ label: '문서', onClick: () => console.log('문서') },
{ label: '서적', onClick: () => console.log('서적') },
{ label: '서적 서적', onClick: () => console.log('서적 서적') },
],
maxLength: 1,
},
};