ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ๋ค๋ฅธ ํ์์ ์ฌ์ฉํ๋ โํ๊ทธ ๋ฑ์ง(TagBadge)โ ์คํ์ผ์ด ์กฐ๊ธ์ฉ ๋ฌ๋ผ์ง๋ ๋ฌธ์ ๊ฐ ์๊ฒผ๋ค.
์๋ฅผ ๋ค์ด, best ํ๊ทธ์ author(๊ธ์ด์ด) ํ๊ทธ๊ฐ ๊ฐ๊ฐ ๋ณ๋๋ก ๋ง๋ค์ด์ ธ ์์๊ณ , ์คํ์ผ์ด ๋น์ทํจ์๋ ๋ถ๊ตฌํ๊ณ ์๋ก ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ก ๊ด๋ฆฌ๋๊ณ ์์๋ค.
์ด๋ฐ ๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ์ ๋ฌธ์ ๋ฅผ ๋ณ๋๋ค.
๊ทธ๋์ ์ด ์ปดํฌ๋ํธ๋ฅผ ํ๋๋ก ํตํฉํ๊ณ , variant prop์ผ๋ก ์ํ๋ฅผ ๊ตฌ๋ถํ๋๋ก ๋ฆฌํฉํฐ๋งํ๋ค.
๋ฆฌํฉํฐ๋ง์ ํต์ฌ์ "ํ๋์ ์ปดํฌ๋ํธ๋ก ๋ค์ํ ์ํ๋ฅผ ํํ" ํ๋ ๊ฒ์ด์๋ค.
๊ตฌ์ฒด์ ์ธ ๋ชฉํ๋ ๋ค์๊ณผ ๊ฐ๋ค.
variant๋ฅผ ํตํด ํ๊ทธ ํ์
(default, author) ๊ตฌ๋ถsize) ์ ์งselected)๋ ์๊ฐ์ ์ธ ๊ฐ์กฐ ํ์ ์ฉ๋๋ก ์ฌ์ฉ๊ธฐ์กด TagBadge๋ ๋จ์ผ ์์ ์ฒด๊ณ๋ง ๊ฐ์ง๊ณ ์์๋ค.
selected ? colorMap.main : colorMap.neutral
์ฆ, ์ ํ ์ํ๋ง ๊ตฌ๋ถ ๊ฐ๋ฅํ์ ๋ฟ,
โ๊ธ์ด์ด(author)โ๋ โ๋ฒ ์คํธ(best)โ์ฒ๋ผ ๋ณ๋์ ์๊ฐ์ ์คํ์ผ์ ์ ์ฉํ ๋ฐฉ๋ฒ์ด ์์๋ค.
variant prop์ ์ถ๊ฐํ๊ณ , ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ ๋งคํ์ ๋ฌ๋ฆฌํ๋ ๊ตฌ์กฐ๋ก ๋ณ๊ฒฝํ๋ค.
์ด๋ ๊ฒ ํ๋ฉด ์ํ๋ณ๋ก ์์ ํ ๋ค๋ฅธ ์คํ์ผ์ ๊ฐ์ง ์ ์์ผ๋ฉด์๋,
๊ณตํต๋ ์ปดํฌ๋ํธ ์ธํฐํ์ด์ค๋ฅผ ์ ์งํ ์ ์๋ค.
import { memo, type ComponentPropsWithoutRef, type ReactNode } from 'react'
type TagBadgeProps = ComponentPropsWithoutRef<'button'> & {
children: ReactNode
selected?: boolean
size?: 'sm' | 'md'
variant?: 'default' | 'author'
}
// size / color ๋งคํ
const sizeMap = {
sm: 'h-6 px-2.5 text-[10px]',
md: 'py-1 px-3 text-xs',
} as const
const colorMap = {
neutral: 'bg-muted/50 text-muted-foreground hover:bg-muted',
main: 'bg-primary text-primary-foreground',
author: 'bg-secondary/20 text-secondary',
} as const
function TagBadgeBase({
children,
selected = false,
size = 'md',
variant = 'default',
className,
...rest
}: TagBadgeProps) {
const tone =
variant === 'author'
? colorMap.author
: selected
? colorMap.main
: colorMap.neutral
return (
<buttontype="button"
data-selected={selected ? '' : undefined}
data-variant={variant}
className={[
'inline-flex items-center justify-center gap-1.5',
'rounded-[14px] select-none whitespace-nowrap',
'cursor-pointer',
tone,
'transition-colors duration-200',
sizeMap[size],
className ?? '',
].join(' ')}
{...rest}
>
{children}
</button>
)
}
export default memo(TagBadgeBase)
<TagBadgeBase>์ผ๋ฐ ํ๊ทธ</TagBadgeBase>
<TagBadgeBase selected>์ ํ๋ ํ๊ทธ</TagBadgeBase>
<TagBadgeBase variant="author">๊ธ์ด์ด</TagBadgeBase>
<TagBadgeBase variant="author" size="sm">
์์ฑ์
</TagBadgeBase>
| Prop ์กฐํฉ | ์ค๋ช |
|---|---|
default | ์ผ๋ฐ์ ์ธ ์ค๋ฆฝ ํ๊ทธ |
selected | ๊ฐ์กฐ(ํ์ฑ) ์ํ ํํ |
author | ๊ธ์ด์ด ์ ์ฉ ์คํ์ผ |
๋ฆฌํฉํฐ๋ง ์ดํ ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ์ด ์ด๋ฃจ์ด์ก๋ค:
์ด๋ฒ ๋ฆฌํฉํฐ๋ง์ ํต์ฌ์ โ์กฐ๊ฑด ๋ถ๊ธฐ๋ฅผ JSX๊ฐ ์๋ ์คํ์ผ ๋ ๋ฒจ์์ ํด๊ฒฐํ๊ธฐโ์๋ค.
variant๋ฅผ ์ค์ฌ์ผ๋ก ์คํ์ผ์ ๊ด๋ฆฌํ๋ฉด, ์๋ก์ด ํ๊ทธ ํ์
์ด ์ถ๊ฐ๋๋๋ผ๋ ๊ตฌ์กฐ๋ฅผ ๋ณ๊ฒฝํ ํ์ ์์ด ํ์ฅ๋ง ํ๋ฉด ๋๋ค.