Tool

Tailwind CSS์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์€ ๋„๊ตฌ ๋ชจ์Œ

์กฐ๊ฑด๋ถ€๋กœ className ๋ฌธ์ž์—ด์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋„๊ตฌ

  • props์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๋‹ฌ๋ผ์ง€๋Š” ์Šคํƒ€์ผ์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋„๊ตฌ๋กœ classnames ์™€ clsx ๊ฐ€ ์žˆ๋‹ค.

  • clsx ๋Š” classnames ๋ณด๋‹ค ๋” ๊ฐ€๋ณ๊ณ  ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

Install

yarn add clsx

className๊ณผ props๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ  ์ด์™€ ๊ด€๋ จํ•œ ํƒ€์ž…์„ ์ˆ˜๋™์œผ๋กœ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ์ž‘์—…์„ ๋‹จ์ˆœํ™”ํ•˜๋ฉฐ, ์Šคํ‚ค๋งˆ๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ์˜ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์›€์„ ์ฃผ๋Š” ๋„๊ตฌ

cva๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ clsx๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, cx ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜์—ฌ clsx์™€ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ํ•˜์ง€๋งŒ "cva"๋Š” "classValue" ํƒ€์ž…์„ ์ œ๊ณตํ•˜์ง€ ์•Š์•„์„œ ํ˜„์žฌ ๋‘ ๊ฐœ์˜ ํŒจํ‚ค์ง€๋ฅผ ๋™์‹œ์— ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋‹ค.

Install

yarn add class-variance-authority

Reference

const componentVariant = cva(base, options);
  • base โ†’ ์ปดํฌ๋„ŒํŠธ์— ์ ์šฉ๋˜๋Š” ๊ธฐ๋ณธ์ ์ธ ์Šคํƒ€์ผ ํด๋ž˜์Šค ๋ฌธ์ž์—ด

    • string, string[], ๋˜๋Š” clsx ๊ฐ’์œผ๋กœ ํ‘œํ˜„๋  ์ˆ˜ ์žˆ๋‹ค.

  • options (optional)

    • variants โ†’ ์ปดํฌ๋„ŒํŠธ์— ์ ์šฉ๋˜๋Š” ๋‹ค์–‘ํ•œ ์ƒํƒœ๋‚˜ ์Šคํƒ€์ผ์˜ ๋ณ€ํ˜•์„ ์ •์˜

    • compoundVaraints โ†’ variants์— ์ •์˜๋œ ๊ฐ’๋“ค์˜ ์กฐํ•ฉ(AND ์—ฐ์‚ฐ)์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒˆ๋กœ์šด varaints๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ

    • defaultvariants โ†’ variants์— ์ •์˜ํ•œ ๊ฐ’๋“ค์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •

cva์—์„œ ์ œ๊ณตํ•˜๋Š” VariantProps๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ •์˜ํ•œ variants๋ฅผ ํƒ€์ž…์œผ๋กœ ์ถ”์ถœํ•˜์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

const componentVariants = cva(...)

interface ComponentProps
  extends VariantProps<typeof componentVariants> {
 //..์ถ”๊ฐ€๋กœ ์„ค์ •ํ•  props ์ •์˜
}

cva๋ฅผ ์‚ฌ์šฉํ•ด ์ƒ์„ฑ๋œ props๋“ค์€ undefined, null ํƒ€์ž… ๊ฐ’๋„ ์ถ”๊ฐ€๋˜์–ด์ง€๋Š”๋ฐ, ์ด๋Š” ํ•ด๋‹น varaint๋ฅผ ์™„์ „ํžˆ ๋ฌดํšจํ™” ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์˜๋„์ ์ธ ๊ธฐ๋Šฅ ์ด๋ผ๊ณ  ํ•œ๋‹ค. #Issue ++ ์ง€๊ธˆ์€ ์‹คํ—˜ ๋‹จ๊ณ„์ด์ง€๋งŒ ๋‹ค์Œ ๋ฒ„์ „์—์„œ๋Š” null ๋Œ€์‹  ๋ช…์‹œ์ ์œผ๋กœ unset์„ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๋ผ ํ•œ๋‹ค.. #check

Usage

cva๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด๊ณ  ๋น„๊ตํ•ด๋ณด์ž.

Before
import type { ButtonHTMLAttributes } from 'react';
import classNames from 'classnames';

type ButtonVariant = 'primary' | 'secondary' | 'outlined' | 'ghost';
type ButtonSize = 'sm' | 'md' | 'lg' | 'xl';
type ButtonState = 'default' | 'hover';

const ButtonVariantClasses: Record<ButtonVariant, Record<ButtonState, string>> = {
  primary: {
    default: 'btn-primary',
    hover: 'btn-primary-hover',
  },
  secondary: {
    default: 'btn-secondary',
    hover: 'btn-secondary-hover',
  },
  outlined: {
    default: 'btn-outlined',
    hover: 'btn-outlined-hover',
  },
  ghost: {
    default: 'btn-ghost',
    hover: 'btn-ghost-hover',
  },
};

const ButtonSizeClasses: Record<ButtonSize, string> = {
  sm: 'btn-sm',
  md: 'btn-md',
  lg: 'btn-lg',
  xl: 'btn-xl',
};

const ButtonIconSizeClasses: Record<ButtonSize, string> = {
  sm: 'btn-icon-sm',
  md: 'btn-icon-md',
  lg: 'btn-icon-lg',
  xl: 'btn-icon-xl',
};

export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
  className?: string;
  variant: ButtonVariant;
  size?: ButtonSize;
  fit?: boolean;
  label: string;
  IconOnly?: React.ReactElement;
  disabled?: boolean;
}

export const Button = ({
  className,
  variant,
  size = 'md',
  fit = false,
  IconOnly,
  disabled = false,
  label,
  ...buttonProps
}: ButtonProps) => {
  const ButtonVariantClassName = ButtonVariantClasses[variant];
  const ButtonIconSizeClassName = ButtonIconSizeClasses[size];

  return (
    <button
      {...buttonProps}
      className={classNames('btn-base', [ButtonVariantClassName.default], className, {
        [classNames('w-full', 'justify-center')]: fit,
        [ButtonSizeClasses[size]]: !IconOnly,
        [classNames(ButtonIconSizeClassName, 'justify-center', 'rounded-full')]: IconOnly,
        [ButtonVariantClassName.hover]: !disabled,
        [classNames('opacity-40', 'cursor-not-allowed')]: disabled,
      })}
    >
      <span className={classNames({ 'sr-only': IconOnly })}>{label}</span>
      {IconOnly ? <IconOnly.type {...IconOnly.props} size={size === 'sm' ? 20 : 24} /> : null}
    </button>
  );
};

After (CVA ํ™œ์šฉ)
import { VariantProps, cva, cx } from "class-variance-authority";
import { ButtonHTMLAttributes, forwardRef, ForwardedRef } from "react";

import { cn } from "../../utils";

const buttonVariants = cva(
  "flex items-center rounded px-[8px] font-bold focus:outline-none whitespace-nowrap select-none transition-all ease-in focus:ring-4 focus:ring-blue-100 dark:focus:ring-gray-100 dark:focus:ring-opacity-20",
  {
    variants: {
      variant: {
        primary: "bg-brand text-white hover:bg-shade",
        secondary: "bg-border text-primary hover:bg-tertiary",
        outlined: "bg-white text-brand border border-brand hover:bg-tint",
        ghost: "text-primary, hover: text-secondary",
        icon: "justify-center rounded-full",
      },
      fit: { true: "w-full justify-center" },
      size: {
        sm: "h-8 text-sm",
        md: "h-10 text-sm",
        lg: "h-12 text-md",
        xl: "h-[55px] text-lg",
      },
    },
    compoundVariants: [
      {
        variant: "icon",
        size: "sm",
        class: "h-8 w-8",
      },
      {
        variant: "icon",
        size: "md",
        class: "h-10 w-10",
      },
      {
        variant: "icon",
        size: "lg",
        class: "h-11 w-11",
      },
      {
        variant: "icon",
        size: "xl",
        class: "h-12 w-12",
      },
    ],
    defaultVariants: {
      variant: "primary",
      fit: false,
      size: "md",
    },
  }
);

export interface ButtonProps
  extends ButtonHTMLAttributes<HTMLButtonElement>,
    VariantProps<typeof buttonVariants> {
  label: string;
  IconOnly?: React.ReactElement;
}

export const Button = forwardRef(
  (
    {
      className,
      variant = "primary",
      size,
      fit,
      label,
      IconOnly,
      ...buttonProps
    }: ButtonProps,
    ref: ForwardedRef<HTMLButtonElement>
  ) => {
    const variantValue = IconOnly ? "icon" : variant;

    return (
      <button
        ref={ref}
        className={cn(
          buttonVariants({ variant: variantValue, size, fit, className })
        )}
        {...buttonProps}
      >
        <span className={cx({ "sr-only": IconOnly })}>{label}</span>
        {IconOnly ? (
          <IconOnly.type {...IconOnly.props} size={size === "sm" ? 20 : 24} />
        ) : null}
      </button>
    );
  }
);

props์™€ class๋ฅผ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€๋˜๋Š” ์ฝ”๋“œ์™€ ํƒ€์ž… ์„ ์–ธ์ด ๊น”๋”ํ•˜๊ณ  ๊ฐ„๊ฒฐํ•ด์กŒ๋‹ค.

Tailwind-merge

ํด๋ž˜์Šค๋ฅผ ์กฐํ•ฉํ•˜๊ณ  ์ค‘๋ณต์„ ํ”ผํ•˜์—ฌ ์Šคํƒ€์ผ ์ถฉ๋Œ ์ด์Šˆ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ณ , ํšจ์œจ์ ์œผ๋กœ ์Šคํƒ€์ผ์„ ์ •์˜ํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋„๊ตฌ

Install

yarn add tailwind-merge

Reference

  • twMerge โ†’ ์—ฌ๋Ÿฌ ํด๋ž˜์Šค๋“ค์„ ๋ณ‘ํ•ฉํ•˜๋Š” ์œ ํ‹ธ ํ•จ์ˆ˜, ์ค‘๋ณต๋œ ํด๋ž˜์Šค ์ถฉ๋Œ์„ ํ•ด๊ฒฐํ•˜๊ณ , ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์ „๋‹ฌ๋œ ํด๋ž˜์Šค๋ฅผ ์œ ์ง€ํ•œ๋‹ค.

  • twJoin โ†’ twMerge()์™€ ๋‹ฌ๋ฆฌ ์ถฉ๋Œ์„ ํ•ด๊ฒฐํ•˜์ง€ ์•Š๋Š”๋‹ค. clsx์™€ ๋น„์Šทํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์‚ฌ์šฉ๋ฒ•๋„ ๋น„์Šทํ•˜๋‹ค. But ๊ฐ์ฒด ์ธ์ž๋Š” ์ง€์›ํ•˜์ง€ ์•Š์Œ

Last updated