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

Color Wheel (색상 휠)

원문 보기 위키 갱신

HSL 또는 HSB 색상 값의 색조(hue)를 원형 트랙 위에서 조정할 수 있게 해주는 컴포넌트예요.

출처: 문서

본문

임포트 (Import)

import { ColorWheel } from "@kobalte/core/color-wheel";
// or
import { Root, Track, ... } from "@kobalte/core/color-wheel";

기능 (Features)

  • HSL 또는 HSB 색상 값의 색조를 조정할 수 있어요.
  • 트랙 클릭 또는 터치로 값을 변경할 수 있어요.
  • 오른쪽 또는 왼쪽 방향을 지원해요.
  • 사용자 정의 값 라벨을 지원해요.
  • 화면 낭독기 사용자를 위한 현지화된 색상 설명을 제공해요.
  • 제어(controlled) 또는 비제어(uncontrolled) 방식으로 사용할 수 있어요.

구조 (Anatomy)

색상 휠은 다음 요소로 구성돼요.

  • ColorWheel: 색상 휠의 루트 컨테이너예요.
  • ColorWheel.Track: 색상 휠 트랙을 시각적으로 나타내는 컴포넌트예요.
  • ColorWheel.Thumb: 색상 휠에서 값을 시각적으로 표시하는 데 사용되는 썸(thumb)이에요.
  • ColorWheel.Input: 색상 휠 썸 안에 시각적으로 숨겨진 네이티브 html 입력 요소예요.
  • ColorWheel.Label: 색상 휠에 대한 정보를 사용자에게 알려주는 라벨이에요.
  • ColorWheel.ValueLabel: 현재 값을 사람이 읽을 수 있는 형식으로 나타내는 접근 가능한 라벨 텍스트예요.
<ColorWheel>
	<ColorWheel.Label />
	<ColorWheel.ValueLabel />
	<ColorWheel.Track>
		<ColorWheel.Thumb>
			<ColorWheel.Input />
		</ColorWheel.Thumb>
	</ColorWheel.Track>
</ColorWheel>

예제 (Example)

기본 예제는 공식 문서 페이지에서 ColorWheel, Label, Track, Thumb, Input 조합과 스타일시트로 확인할 수 있어요.

사용법 (Usage)

기본값 (Default value)

기본적으로 ColorWheel은 빨간색(hue = 0)을 기본값으로 하는 비제어 컴포넌트예요. defaultValue prop으로 기본값을 변경할 수 있어요.

import { ColorWheel } from "@kobalte/core/color-wheel";
import { parseColor } from "@kobalte/core/colors";

function App() {
	return (
		<ColorWheel defaultValue={parseColor("hsl(80, 100%, 50%)")}>
			<ColorWheel.Label>Label</ColorWheel.Label>
			<ColorWheel.Track>
				<ColorWheel.Thumb>
					<ColorWheel.Input />
				</ColorWheel.Thumb>
			</ColorWheel.Track>
		</ColorWheel>
	);
}

제어값 (Controlled value)

value prop을 사용해 ColorWheel을 제어할 수 있어요. 사용자가 썸을 드래그하면 onChange 이벤트가 발생하며 새 값을 받아요.

import { createSignal } from "solid-js";

function ControlledValueExample() {
	const [value, setValue] = createSignal(parseColor("hsl(0, 100%, 50%)"));

	return (
		<>
			<ColorWheel value={value()} onChange={setValue}>
				<ColorWheel.Label>Label</ColorWheel.Label>
				<ColorWheel.Track>
					<ColorWheel.Thumb>
						<ColorWheel.Input />
					</ColorWheel.Thumb>
				</ColorWheel.Track>
			</ColorWheel>
			<p>Current color value: {value().toString("hsl")}</p>
		</>
	);
}

두께 (Thickness)

thickness prop은 ColorWheel의 원형 트랙 너비를 제어해요. 이 prop은 필수예요.

<ColorWheel thickness={60}>
	<ColorWheel.Label>Label</ColorWheel.Label>
	<ColorWheel.Track>
		<ColorWheel.Thumb>
			<ColorWheel.Input />
		</ColorWheel.Thumb>
	</ColorWheel.Track>
</ColorWheel>

사용자 정의 값 라벨 (Custom Value Label)

<ColorWheel
	getValueLabel={color =>
		color
			.toFormat("hsl")
			.withChannelValue("saturation", 100)
			.withChannelValue("lightness", 50)
			.withChannelValue("alpha", 1)
			.toString()
	}
>
	<ColorWheel.Label>Label</ColorWheel.Label>
	<ColorWheel.Track>
		<ColorWheel.Thumb>
			<ColorWheel.Input />
		</ColorWheel.Thumb>
	</ColorWheel.Track>
	<div>
		<ColorWheel.ValueLabel />
	</div>
</ColorWheel>

HTML 폼 (HTML forms)

ColorWheel은 HTML 폼 통합을 위한 name prop을 지원해요.

function HTMLFormExample() {
	const onSubmit = (e: SubmitEvent) => {
		// handle form submission.
	};

	return (
		<form onSubmit={onSubmit}>
			<ColorWheel name="hue">
				<ColorWheel.Track>
					<ColorWheel.Thumb>
						<ColorWheel.Input />
					</ColorWheel.Thumb>
				</ColorWheel.Track>
			</ColorWheel>
		</form>
	);
}

API 참조 (API Reference)

ColorWheel

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

Prop Description
value Color
The controlled value of the color wheel. Must be used in conjunction with onChange.
defaultValue Color
The value of the color wheel when initially rendered. Use when you do not need to control the state of the color wheel.
thickness number
The thickness of the track 0-100.
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.
getValueLabel (param: Color) => string
A function to get the accessible label text representing the current value in a human-readable format.
name string
The name of the color wheel, used when submitting an HTML form.
validationState 'valid' | 'invalid'
Whether the color wheel 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 color wheel is disabled.
readOnly boolean
Whether the color wheel items can be selected but not changed by the user.
translations ColorIntlTranslations
Localization strings.
Data attribute Description
data-valid Present when the color wheel is valid according to the validation rules.
data-invalid Present when the color wheel is invalid according to the validation rules.
data-required Present when the color wheel is required.
data-disabled Present when the color wheel is disabled.
data-readonly Present when the color wheel is read only.

ColorWheel.ValueLabel, ColorWheel.Input, ColorWheel.Thumb 및 ColorWheel.Track는 동일한 data-attributes를 공유해요.

ColorWheel.Thumb

현재 색상은 사용자 정의 CSS 속성 --kb-color-current를 통해 썸에서 확인할 수 있어요.

렌더링 요소 (Rendered elements)

Component Default rendered element
ColorWheel div
ColorWheel.Track div
ColorWheel.Thumb span
ColorWheel.Input input
ColorWheel.ValueLabel div
ColorWheel.Label label
ColorWheel.Description div
ColorWheel.ErrorMessage div

접근성 (Accessibility)

키보드 상호작용 (Keyboard Interactions)

Key Description
PageUp Increments the value of the thumb by a larger step.
PageDown Decrements the value of the thumb by a larger step.
ArrowDown Decrements the value of the thumb by the step amount.
ArrowUp Increments the value of the thumb by the step amount.
ArrowRight Increments the value of the thumb by the step value.
ArrowLeft Decrements the value of the thumb by the step value.
Home Sets the value of the thumb to the minimum value.
End Sets the value of the thumb to the maximum value.

더 알아보기 (Learn more)