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

Color Area (컬러 영역)

원문 보기 위키 갱신

컬러 영역(Color Area)은 사용자가 2차원 그라데이션 배경 위에서 RGB, HSL 또는 HSB 색상 값의 두 채널(channel)을 조정할 수 있게 해주는 컴포넌트예요. 색상 선택기에서 색을 직접 골라 조정할 때 주로 사용해요.

출처: 문서

본문

Import (불러오기)

import { ColorArea } from "@kobalte/core/color-area";
// or
import { Root, Background, ... } from "@kobalte/core/color-area";

Features (기능)

  • 제어 방식(controlled) 또는 비제어 방식(uncontrolled)으로 사용할 수 있어요.
  • RGB, HSL 또는 HSB 색상 값의 두 채널 값을 조정하는 것을 지원해요.
  • 컬러 영역 배경을 누르면 썸(thumb)이 해당 위치로 이동해요.
  • 방향키로 값을 step만큼 변경하고, shift + 방향키, page up/down, home, end 키로 값을 page step만큼 변경하는 것을 지원해요.
  • 컬러 영역 비활성화를 지원해요.
  • 드래그 중 텍스트 선택을 방지해요.
  • ARIA를 통해 보조 기술에 2D 슬라이더 요소로 노출돼요.
  • 터치 스크린 낭독기를 지원하기 위해 그룹 안에 두 개의 숨겨진 네이티브 input 요소를 사용해요.
  • 기본적으로 지역화된 채널 이름을 이용한 자동 ARIA 라벨링을 제공해요.
  • RTL 로케일에서의 미러링을 지원해요.

Anatomy (구조)

컬러 영역은 다음의 부분들로 이루어져요:

  • ColorArea: 컬러 영역의 루트 컨테이너예요.
  • ColorArea.Background: 사용자가 선택할 수 있는 색상 범위를 시각적으로 나타내는 컴포넌트예요.
  • ColorArea.Thumb: 컬러 영역 배경에서 값을 시각적으로 나타내는 데 사용되는 썸이에요.
  • ColorArea.HiddenInputX: 컬러 영역 썸 안에서 시각적으로 숨겨진 가로 방향 네이티브 html input이에요.
  • ColorArea.HiddenInputY: 컬러 영역 썸 안에서 시각적으로 숨겨진 세로 방향 네이티브 html input이에요.
  • ColorArea.Label: 사용자에게 컬러 영역에 대한 정보를 주는 라벨이에요.
<ColorArea>
  <ColorArea.Label />
  <ColorArea.Background>
    <ColorArea.Thumb>
      <ColorArea.HiddenInputX />
      <ColorArea.HiddenInputY />
    </ColorArea.Thumb>
  </ColorArea.Background>
</ColorArea>

Example (예제)

기본 예제를 통해 컬러 영역의 구조와 스타일링 방식을 확인해 보세요. 인터랙티브한 미리보기는 출처 문서에서 직접 확인할 수 있어요.

Usage (사용법)

defaultValue나 value에 전달하는 값은 Color 객체여야 해요. parseColor 함수로 문자열에서 색상을 파싱하면 Color 객체를 얻을 수 있답니다. xChannel과 yChannel은 색상 값에 포함된 채널 중 하나여야 해요. 예를 들어 RGB 색상에서는 "red", "green", "blue" 채널을 사용할 수 있어요. xChannel이나 yChannel을 제공하지 않으면, RGB 색상 공간에서는 빨간색 채널이 xChannel(가로축)에, 초록색 채널이 yChannel(세로축)에 매핑돼요. 마찬가지로 HSL과 HSB 색상 공간에서는 색상(hue) 채널이 xChannel(가로축)에, 채도(saturation) 채널이 yChannel(세로축)에 매핑돼요.

Default value (기본값)

<ColorArea defaultValue={parseColor("rgb(2, 132, 197)")}>
	<ColorArea.Label>Label</ColorArea.Label>
	<ColorArea.Background>
		<ColorArea.Thumb>
			<ColorArea.HiddenInputX />
			<ColorArea.HiddenInputY />
		</ColorArea.Thumb>
	</ColorArea.Background>
</ColorArea>

Controlled value (제어 방식)

import { createSignal } from "solid-js";

const [value, setValue] = createSignal(parseColor("hsl(0, 100%, 50%)"));
<>
	<ColorArea value={value()} onChange={setValue}>
		<ColorArea.Label>Label</ColorArea.Label>
		<ColorArea.Background>
			<ColorArea.Thumb>
				<ColorArea.HiddenInputX />
				<ColorArea.HiddenInputY />
			</ColorArea.Thumb>
		</ColorArea.Background>
	</ColorArea>
	<p>Current color value: {value().toString("hsl")}</p>
</>;

xChannel and yChannel (x 및 y 채널)

컬러 영역의 각 축에 대한 색상 채널은 xChannel과 yChannel prop으로 지정할 수 있어요. 색상의 채널 이름 배열은 color.getColorChannels 메서드로 얻을 수 있어요.

const [value, setValue] = createSignal(parseColor("rgb(100, 149, 237)"));
const [rChannel, gChannel, bChannel] = value().getColorChannels();

<ColorArea value={value()} onChange={setValue} xChannel={gChannel} yChannel={bChannel}>
	<ColorArea.Label>Label</ColorArea.Label>
	<ColorArea.Background>
		<ColorArea.Thumb>
			<ColorArea.HiddenInputX />
			<ColorArea.HiddenInputY />
		</ColorArea.Thumb>
	</ColorArea.Background>
</ColorArea>;

HTML forms (HTML 폼)

ColorArea는 HTML 폼과의 통합을 위해 xName과 yName prop을 지원해요.

<ColorArea defaultValue={parseColor("rgb(100, 149, 237)")} xName="red" yName="green">
	<ColorArea.Label>Label</ColorArea.Label>
	<ColorArea.Background>
		<ColorArea.Thumb>
			<ColorArea.HiddenInputX />
			<ColorArea.HiddenInputY />
		</ColorArea.Thumb>
	</ColorArea.Background>
</ColorArea>

API Reference (API 참조)

ColorArea

ColorArea는 @kobalte/core/color-area의 Root 임포트와 동일해요.

Prop Description
value Color
The controlled value of the color area..
defaultValue Color
The value of the color area when initially rendered.
colorSpace ColorSpace
The color space that the color area operates in. The xChannel and yChannel must be in this color space.
onChange (value: Color) => void
Event handler called when the value changes.
onChangeEnd (value: Color) => void
Event handler called when the value changes at the end of an interaction.
xChannel ColorChannel
Color channel for the horizontal axis.
yChannel ColorChannel
Color channel for the vertical axis.
xName string
The name of the x channel input element, used when submitting an HTML form. See MDN.
yName string
The name of the y channel input element, used when submitting an HTML form. See MDN.
name string
The name of the color area, used when submitting an HTML form.
validationState 'valid' | 'invalid'
Whether the color area should display its "valid" or "invalid" visual styling.
required boolean
Whether the user must check a radio group item before the owning form can be submitted.
disabled boolean
Whether the radio group is disabled.
readOnly boolean
Whether the radio group items can be selected but not changed by the user.
translations ColorAreaIntlTranslations
Localization strings.
Data attribute Description
data-valid Present when the slider is valid according to the validation rules.
data-invalid Present when the slider is invalid according to the validation rules.
data-required Present when the user must slider an item before the owning form can be submitted.
data-disabled Present when the slider is disabled.
data-readonly Present when the slider is read only.

ColorArea.Background, ColorArea.Thumb, ColorArea.HiddenInputX, ColorArea.HiddenInputY와 ColorArea.Label는 동일한 data-attribute를 공유해요.

ColorArea.Thumb

현재 색상은 CSS 커스텀 프로퍼티 --kb-color-current를 사용해 썸에서 확인할 수 있어요.

ColorArea.HiddenInputX

Data attribute Description
data-orientation='horizontal' Always present

ColorArea.HiddenInputY

Data attribute Description
data-orientation='vertical' Always present

Rendered elements (렌더링되는 요소)

Component Default rendered element
ColorArea div
ColorArea.Background div
ColorArea.Thumb span
ColorArea.HiddenInputX input
ColorArea.HiddenInputY input
ColorArea.Label label
ColorArea.Description div
ColorArea.ErrorMessage div

Accessibility (접근성)

Keyboard Interactions (키보드 상호작용)

Key Description
PageUp Increments the value of the thumb in the vertical axis by a larger step.
PageDown Decrements the value of the thumb in the vertical axis by a larger step.
ArrowDown Decrements the value of the thumb in the vertical axis by the step amount.
ArrowUp Increments the value of the thumb in the vertical axis by the step amount.
ArrowRight Increments the value of the thumb in the horizontal axis by the step amount.
ArrowLeft Decrements the value of the thumb in the vertical axis by the step amount.
Home Decrements the value of the thumb in the horizontal axis by a larger step.
End Increments the value of the thumb in the horizontal axis by a larger step.

더 알아보기 (Learn more)