ColorSlider allow users to adjust an individual channel of a color value.
< ColorSlider channel = "hue" defaultValue = "hsl(200, 100%, 50%)" />
Installation
Install the following dependencies: npm install react-aria-components
Copy and paste the following code into your project. color-slider.tsx
color-thumb.tsx
field.tsx
"use client" ;
import {
composeRenderProps ,
ColorSlider as AriaColorSlider ,
SliderOutput as AriaSliderOutput ,
SliderTrack as AriaSliderTrack ,
type ColorSliderProps as AriaColorSliderProps ,
type SliderOutputProps as AriaSliderOutputProps ,
type SliderTrackProps as AriaSliderTrackProps ,
} from "react-aria-components" ;
import { tv } from "tailwind-variants" ;
import { cn } from "@/lib/utils/classes" ;
import { ColorThumb } from "./color-thumb" ;
import { Label } from "./field" ;
const colorSliderStyles = tv ({
slots: {
root: "group/color-slider flex flex-col gap-2 orientation-horizontal:w-48 orientation-vertical:h-48 orientation-vertical:items-center" ,
output: "text-fg-muted text-sm" ,
track: [
"orientation-horizontal:w-48 rounded-md orientation-horizontal:h-6 orientation-vertical:w-6 orientation-vertical:h-48 disabled:!bg-bg-disabled" ,
"relative before:absolute before:inset-0 before:z-[-1] before:bg-[repeating-conic-gradient(#e6e6e6_0%_25%,_#fff_0%_50%)] before:bg-[length:16px_16px] before:bg-center before:content-[''] before:rounded-[inherit]" ,
],
},
});
interface ColorSliderProps extends ColorSliderRootProps {
showValueLabel ?: boolean ;
label ?: string ;
}
const ColorSlider = ({ label , channel , showValueLabel = true , ... props } : ColorSliderProps ) => {
return (
< ColorSliderRoot channel = { channel } { ... props } >
{ ( label || showValueLabel ) && (
< div className = { cn ( "flex items-center justify-between gap-2" , ! label && "justify-end" ) } >
{ label && < Label > { label } </ Label > }
{ showValueLabel && < ColorSliderOutput /> }
</ div >
) }
< ColorSliderTrack >
< ColorThumb />
</ ColorSliderTrack >
</ ColorSliderRoot >
);
};
type ColorSliderRootProps = AriaColorSliderProps ;
const ColorSliderRoot = ( props : ColorSliderRootProps ) => {
const { root } = colorSliderStyles ();
return (
< AriaColorSlider
{ ... props }
className = { composeRenderProps ( props . className , ( className ) => root ({ className })) }
/>
);
};
type ColorSliderTrackProps = AriaSliderTrackProps ;
const ColorSliderTrack = ( props : ColorSliderTrackProps ) => {
const { track } = colorSliderStyles ();
return (
< AriaSliderTrack
{ ... props }
style = { composeRenderProps ( props . style , ( style , { isDisabled }) => ({
... style ,
... ( isDisabled ? { background: "none" } : {}),
})) }
className = { composeRenderProps ( props . className , ( className ) => track ({ className })) }
/>
);
};
type ColorSliderOutputProps = AriaSliderOutputProps ;
const ColorSliderOutput = ( props : ColorSliderOutputProps ) => {
const { output } = colorSliderStyles ();
return (
< AriaSliderOutput
{ ... props }
className = { composeRenderProps ( props . className , ( className ) => output ({ className })) }
/>
);
};
export type {
ColorSliderProps ,
ColorSliderRootProps ,
ColorSliderTrackProps ,
ColorSliderOutputProps ,
};
export { ColorSlider , ColorSliderRoot , ColorSliderOutput , ColorSliderTrack , colorSliderStyles };
Update the import paths to match your project setup.
Usage
Use ColorSlider to allow users to adjust an individual channel of a color value.
Options
Label
Use the label prop to provide a label for the color slider.
< ColorSlider label = "Hue" channel = "hue" defaultValue = "hsl(200, 100%, 50%)" />
Orientation
orientation prop may be provided to specify the orientation of the color slider.
< ColorSlider orientation = "vertical" channel = "hue" defaultValue = "hsl(0, 100%, 50%)" />
Channel
channel prop may be provided to specify which color channel the color slider should display
< ColorSlider label = "Opacity" defaultValue = "#f00" channel = "alpha" />
Disabled
Use the isDisabled prop to disable the color slider.
< ColorSlider label = "Opacity" defaultValue = "#f00" channel = "alpha" isDisabled />
Unontrolled
Use the value and onChange props to control the value of the slider.
< ColorSlider channel = "hue" defaultValue = "hsl(0, 100%, 50%)" />
Controlled
Use the value and onChange props to control the value of the slider.
const [ value , setValue ] = React . useState ( parseColor ( "hsl(0, 100%, 50%)" ));
return < ColorSlider value = { value } onChange = { setValue } channel = "hue" />
Composition
If you need to customize things further, you can drop down to the composition level.
< ColorSliderRoot channel = "hue" defaultValue = "hsl(0, 100%, 50%)" >
< Label > Hue </ Label >
< ColorSliderOutput />
< ColorSliderTrack >
< ColorThumb />
</ ColorSliderTrack >
</ ColorSliderRoot >
API Reference
Prop Type Default Description channel *
'hue' | 'saturation' | 'brightness' | 'lightness' | 'red' | 'green' | 'blue' | 'alpha'
-
The color channel that the slider manipulates.
colorSpace
'rgb' | 'hsl' | 'hsb'
-
The color space that the slider operates in. The channel must be in this color space. If not provided, this defaults to the color space of the color or defaultColor value.
orientation
'horizontal' | 'vertical'
'horizontal'
The orientation of the Slider.
isDisabled
boolean
-
Whether the whole Slider is disabled.
value
T
-
The current value (controlled).
defaultValue
T
-
The default value (uncontrolled).
name
string
-
The name of the input element, used when submitting an HTML form.
children
ReactNode | ( values : ColorSliderRenderProps & { defaultChildren : ReactNode | undefined }) => ReactNode
-
The children of the component. A function may be provided to alter the children based on component state.
className
string
-
The CSS className for the element.
style
CSSProperties | ( values : ColorSliderRenderProps & { defaultStyle : CSSProperties }) => CSSProperties
-
The inline style for the element. A function may be provided to compute the style based on component state.
Event Type Description onChange
( value : Color ) => void
Handler that is called when the value changes, as the user drags.
onChangeEnd
( value : Color ) => void
Handler that is called when the user stops dragging.
Data attribute Description [ data - orientation = "horizontal | vertical" ]
The orientation of the color slider.
[ data - disabled ]
Whether the color slider is disabled.
Accessibility
Keyboard interactions
Key Description Tab
Places focus on the handle. If the handle is already in focus, moves focus to the next handle or next element in the page tab sequence.
Shift + Tab
Places focus on the previous handle or previous element in the page tab sequence.
ArrowUp ArrowDown ArrowLeft ArrowRight
Moves the handle up/down/left/right.