Components
Color Wheel
A ring of colors to pick a hue from: drag the thumb round it and the hue changes while the saturation and the brightness stay.
Introduction
Color Wheel is a ring painted with every hue. Drag the thumb round it, or focus it and use the arrow keys, and the hue of the color changes. The saturation and the brightness stay where they are, so it fits beside a ColorArea, which sets those two.
- Built on react-aria's ColorWheel: mouse, touch and keyboard all work, and a screen reader hears the hue and its name, such as "vibrant cyan blue".
- The color is a plain string in CSS, such as
"#3b82f6", and the whole color goes back as hex, rgb, hsl or hsb. - The other channels and a hue that hex loses when the color is gray or black are kept in the wheel. Controls that share a color through a string should give it back as
hsborhsl, which carry every channel. - Three sizes or any number of pixels, a ring as thin or as thick as you like, a disc of the color in the hole, and the hue as a number.
- The thumb is the same
ColorThumbas the other color controls.
The wheel picks the hue. For a whole picker in one, see ColorPicker. It is a Client Component.
Installation
centerUI has no CLI or registry yet: components are copied into your project.
# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components tailwind-variants tailwind-merge clsxManual installation
Copy these files into your project and keep the paths, or adjust the imports. It uses Color Thumb.
1. Add Color Thumb
src/components/ui/color-thumb/color-thumb.tsxShow code
"use client";
import {
ColorThumb as AriaColorThumb,
type ColorThumbProps as AriaColorThumbProps,
} from "react-aria-components";
import { cn } from "@/lib/cn";
import { colorThumbStyles } from "@/types/color-thumb-styles";
export interface ColorThumbProps
extends Omit<AriaColorThumbProps, "className"> {
/** How big it is. It grows a little while it is dragged or has focus. Defaults to `md`. */
size?: "sm" | "md" | "lg";
/**
* For the thumb of a slider: which way the track goes, so the thumb is centered
* across it. Leave it out in a color area or a color wheel, where the thumb is
* placed on both axes.
*/
orientation?: "horizontal" | "vertical";
className?: string;
}
/**
* The round handle of a color control: the thing you drag in a `ColorArea`, a
* `ColorSlider` or a color wheel. It is filled with the color it stands on, has a
* white ring with a dark line each side of it so it shows on any color, grows a
* little while it is dragged or has focus, and gets a focus ring from the
* keyboard.
*
* It only works inside one of those, which give it its place, its color and its
* behavior: put it as the child of a react-aria `ColorArea`, of the `SliderTrack`
* of a `ColorSlider`, or of the `ColorWheelTrack` of a `ColorWheel`. `ColorArea`,
* `ColorSlider` and `ColorPicker` already use it. Use it yourself to build a
* control of your own with the same handle.
*
* @example
* <ColorArea>
* <ColorThumb />
* </ColorArea>
* <ColorSlider channel="hue">
* <SliderTrack><ColorThumb orientation="horizontal" /></SliderTrack>
* </ColorSlider>
*/
export function ColorThumb({
size,
orientation,
className,
...props
}: ColorThumbProps) {
return (
<AriaColorThumb
{...props}
className={cn(colorThumbStyles({ size, orientation }), className)}
/>
);
}2. Add the styles
src/types/color-wheel-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for ColorWheel. The rainbow ring is painted by react-aria (a conic
// gradient, masked to a ring by the two radii it is given), and the place of the
// thumb on it is set as an inline style, so they are not here.
export const colorWheelStyles = tv({
slots: {
root: "inline-flex max-w-full flex-col items-center gap-2 text-fg",
// The wheel and what sits in the middle of it.
frame: "relative shrink-0",
wheel: "data-disabled:cursor-not-allowed data-disabled:opacity-60",
// In the hole: a disc of the color, kept off the ring by a gap.
center:
"pointer-events-none absolute inset-0 flex items-center justify-center",
preview: "rounded-full border shadow-[inset_0_0_0_1px_rgb(0_0_0/0.06)]",
value: "font-mono text-xs text-muted-fg tabular-nums",
},
});
/** How big the wheel is, in pixels, for each size. */
export const colorWheelSizes = { sm: 128, md: 176, lg: 240 } as const;3. Add the Color Wheel file
src/components/ui/color-wheel/color-wheel.tsxShow code
"use client";
import { useState } from "react";
import {
ColorWheel as AriaColorWheel,
type Color,
ColorWheelTrack,
parseColor,
} from "react-aria-components";
import { ColorThumb } from "@/components/ui/color-thumb/color-thumb";
import { cn } from "@/lib/cn";
import { colorWheelSizes, colorWheelStyles } from "@/types/color-wheel-styles";
/** How the color is written when it is given back. */
export type ColorWheelFormat = "hex" | "rgb" | "hsl" | "hsb";
export interface ColorWheelProps {
/** The color, written in CSS: `"#3b82f6"`, `"rgb(59, 130, 246)"`, `"hsl(217, 91%, 60%)"` or `"hsb(217, 76%, 96%)"`. The wheel changes its hue. For a controlled wheel. */
value?: string;
/** The color to start with, for an uncontrolled wheel. */
defaultValue?: string;
/** Called as the thumb moves, with the whole color written in `format`. */
onChange?: (color: string) => void;
/** Called when the thumb is let go, or a key press is done, with the color written in `format`. */
onChangeEnd?: (color: string) => void;
/** How the color is written when it is given back. It carries its opacity when the color is not solid. Defaults to `hex`. */
format?: ColorWheelFormat;
/** How big the wheel is: `sm`, `md` or `lg` (128, 176 and 240 pixels), or a number of pixels. Defaults to `md`. */
size?: "sm" | "md" | "lg" | number;
/** How thick the ring is, in pixels. Defaults to a seventh of the size. */
thickness?: number;
/** A disc of the color in the hole of the wheel. Defaults to `false`. */
showPreview?: boolean;
/** The hue as a number of degrees, such as 217°, under the wheel. Defaults to `false`. */
showValue?: boolean;
/** The name of the form field that holds the hue, sent as a number of degrees. */
name?: string;
isDisabled?: boolean;
/** Names the wheel for screen readers, such as "Hue". Defaults to "Hue". */
"aria-label"?: string;
className?: string;
}
const fallback = "hsb(220, 80%, 90%)";
// The wheel works on the hue of an hsb or an hsl color: the color is kept in hsb,
// so the saturation and the brightness stay where they are, and a hue that hex
// loses when the color is gray or black is not lost.
function toColor(text: string | undefined): Color {
try {
return parseColor(text ?? fallback).toFormat("hsb");
} catch {
return parseColor(fallback).toFormat("hsb");
}
}
const write = (color: Color, format: ColorWheelFormat) => {
const alpha = color.getChannelValue("alpha") < 1;
const text = color.toString(alpha ? (`${format}a` as const) : format);
return format === "hex" ? text.toLowerCase() : text;
};
/**
* A ring of colors to pick a hue from: drag the thumb round the ring, or use the
* arrow keys, and the hue of the color changes while its saturation and
* brightness stay. Pair it with a `ColorArea` for the other two, or use it alone
* to choose an accent hue.
*
* Built on react-aria's ColorWheel, so it works with a mouse, a finger and the
* keyboard, and a screen reader hears the hue and its name. The color is a plain
* string in CSS and goes back as hex unless you choose another `format`. Sliders
* and wheels that share one color through a string should give it back as `hsb`
* or `hsl`, which carry every channel: hex loses the hue of a gray or a black.
*
* @example
* <ColorWheel aria-label="Hue" defaultValue="#3b82f6" showPreview onChange={setColor} />
*/
export function ColorWheel({
value,
defaultValue,
onChange,
onChangeEnd,
format = "hex",
size = "md",
thickness,
showPreview = false,
showValue = false,
name,
isDisabled,
"aria-label": ariaLabel = "Hue",
className,
}: ColorWheelProps) {
const styles = colorWheelStyles();
const diameter = typeof size === "number" ? size : colorWheelSizes[size];
const outer = diameter / 2;
const ring = Math.min(thickness ?? Math.round(diameter / 7), outer - 8);
const inner = outer - ring;
const [color, setColor] = useState<Color>(() =>
toColor(value ?? defaultValue),
);
// A controlled wheel follows its value, but only when it is really another
// color: the one it gave back comes round again, and must not change what it
// holds. Two colors are the same when every channel matches.
if (value !== undefined) {
const next = toColor(value);
if (next.toString("hsba") !== color.toString("hsba")) setColor(next);
}
const hue = Math.round(color.getChannelValue("hue"));
// The disc in the hole sits a little off the ring.
const disc = Math.max(inner * 2 - 16, 0);
return (
<div className={cn(styles.root(), className)}>
<div
className={styles.frame()}
style={{ width: diameter, height: diameter }}
>
<AriaColorWheel
value={color}
outerRadius={outer}
innerRadius={inner}
name={name}
isDisabled={isDisabled}
aria-label={ariaLabel}
onChange={(next) => {
setColor(next);
onChange?.(write(next, format));
}}
onChangeEnd={(next) => onChangeEnd?.(write(next, format))}
className={styles.wheel()}
>
<ColorWheelTrack />
<ColorThumb size={diameter < 150 ? "sm" : "md"} />
</AriaColorWheel>
{showPreview && disc > 8 ? (
<div className={styles.center()}>
<span
aria-hidden="true"
className={styles.preview()}
style={{
width: disc,
height: disc,
background: color.toString("css"),
}}
/>
</div>
) : null}
</div>
{showValue ? <output className={styles.value()}>{hue}°</output> : null}
</div>
);
}4. Use it
import { ColorWheel } from "@/components/ui/color-wheel/color-wheel";Anatomy
A ring with a thumb on it, and, if you ask, a disc in the hole and the hue under it.
<ColorWheel
aria-label="Hue"
defaultValue="#3b82f6"
showPreview
onChange={(color) => console.log(color)} // "#3b82f6"
/>| Part | Rendered as | Notes |
|---|---|---|
| Ring | div | A rainbow ring painted with every hue. Pressing it moves the thumb to that point. |
| Thumb | div | The round handle on the ring. It is filled with the color of its hue. |
| Preview | span | A disc of the whole color in the hole of the ring. Off unless showPreview. |
| Value | output | The hue in degrees under the wheel, such as 217°. Off unless showValue. |
Value
The color is text in CSS. The wheel changes its hue, and the whole color goes back.
// Any color in CSS. The wheel changes its hue, and the whole color goes back
// in the format you choose (hex by default).
"#3b82f6"
"rgb(59, 130, 246)"
"hsl(217, 91%, 60%)"
"hsb(217, 76%, 96%)"Keyboard
The wheel is one place in the tab order. These keys work while it has focus.
| Key | What it does |
|---|---|
| Arrow Right, Arrow Up | Turn the thumb clockwise by one degree. |
| Arrow Left, Arrow Down | Turn the thumb counterclockwise by one degree. |
| Page Up, Page Down | Turn by a larger step. |
| Home, End | Go to the start or the end of the ring, 0° and 360°. |
Examples
Usage
Drag the thumb round the ring, or press the ring where you want the hue.
Controlled
Keep the color in your own state with value and onChange. Giving it a new color, such as from a button, moves the thumb to its hue.
#3b82f6With a color area
The wheel sets the hue, the area sets the saturation and the brightness, and the field types the color. They hold one color, given back as hsb so that no channel is lost.
Preview and value
showPreview draws a disc of the whole color in the hole of the ring, and showValue writes the hue in degrees under it.
Sizes
128, 176 and 240 pixels, or a number of pixels of your own.
Thickness
thickness is how wide the ring is, in pixels. A thick ring leaves a smaller hole.
When it is let go
onChange is called all the while the thumb moves, which is many times a second. onChangeEnd is called once, when it is let go or a key press is done, which is the moment to save.
moving: #3b82f6
saved when let go: #3b82f6
Formats
format decides how the whole color is written when it is given back. Move a thumb in each.
hex: move the thumb
rgb: move the thumb
hsl: move the thumb
hsb: move the thumb
In a form
Name the wheel with name, and its hue is submitted as a number of degrees.
Disabled
Component API
Color Wheel takes the props below.
| Prop | Type | Default |
|---|---|---|
| value | string | - |
| defaultValue | string | a blue |
| onChange | (color: string) => void | - |
| onChangeEnd | (color: string) => void | - |
| format | "hex" | "rgb" | "hsl" | "hsb" | "hex" |
| size | "sm" | "md" | "lg" | number | "md" |
| thickness | number (px) | a seventh of the size |
| showPreview | boolean | false |
| showValue | boolean | false |
| name | string | - |
| isDisabled | boolean | false |
| aria-label | string | "Hue" |
| className | string | - |