본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

ColorSwatch (컬러 스와치)

원문 보기 위키 갱신

ColorSwatch는 접근성을 지원하며 색상 값을 시각적으로 미리 보여주는 컴포넌트예요. 해요체로 쉽게 설명드릴게요.

출처: HeroUI ColorSwatch

본문

사용법 (Usage)

import { ColorSwatch } from '@heroui/react';
{"name": "color-swatch-basic"}

예시 (Examples)

크기 (Sizes)

{"name": "color-swatch-sizes"}

모양 (Shapes)

{"name": "color-swatch-shapes"}

투명도 (Transparency)

{"name": "color-swatch-transparency"}

렌더 함수 (Render Function)

style 렌더 프롭을 사용해 색상 값에 접근하고 커스텀 시각 효과를 만들 수 있어요.

{"name": "color-swatch-render-function"}

접근성 (Accessibility)

colorName을 사용해 색상의 커스텀 접근 가능 이름을 제공하고, aria-label을 사용해 색상 사용 방식에 대한 맥락을 추가할 수 있어요.

{"name": "color-swatch-accessibility"}

커스터마이징 (Customization)

Tailwind CSS

{"name": "color-swatch-custom-styles"}

Global CSS

ColorSwatch 컴포넌트 클래스를 커스터마이징하려면 @layer components 지시어를 사용할 수 있어요. 더 알아보기.

@layer components {
  .color-swatch {
    @apply border-2 border-white;
  }
}

스타일링 참고 (Styling Reference)

HeroUI는 BEM 방법론을 따라 컴포넌트 변형과 상태를 재사용하고 쉽게 커스터마이징할 수 있게 해요.

CSS 클래스 (CSS Classes)

ColorSwatch 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기):

기본 클래스 [!toc]

  • .color-swatch - 투명도를 위한 체크 무늬 배경이 있는 기본 스와치 스타일

모양 클래스 [!toc]

  • .color-swatch--circle - 원형 모양 (기본값)
  • .color-swatch--square - 둥근 모서리의 사각형 모양

크기 클래스 [!toc]

  • .color-swatch--xs - 초소형 (16px)
  • .color-swatch--sm - 소형 (24px)
  • .color-swatch--md - 중형 (32px, 기본값)
  • .color-swatch--lg - 대형 (36px)
  • .color-swatch--xl - 초대형 (40px)

API 참고 (API Reference)

ColorSwatch

Prop Type Default Description
color string | Color - The color value to display (hex, rgb, hsl, etc.)
colorName string - Accessible name for the color (overrides auto-generated description)
className string - Additional CSS classes
shape "circle" | "square" "circle" Shape of the swatch
size "xs" | "sm" | "md" | "lg" | "xl" "md" Size of the swatch
style CSSProperties | ((renderProps) => CSSProperties) - Inline styles or render props function with access to color
aria-label string - Accessible label for the swatch
render DOMRenderFunction<keyof React.JSX.IntrinsicElements, ColorSwatchRenderProps> - Overrides the default DOM element with a custom render function.

스타일 렌더 프롭 (Style Render Props)

style prop을 함수로 사용하면 색상에 접근할 수 있는 렌더 프롭을 받아요:

<ColorSwatch
  color="#0485F7"
  style={({ color }) => ({
    boxShadow: `0 4px 14px ${color.toString("css")}80`,
  })}
/>

color 객체는 다음과 같은 메서드를 제공해요:

  • color.toString("css") - CSS 색상 문자열 반환
  • color.toString("hex") - hex 색상 문자열 반환
  • color.getChannelValue("alpha") - 알파 채널 값 반환

더 알아보기 (Learn more)