The picker unfolds from its swatch, the thumb grows while you drag and the colour cross-fades.
Part of Keyframery, the open-source animated components for UI SDK
The demo plays by itself. Point at it or press Tab to take over.
Installation
npx shadcn@latest add @keyframery/color-pickerThis adds components/keyframery/color-picker.tsx and its primitive, components/keyframery/primitives/color-picker.tsx, to your project, and installs @ui-sdk/react. The motion tokens are added to your global CSS the first time you install a component.
Usage
import {
ColorPicker,
ColorPickerArea,
ColorPickerAreaCanvas,
ColorPickerAreaThumb,
ColorPickerChannelInput,
ColorPickerContent,
ColorPickerEyeDropper,
ColorPickerInput,
ColorPickerPopover,
ColorPickerSlider,
ColorPickerSliderThumb,
ColorPickerSwatch,
ColorPickerSwatches,
ColorPickerSwatchItem,
ColorPickerTrigger,
} from "@/components/keyframery/color-picker"The parts and props are the same as UI SDK's Color Picker. For every prop, see the UI SDK color picker reference.
What moves
| Move | What you see |
|---|---|
| Unfold | The picker unfolds from its swatch. |
| Pop | A thumb grows while you hold it. |
| Fade | The chosen colour cross-fades everywhere it is used. |
Timing and springiness follow the motion style you set, so this component moves like the rest of your app.
Swap the import
Already using UI SDK's color picker with your own styles? Keep your markup and change one line. The primitive adds motion and nothing else.
// Before
import { … } from "@ui-sdk/react/color-picker"
// After: same parts, same props, now animated
import { … } from "@/components/keyframery/primitives/color-picker"Parts
The finished component exports 15 parts:
ColorPicker, ColorPickerArea, ColorPickerAreaCanvas, ColorPickerAreaThumb, ColorPickerChannelInput, ColorPickerContent, ColorPickerEyeDropper, ColorPickerInput, ColorPickerPopover, ColorPickerSlider, ColorPickerSliderThumb, ColorPickerSwatch, ColorPickerSwatches, ColorPickerSwatchItem, ColorPickerTrigger
Accessibility
Keyboard, focus and screen-reader behaviour come from UI SDK and are not changed. With reduced motion turned on, nothing in the color picker moves: changes happen at once or with a short fade. More in Accessibility.
Source
The code that lands in your project. It is yours to change.
"use client"
/* Styled color picker: the animated primitive plus the Keyframery look. */
import {
ColorPicker as ColorPickerRoot,
ColorPickerArea as ColorPickerAreaPart,
ColorPickerAreaCanvas as ColorPickerAreaCanvasPart,
ColorPickerAreaThumb as ColorPickerAreaThumbPart,
ColorPickerChannelInput as ColorPickerChannelInputPart,
ColorPickerContent as ColorPickerContentPart,
ColorPickerEyeDropper as ColorPickerEyeDropperPart,
ColorPickerInput as ColorPickerInputPart,
ColorPickerPopover as ColorPickerPopoverPart,
ColorPickerSlider as ColorPickerSliderPart,
ColorPickerSliderThumb as ColorPickerSliderThumbPart,
ColorPickerSwatch as ColorPickerSwatchPart,
ColorPickerSwatches as ColorPickerSwatchesPart,
ColorPickerSwatchItem as ColorPickerSwatchItemPart,
ColorPickerTrigger as ColorPickerTriggerPart,
createColorPickerController,
} from "@/components/keyframery/primitives/color-picker"
import * as React from "react"
import { cn } from "@/lib/utils"
/* A hairline inside anything filled with the colour, so white and black still have an edge. */
const colorEdge =
"shadow-[inset_0_0_0_1px_rgb(0_0_0/0.1)] dark:shadow-[inset_0_0_0_1px_rgb(255_255_255/0.12)]"
/* Transparent colours sit on a quiet checkerboard. */
const checker =
"[background:linear-gradient(var(--color-picker-color),var(--color-picker-color)),repeating-conic-gradient(var(--muted)_0_25%,var(--background)_0_50%)_0_0/8px_8px]"
/* Thumbs are small keys filled with the colour: a white collar, a hairline and a soft drop. */
const thumb = cn(
"absolute size-[18px] -translate-x-1/2 -translate-y-1/2 cursor-grab rounded-full bg-(--color-picker-color) outline-none",
"shadow-[0_0_0_2.5px_rgb(255_255_255),0_0_0_3.5px_rgb(0_0_0/0.12),0_2px_6px_rgb(0_0_0/0.3)]",
"dark:shadow-[0_0_0_2.5px_rgb(255_255_255),0_0_0_3.5px_rgb(0_0_0/0.5),0_2px_8px_rgb(0_0_0/0.6)]",
"active:cursor-grabbing",
"focus-visible:shadow-[0_0_0_2.5px_rgb(255_255_255),0_0_0_4.5px_var(--ring),0_2px_6px_rgb(0_0_0/0.3)]",
"data-disabled:cursor-not-allowed data-readonly:cursor-default",
)
const fieldLook =
"rounded-[9px] bg-background text-[13px] text-foreground outline-none " +
"[--field-line:rgb(0_0_0/0.09)] dark:[--field-line:rgb(255_255_255/0.1)] " +
"shadow-[0_0_0_1px_var(--field-line),0_1px_2px_rgb(0_0_0/0.05)] dark:shadow-[0_0_0_1px_var(--field-line)] " +
"transition-[box-shadow] duration-[calc(150ms*var(--kf-speed,1))] ease-house motion-reduce:transition-none! " +
"hover:[--field-line:rgb(0_0_0/0.16)] dark:hover:[--field-line:rgb(255_255_255/0.18)] " +
"focus-visible:ring-[3px] focus-visible:ring-ring/25 focus-visible:[--field-line:var(--ring)] dark:focus-visible:[--field-line:var(--ring)] " +
"aria-invalid:ring-destructive/25 aria-invalid:[--field-line:var(--destructive)] " +
"data-disabled:cursor-not-allowed data-disabled:bg-muted data-disabled:text-muted-foreground data-readonly:bg-muted/50"
function ColorPicker({ className, ...props }: React.ComponentProps<typeof ColorPickerRoot>) {
return (
<ColorPickerRoot
className={cn("flex flex-col gap-3 data-disabled:opacity-60", className)}
{...props}
/>
)
}
/* The area is a well painted with the colour's two channels. */
function ColorPickerArea({
className,
...props
}: React.ComponentProps<typeof ColorPickerAreaPart>) {
return (
<ColorPickerAreaPart
className={cn(
"relative isolate h-36 w-full shrink-0 touch-none rounded-[10px] [background:var(--color-area-background)] select-none",
"after:pointer-events-none after:absolute after:inset-0 after:rounded-[inherit] after:shadow-[inset_0_1px_2px_rgb(0_0_0/0.12),inset_0_0_0_1px_rgb(0_0_0/0.08)] dark:after:shadow-[inset_0_1px_2px_rgb(0_0_0/0.5),inset_0_0_0_1px_rgb(255_255_255/0.06)]",
"data-disabled:cursor-not-allowed",
className,
)}
{...props}
/>
)
}
function ColorPickerAreaCanvas({
className,
...props
}: React.ComponentProps<typeof ColorPickerAreaCanvasPart>) {
return (
<ColorPickerAreaCanvasPart
className={cn("absolute inset-0 -z-10 size-full rounded-[inherit]", className)}
{...props}
/>
)
}
function ColorPickerAreaThumb({
className,
...props
}: React.ComponentProps<typeof ColorPickerAreaThumbPart>) {
return <ColorPickerAreaThumbPart className={cn(thumb, "z-10", className)} {...props} />
}
/* A slider is a pill-shaped well drawn with its channel's gradient. */
function ColorPickerSlider({
className,
...props
}: React.ComponentProps<typeof ColorPickerSliderPart>) {
return (
<ColorPickerSliderPart
className={cn(
"relative isolate shrink-0 touch-none rounded-full select-none",
"data-[orientation=horizontal]:h-3 data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-36 data-[orientation=vertical]:w-3",
"[background:var(--color-slider-background)]",
"data-[channel=alpha]:[background:var(--color-slider-background),repeating-conic-gradient(var(--muted)_0_25%,var(--background)_0_50%)_0_0/8px_8px]",
"after:pointer-events-none after:absolute after:inset-0 after:rounded-[inherit] after:shadow-[inset_0_1px_2px_rgb(0_0_0/0.12),inset_0_0_0_1px_rgb(0_0_0/0.08)] dark:after:shadow-[inset_0_1px_2px_rgb(0_0_0/0.5),inset_0_0_0_1px_rgb(255_255_255/0.06)]",
"data-disabled:cursor-not-allowed",
className,
)}
{...props}
/>
)
}
function ColorPickerSliderThumb({
className,
...props
}: React.ComponentProps<typeof ColorPickerSliderThumbPart>) {
return (
<ColorPickerSliderThumbPart
className={cn(
thumb,
"z-10 data-[orientation=horizontal]:top-1/2 data-[orientation=vertical]:left-1/2",
className,
)}
{...props}
/>
)
}
/* The current colour, on a checkerboard so transparency reads. */
function ColorPickerSwatch({
className,
...props
}: React.ComponentProps<typeof ColorPickerSwatchPart>) {
return (
<ColorPickerSwatchPart
className={cn("relative size-8 shrink-0 rounded-[8px]", checker, colorEdge, className)}
{...props}
/>
)
}
function ColorPickerInput({
className,
...props
}: React.ComponentProps<typeof ColorPickerInputPart>) {
return (
<ColorPickerInputPart
className={cn(fieldLook, "h-8 w-full min-w-0 px-2.5 tabular-nums", className)}
{...props}
/>
)
}
function ColorPickerChannelInput({
className,
...props
}: React.ComponentProps<typeof ColorPickerChannelInputPart>) {
return (
<ColorPickerChannelInputPart
className={cn(fieldLook, "h-8 w-full min-w-0 px-2 text-center tabular-nums", className)}
{...props}
/>
)
}
function ColorPickerEyeDropper({
className,
children,
...props
}: React.ComponentProps<typeof ColorPickerEyeDropperPart>) {
return (
<ColorPickerEyeDropperPart
className={cn(
"grid size-8 shrink-0 cursor-pointer place-items-center rounded-[9px] bg-background text-foreground outline-none",
"shadow-[0_0_0_1px_rgb(0_0_0/0.06),0_1px_1px_rgb(0_0_0/0.05),0_3px_6px_-2px_rgb(0_0_0/0.1)]",
"dark:bg-accent dark:shadow-[0_0_0_1px_rgb(255_255_255/0.07),inset_0_1px_0_rgb(255_255_255/0.08),0_2px_6px_-1px_rgb(0_0_0/0.6)]",
"hover:bg-accent",
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-popover",
"data-disabled:pointer-events-none data-disabled:opacity-40 data-unsupported:hidden",
"[&_svg]:size-4",
className,
)}
{...props}
>
{children ?? (
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<path d="m9.5 4.5 2 2M3 13l1-3 6.25-6.25a1.4 1.4 0 0 1 2 2L6 12z" />
</svg>
)}
</ColorPickerEyeDropperPart>
)
}
function ColorPickerSwatches({
className,
...props
}: React.ComponentProps<typeof ColorPickerSwatchesPart>) {
return (
<ColorPickerSwatchesPart
className={cn("flex flex-wrap items-center gap-2 p-0.5", className)}
{...props}
/>
)
}
/* A preset. When it is the value, a ring in its own colour opens around it. */
function ColorPickerSwatchItem({
className,
...props
}: React.ComponentProps<typeof ColorPickerSwatchItemPart>) {
return (
<ColorPickerSwatchItemPart
className={cn(
"relative size-6 shrink-0 cursor-pointer rounded-full bg-(--color-picker-color) outline-none",
"shadow-[inset_0_0_0_1px_rgb(0_0_0/0.1),0_0_0_0_var(--popover),0_0_0_0_var(--color-picker-color)]",
"dark:shadow-[inset_0_0_0_1px_rgb(255_255_255/0.12),0_0_0_0_var(--popover),0_0_0_0_var(--color-picker-color)]",
"data-selected:shadow-[inset_0_0_0_1px_rgb(0_0_0/0.1),0_0_0_2px_var(--popover),0_0_0_3.5px_var(--color-picker-color)]",
"dark:data-selected:shadow-[inset_0_0_0_1px_rgb(255_255_255/0.12),0_0_0_2px_var(--popover),0_0_0_3.5px_var(--color-picker-color)]",
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-[5px] focus-visible:ring-offset-popover",
"data-disabled:pointer-events-none data-disabled:opacity-40 data-readonly:cursor-default",
className,
)}
{...props}
/>
)
}
/* With a popover inside the picker, a swatch button opens the parts in a sheet. */
function ColorPickerPopover(props: React.ComponentProps<typeof ColorPickerPopoverPart>) {
return <ColorPickerPopoverPart {...props} />
}
function ColorPickerTrigger({
className,
...props
}: React.ComponentProps<typeof ColorPickerTriggerPart>) {
return (
<ColorPickerTriggerPart
className={cn(
"inline-flex h-9 cursor-pointer items-center gap-2 rounded-[10px] bg-background pr-3 pl-1.5 text-[13px] leading-none font-medium text-foreground outline-none",
"shadow-[0_0_0_1px_rgb(0_0_0/0.06),0_1px_1px_rgb(0_0_0/0.05),0_3px_6px_-2px_rgb(0_0_0/0.1)]",
"dark:bg-accent dark:shadow-[0_0_0_1px_rgb(255_255_255/0.07),inset_0_1px_0_rgb(255_255_255/0.08),0_2px_6px_-1px_rgb(0_0_0/0.6)]",
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
"data-disabled:pointer-events-none data-disabled:opacity-50",
"*:data-[slot=color-picker-swatch]:size-6 *:data-[slot=color-picker-swatch]:rounded-[6px]",
"[&>svg]:size-4 [&>svg]:text-muted-foreground",
className,
)}
{...props}
/>
)
}
function ColorPickerContent({
className,
sideOffset = 8,
...props
}: React.ComponentProps<typeof ColorPickerContentPart>) {
return (
<ColorPickerContentPart
sideOffset={sideOffset}
className={cn(
"flex w-64 flex-col gap-3 rounded-[14px] bg-popover p-3 text-popover-foreground outline-none",
"shadow-[0_0_0_1px_rgb(0_0_0/0.06),0_2px_6px_-2px_rgb(0_0_0/0.08),0_12px_32px_-8px_rgb(0_0_0/0.18)]",
"dark:shadow-[0_0_0_1px_rgb(255_255_255/0.08),0_12px_32px_-8px_rgb(0_0_0/0.7)]",
className,
)}
{...props}
/>
)
}
export {
ColorPicker,
ColorPickerArea,
ColorPickerAreaCanvas,
ColorPickerAreaThumb,
ColorPickerChannelInput,
ColorPickerContent,
ColorPickerEyeDropper,
ColorPickerInput,
ColorPickerPopover,
ColorPickerSlider,
ColorPickerSliderThumb,
ColorPickerSwatch,
ColorPickerSwatches,
ColorPickerSwatchItem,
ColorPickerTrigger,
createColorPickerController,
}"use client"
/* Animated drop-in for `@ui-sdk/react/color-picker`: same parts, same props. Swap the import and keep your code.
This file adds motion only. Colours, radii, shadows and spacing stay yours.
- Area and slider thumbs: grow with a pop while grabbed, whether you press the thumb or the track.
- Swatch items: the chosen one pops (scale 0.7 to 1 on `ease-pop`), so its ring opens with a bounce.
- Extra helpers for a picker in a popup: ColorPickerPopover, ColorPickerTrigger (Press) and
ColorPickerContent (Unfold), built on `@ui-sdk/react/popover`. */
import {
ColorPickerArea as ColorPickerAreaPart,
ColorPickerAreaThumb as ColorPickerAreaThumbPart,
ColorPickerEyeDropper as ColorPickerEyeDropperPart,
ColorPickerSlider as ColorPickerSliderPart,
ColorPickerSliderThumb as ColorPickerSliderThumbPart,
ColorPickerSwatchItem as ColorPickerSwatchItemPart,
} from "@ui-sdk/react/color-picker"
import {
Popover as PopoverRoot,
PopoverContent as PopoverContentPart,
PopoverTrigger as PopoverTriggerPart,
} from "@ui-sdk/react/popover"
import * as React from "react"
import { cn } from "@/lib/utils"
export * from "@ui-sdk/react/color-picker"
const popKeyframes = "@keyframes kf-pop{from{scale:.7}to{scale:1}}"
const thumb =
"transition-[scale,box-shadow] duration-[calc(320ms*var(--kf-speed,1))] ease-pop hover:scale-110 active:scale-[1.35] motion-reduce:transition-none!"
function ColorPickerArea({ className, ...props }: React.ComponentProps<typeof ColorPickerAreaPart>) {
return <ColorPickerAreaPart className={cn("group/kf-track", className)} {...props} />
}
function ColorPickerSlider({ className, ...props }: React.ComponentProps<typeof ColorPickerSliderPart>) {
return <ColorPickerSliderPart className={cn("group/kf-track", className)} {...props} />
}
function ColorPickerAreaThumb({ className, ...props }: React.ComponentProps<typeof ColorPickerAreaThumbPart>) {
return (
<ColorPickerAreaThumbPart
className={cn(thumb, "group-active/kf-track:scale-[1.35]", className)}
{...props}
/>
)
}
function ColorPickerSliderThumb({ className, ...props }: React.ComponentProps<typeof ColorPickerSliderThumbPart>) {
return (
<ColorPickerSliderThumbPart
className={cn(thumb, "group-active/kf-track:scale-[1.35]", className)}
{...props}
/>
)
}
function ColorPickerSwatchItem({ className, ...props }: React.ComponentProps<typeof ColorPickerSwatchItemPart>) {
return (
<>
<style href="kf-pop" precedence="default">
{popKeyframes}
</style>
<ColorPickerSwatchItemPart
className={cn(
"transition-[scale,box-shadow] duration-[calc(260ms*var(--kf-speed,1))] ease-pop hover:scale-110 active:scale-90",
"data-selected:animate-[kf-pop_calc(460ms*var(--kf-speed,1))_var(--ease-pop)] data-selected:hover:scale-100",
"motion-reduce:transition-none! motion-reduce:animate-none!",
className,
)}
{...props}
/>
</>
)
}
function ColorPickerEyeDropper({ className, ...props }: React.ComponentProps<typeof ColorPickerEyeDropperPart>) {
return (
<ColorPickerEyeDropperPart
className={cn(
"transition-[scale,background-color] duration-[calc(200ms*var(--kf-speed,1))] ease-pop active:scale-[0.96] motion-reduce:transition-none!",
className,
)}
{...props}
/>
)
}
/* Helpers for a picker that opens from a swatch. Put the popover inside the ColorPicker. */
function ColorPickerPopover(props: React.ComponentProps<typeof PopoverRoot>) {
return <PopoverRoot {...props} />
}
function ColorPickerTrigger({ className, ...props }: React.ComponentProps<typeof PopoverTriggerPart>) {
return (
<PopoverTriggerPart
className={cn(
"transition-[scale,background-color] duration-[calc(200ms*var(--kf-speed,1))] ease-pop active:scale-[0.96] motion-reduce:transition-none!",
"[&>svg]:transition-[rotate] [&>svg]:duration-[calc(420ms*var(--kf-speed,1))] [&>svg]:ease-spring data-open:[&>svg]:rotate-180 motion-reduce:[&>svg]:transition-none",
className,
)}
{...props}
/>
)
}
/* Unfold: from the swatch, scale 0.86 with a 6px blur and 8px toward it, on a spring. */
function ColorPickerContent({ className, ...props }: React.ComponentProps<typeof PopoverContentPart>) {
return (
<PopoverContentPart
className={cn(
"origin-(--transform-origin) transition-[opacity,scale,translate,filter] duration-[calc(380ms*var(--kf-speed,1))] ease-spring",
"data-starting-style:scale-[0.86] data-starting-style:opacity-0 data-starting-style:blur-[6px]",
"data-[side=bottom]:data-starting-style:-translate-y-2 data-[side=top]:data-starting-style:translate-y-2",
"data-[side=left]:data-starting-style:translate-x-2 data-[side=right]:data-starting-style:-translate-x-2",
"data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-ending-style:blur-[2px] data-ending-style:duration-[calc(150ms*var(--kf-speed,1))] data-ending-style:ease-house",
"motion-reduce:transition-none!",
className,
)}
{...props}
/>
)
}
export {
ColorPickerArea,
ColorPickerAreaThumb,
ColorPickerContent,
ColorPickerEyeDropper,
ColorPickerPopover,
ColorPickerSlider,
ColorPickerSliderThumb,
ColorPickerSwatchItem,
ColorPickerTrigger,
}Questions
How does the animated Color Picker for UI SDK move?
Unfold: The picker unfolds from its swatch. Pop: A thumb grows while you hold it. Fade: The chosen colour cross-fades everywhere it is used.
Do I need Motion or Framer Motion for the animated Color Picker?
No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.
Can I keep my own styling and only take the motion?
Yes. Import from @/components/keyframery/primitives/color-picker instead. It has the same parts and props as @ui-sdk/react/color-picker and adds motion only.
What happens to the Color Picker when someone turns on reduced motion?
Nothing moves. State changes happen at once, or with a short fade.