Components
Color Thumb
The round handle of a color control: what you drag in a color area, a color slider or a color wheel.
Introduction
Color Thumb is the handle you drag to choose a color. It is the small circle in the square of a color picker, on the track of a hue slider, and on the ring of a color wheel. It is filled with the color it stands on, so you see what you are choosing, and it stays visible on any color.
- A white ring with a dark line inside and outside it, so it shows on white and on black alike.
- It grows a little while it is dragged or has the keyboard focus, and gets a focus ring from the keyboard. Both stop for people who prefer reduced motion.
- Three sizes, and a
classNameto change the look. - It is built on react-aria's ColorThumb, which gives it its place, its color and its keyboard and screen reader behavior.
A thumb does nothing alone: it works inside a color area, the track of a color slider or the track of a color wheel, which give it those. Color Area, Color Slider and Color Picker already use it. Use it yourself to build a color control of your own with the same handle. 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.
1. Add the styles
src/types/color-thumb-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for ColorThumb. react-aria sets where the thumb is (its left and top)
// and its fill (the color it stands on) as inline styles, so they are not here.
// The thumb is a white ring with a dark line inside and one outside it, which
// keeps it visible on any color, light or dark.
export const colorThumbStyles = tv({
base: [
"rounded-full border-2 border-white outline-none",
"shadow-[0_0_0_1px_rgb(0_0_0/0.45),inset_0_0_0_1px_rgb(0_0_0/0.45)]",
"transition-[width,height] duration-150 ease-out motion-reduce:transition-none",
"data-focus-visible:ring-2 data-focus-visible:ring-ring",
"data-disabled:opacity-60",
],
variants: {
// It grows a little while it is dragged or has the keyboard focus.
size: {
sm: "size-4 data-dragging:size-5 data-focus-visible:size-5",
md: "size-5 data-dragging:size-6 data-focus-visible:size-6",
lg: "size-6 data-dragging:size-8 data-focus-visible:size-8",
},
// react-aria places the thumb along the track. The other axis is centered
// here: `horizontal` for a slider that lies along the width, `vertical` for
// one that stands up. An area and a wheel are placed on both, so they set none.
orientation: {
horizontal: "top-1/2",
vertical: "left-1/2",
},
},
defaultVariants: { size: "md" },
});2. Add the Color Thumb file
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)}
/>
);
}3. Use it
import { ColorThumb } from "@/components/ui/color-thumb/color-thumb";Anatomy
The thumb is the child of the control it belongs to.
import { ColorArea } from "react-aria-components";
<ColorArea aria-label="Color" defaultValue="#3b82f6" className="relative h-44 w-44 rounded-lg">
<ColorThumb />
</ColorArea>States
What the thumb looks like as it is used.
| State | What it looks like |
|---|---|
| Resting | 20 px, a white ring with a dark line inside and outside it, filled with the color it stands on. |
| Dragging | Grows to 24 px while it is pressed and moved. |
| Focused with the keyboard | Grows to 24 px and gets a focus ring. |
| Disabled | Dimmed to 60%. |
Examples
In an area
In a color area the thumb is placed on both axes by react-aria, so it needs no orientation.
In a slider
In the track of a slider the thumb is placed along the track, and orientation centers it across. It takes the color of the point it is on: the hue slider above, the opacity slider below.
In a vertical slider
For a slider that stands up, set orientation="vertical".
In a wheel
On a color wheel the thumb goes round the ring. Like the area, it is placed on both axes.
Sizes
Small, medium and large are 16, 20 and 24 pixels at rest, and a little bigger while they are dragged. Move a thumb of each.
sm
md
lg
Styling
className is added to the classes of the thumb. Use it to change the corners, the ring or anything else.
Component API
Color Thumb takes the props below.
| Prop | Type | Default |
|---|---|---|
| size | "sm" | "md" | "lg" | "md" |
| orientation | "horizontal" | "vertical" | - |
| className | string | - |
| children | ReactNode | - |