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

[#364] Badge 컴포넌트 #396

Merged
merged 3 commits into from
Sep 15, 2023
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
4 changes: 2 additions & 2 deletions public/icons/heart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions src/stories/Base/Badge.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Badge from '@/ui/Base/Badge';
import { IconHeart } from '@public/icons';
import { Meta, StoryObj } from '@storybook/react';

const meta: Meta<typeof Badge> = {
title: 'Base/Badge',
component: Badge,
tags: ['autodocs'],
};

export default meta;

type Story = StoryObj<typeof Badge>;

export const Default: Story = {
args: {
size: 'small',
colorScheme: 'main-light',
fontWeight: 'bold',
},
render: args => <Badge {...args}>프론트엔드 개발자</Badge>,
};

export const BookshelfLike: Story = {
args: {
size: 'small',
colorScheme: 'red',
fontWeight: 'bold',
},
render: args => (
<Badge {...args}>
<div className="h-[1.3rem] w-[1.3rem] fill-white">
<IconHeart />
</div>
99
</Badge>
),
};

export const GroupProgress: Story = {
args: {
size: 'large',
colorScheme: 'main',
fontWeight: 'bold',
},
render: args => <Badge {...args}>진행중</Badge>,
};

export const GroupDisclosure: Story = {
args: {
size: 'medium',
colorScheme: 'grey',
fontWeight: 'normal',
},
render: args => <Badge {...args}>공개</Badge>,
};
93 changes: 93 additions & 0 deletions src/ui/Base/Badge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { PropsWithChildren, useMemo } from 'react';

type Size = 'small' | 'medium' | 'large';
type ColorScheme = 'main' | 'main-light' | 'grey' | 'red';
type FontWeight = 'thin' | 'normal' | 'bold';

type BadgeProps = PropsWithChildren<{
size?: Size;
colorScheme?: ColorScheme;
fontWeight?: FontWeight;
isFilled?: boolean;
}>;

const getSizeClasses = (size: Size) => {
switch (size) {
case 'small': {
return 'h-[1.8rem] text-2xs';
}
case 'medium': {
return 'h-[1.9rem] text-xs';
}
case 'large': {
return 'h-[2.1rem] text-xs';
}
}
};

const getSchemeClasses = (colorScheme: ColorScheme, isFilled: boolean) => {
switch (colorScheme) {
case 'main': {
return isFilled
? 'border-main-900 bg-main-900 text-white'
: 'border-main-900 text-main-900';
}
case 'main-light': {
return isFilled
? 'border-main-600 bg-main-600 text-white'
: 'border-main-600 text-main-600';
}
case 'grey': {
return isFilled
? 'border-black-100 bg-black-100 text-black-500'
: 'border-black-500 text-black-500';
}
case 'red': {
return isFilled
? 'border-warning-800 bg-warning-800 text-white'
: 'border-warning-800 text-warning-800';
}
}
};

const getFontWeightClasses = (fontWeight: FontWeight) => {
switch (fontWeight) {
case 'thin': {
return 'font-thin';
}
Comment on lines +55 to +57
Copy link
Member

@minjongbaek minjongbaek Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

피그마 기준으로 thin은 사용하지 않는 것 같은데, 제가 놓친 부분이 있을까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment; @minjongbaek

이후 확장성을 고려해서 작성하긴했는데 지금 생각해보니 굳이 필요한가 싶긴하네요 ㅋㅋㅋ

case 'normal': {
return 'font-normal';
}
case 'bold': {
return 'font-bold';
}
}
};

const Badge = ({
size = 'medium',
colorScheme = 'main',
fontWeight = 'normal',
isFilled = true,
children,
...props
}: BadgeProps) => {
const computedClasses = useMemo(() => {
const sizeClass = getSizeClasses(size);
const schemeClass = getSchemeClasses(colorScheme, isFilled);
const fontWeightClass = getFontWeightClasses(fontWeight);

return [sizeClass, schemeClass, fontWeightClass].join(' ');
}, [size, colorScheme, isFilled, fontWeight]);

return (
<div
className={`m-0 flex w-fit items-center justify-center gap-[0.4rem] rounded-[0.5rem] border-[0.05rem] px-[0.8rem] py-[0.25rem] ${computedClasses}`}
{...props}
>
{children}
</div>
);
};

export default Badge;