Kobalte.v0.13.7

Color Wheel

Allows users to adjust the hue of an HSL or HSB color value on a circular track.

Import

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

Features

  • Support for adjusting the hue of an HSL or HSB color value.
  • Support click or touch on track to change value.
  • Support right or left direction.
  • Support for custom value label.
  • Localized color descriptions for screen reader users.
  • Can be controlled or uncontrolled.

Anatomy

The color wheel consists of:

  • ColorWheel: The root container for the color wheel.
  • ColorWheel.Track: The component that visually represents the color wheel track.
  • ColorWheel.Thumb: The thumb that is used to visually indicate a value in the color wheel.
  • ColorWheel.Input: The native html input that is visually hidden in the color wheel thumb.
  • ColorWheel.Label: The label that gives the user information on the color wheel.
  • ColorWheel.ValueLabel: The accessible label text representing the current value in a human-readable format.
tsx
<ColorWheel>
<ColorWheel.Label />
<ColorWheel.ValueLabel />
<ColorWheel.Track>
<ColorWheel.Thumb>
<ColorWheel.Input />
</ColorWheel.Thumb>
</ColorWheel.Track>
</ColorWheel>
tsx
<ColorWheel>
<ColorWheel.Label />
<ColorWheel.ValueLabel />
<ColorWheel.Track>
<ColorWheel.Thumb>
<ColorWheel.Input />
</ColorWheel.Thumb>
</ColorWheel.Track>
</ColorWheel>

Example

Usage

Default value

By default, ColorWheel is uncontrolled with a default value of red (hue = 0). You can change the default value using the defaultValue prop.

tsx
import { ColorWheel } from "@kobalte/core/color-wheel";
import { parseColor } from "@kobalte/core";
function App() {
return (
<ColorWheel defaultValue={parseColor("hsl(80, 100%, 50%)")} thickness={24}>
<ColorWheel.Track>
<ColorWheel.Thumb>
<ColorWheel.Input />
</ColorWheel.Thumb>
</ColorWheel.Track>
</ColorWheel>
);
}
tsx
import { ColorWheel } from "@kobalte/core/color-wheel";
import { parseColor } from "@kobalte/core";
function App() {
return (
<ColorWheel defaultValue={parseColor("hsl(80, 100%, 50%)")} thickness={24}>
<ColorWheel.Track>
<ColorWheel.Thumb>
<ColorWheel.Input />
</ColorWheel.Thumb>
</ColorWheel.Track>
</ColorWheel>
);
}

Controlled value

A ColorWheel can be made controlled using the value prop. The onChange event is fired when the user drags the thumb and receive the new value.

Current color value: hsl(0, 100%, 50%)

tsx
import { createSignal } from "solid-js";
function ControlledValueExample() {
const [value, setValue] = createSignal(parseColor("hsl(0, 100%, 50%)"));
return (
<>
<ColorWheel value={value()} onChange={setValue} thickness={24}>
<ColorWheel.Track>
<ColorWheel.Thumb>
<ColorWheel.Input />
</ColorWheel.Thumb>
</ColorWheel.Track>
</ColorWheel>
<p>Current color value: {value().toString("hsl")}</p>
</>
);
}
tsx
import { createSignal } from "solid-js";
function ControlledValueExample() {
const [value, setValue] = createSignal(parseColor("hsl(0, 100%, 50%)"));
return (
<>
<ColorWheel value={value()} onChange={setValue} thickness={24}>
<ColorWheel.Track>
<ColorWheel.Thumb>
<ColorWheel.Input />
</ColorWheel.Thumb>
</ColorWheel.Track>
</ColorWheel>
<p>Current color value: {value().toString("hsl")}</p>
</>
);
}

Thickness

The thickness prop controls the width of the ColorWheel's circular track. This prop is required.

tsx
<ColorWheel thickness={56}>
<ColorWheel.Track>
<ColorWheel.Thumb>
<ColorWheel.Input />
</ColorWheel.Thumb>
</ColorWheel.Track>
</ColorWheel>
tsx
<ColorWheel thickness={56}>
<ColorWheel.Track>
<ColorWheel.Thumb>
<ColorWheel.Input />
</ColorWheel.Thumb>
</ColorWheel.Track>
</ColorWheel>

Custom Value Label

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

HTML forms

ColorWheel supports the name prop for integration with HTML forms.

tsx
function HTMLFormExample() {
const onSubmit = (e: SubmitEvent) => {
// handle form submission.
};
return (
<form onSubmit={onSubmit}>
<ColorWheel name="hue" thickness={24}>
<ColorWheel.Track>
<ColorWheel.Thumb>
<ColorWheel.Input />
</ColorWheel.Thumb>
</ColorWheel.Track>
</ColorWheel>
</form>
);
}
tsx
function HTMLFormExample() {
const onSubmit = (e: SubmitEvent) => {
// handle form submission.
};
return (
<form onSubmit={onSubmit}>
<ColorWheel name="hue" thickness={24}>
<ColorWheel.Track>
<ColorWheel.Thumb>
<ColorWheel.Input />
</ColorWheel.Thumb>
</ColorWheel.Track>
</ColorWheel>
</form>
);
}

API Reference

ColorWheel

ColorWheel is equivalent to the Root import from @kobalte/core/color-wheel.

PropDescription
valueColor
The controlled value of the color wheel. Must be used in conjunction with onChange.
defaultValueColor
The value of the color wheel when initially rendered. Use when you do not need to control the state of the color wheel.
thicknessnumber
The thickness of the track.
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.
namestring
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.
requiredboolean
Whether the user must check a radio group item before the owning form can be submitted.
disabledboolean
Whether the color wheel is disabled.
readOnlyboolean
Whether the color wheel items can be selected but not changed by the user.
translationsColorIntlTranslations
Localization strings.
Data attributeDescription
data-validPresent when the color wheel is valid according to the validation rules.
data-invalidPresent when the color wheel is invalid according to the validation rules.
data-requiredPresent when the color wheel is required.
data-disabledPresent when the color wheel is disabled.
data-readonlyPresent when the color wheel is read only.

ColorWheel.ValueLabel, ColorWheel.Input, ColorWheel.Thumb and ColorWheel.Track share the same data-attributes.

Rendered elements

ComponentDefault rendered element
ColorWheeldiv
ColorWheel.Trackdiv
ColorWheel.Thumbspan
ColorWheel.Inputinput
ColorWheel.ValueLabeldiv

Accessibility

Keyboard Interactions

KeyDescription
PageUpIncrements the value of the thumb by a larger step.
PageDownDecrements the value of the thumb by a larger step.
ArrowDownDecrements the value of the thumb by the step amount.
ArrowUpIncrements the value of the thumb by the step amount.
ArrowRightIncrements the value of the thumb by the step value.
ArrowLeftDecrements the value of the thumb by the step value.
HomeSets the value of the thumb to the minimum value.
EndSets the value of the thumb to the maximum value.