---
title: "Color Picker · Base UI — StyleX Blocks"
description: "Swatch trigger and checkerboard swatch grid with a selection indicator, from 2xs to 2xl."
url: "https://stylexblocks.com/components/base-ui/color-picker/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Color Picker

Swatch trigger and checkerboard swatch grid with a selection indicator, from 2xs to 2xl.

- Behavior engine: Base UI
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/color-picker.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/color-picker.md)
- Category: Form
- Variants: outline, subtle
- Sizes: 2xs, xs, sm, md, lg, xl, 2xl
- HTML page: https://stylexblocks.com/components/base-ui/color-picker/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style base-ui
```

Then add the item as source you own:

```sh
npx stylexblocks add color-picker
```

Equivalent: `npx shadcn add @stylexblocks/color-picker`. Registry item JSON: https://stylexblocks.com/r/base-ui/color-picker.json

## Usage

```tsx
import { ColorPicker } from "#components/ui/base-ui/color-picker.tsx";

const swatches = ["#ef4444", "#22c55e", "#6366f1", "#a855f7"];

export function Example() {
  return (
    // Base UI has no color-picker machine, so Root owns the color value
    // (value/defaultValue/onValueChange); the selected SwatchTrigger rings
    // its Swatch and reveals its SwatchIndicator, and parts compose through
    // render
    <ColorPicker.Root defaultValue="#6366f1" variant="outline" size="md">
      <ColorPicker.Label>Brand color</ColorPicker.Label>
      <ColorPicker.Control>
        <ColorPicker.Trigger>
          <ColorPicker.Swatch />
          <ColorPicker.ValueText />
        </ColorPicker.Trigger>
      </ColorPicker.Control>
      <ColorPicker.SwatchGroup>
        {swatches.map((hex) => (
          <ColorPicker.SwatchTrigger key={hex} value={hex}>
            <ColorPicker.Swatch value={hex}>
              <ColorPicker.SwatchIndicator />
            </ColorPicker.Swatch>
          </ColorPicker.SwatchTrigger>
        ))}
      </ColorPicker.SwatchGroup>
    </ColorPicker.Root>
  );
}
```

## Source

`components/ui/base-ui/color-picker.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { createContext, useContext, useMemo, useState } from "react";
import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import { createSlotRecipeContext } from "#lib/base-ui/create-slot-recipe-context.tsx";
import {
  defineSlotRecipe,
  resolveSlotStyles,
  type SlotRecipeVariants,
} from "#lib/recipe-props.ts";
import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { colors } from '@stylexblocks/ui/tokens/colors.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { semanticShadows } from '@stylexblocks/ui/semantic-tokens/shadows.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
import { zIndices } from '@stylexblocks/ui/tokens/z-indices.stylex.ts'

const colorPickerRoot = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'column',
    gap: spacing._1_5,
  },
})

const colorPickerLabel = stylex.create({
  base: {
    color: semanticColors.fg,
    fontWeight: fontWeights.medium,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
  },
})

const colorPickerValueText = stylex.create({
  base: {
    textAlign: 'start',
  },
})

const colorPickerControl = stylex.create({
  base: {
    display: 'flex',
    alignItems: 'center',
    flexDirection: 'row',
    gap: spacing._2,
    position: 'relative',
  },
})

const colorPickerSwatchTrigger = stylex.create({
  base: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
  },
})

const colorPickerTrigger = stylex.create({
  base: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    flexDirection: 'row',
    flexShrink: 0,
    gap: spacing._2,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    minHeight: 'var(--input-height)',
    minWidth: 'var(--input-height)',
    paddingInline: spacing._1,
    borderRadius: semanticRadii.controlMd,
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
    '--focus-color': colorPalette.focusRing,
  },
})

const colorPickerTriggerSizes = stylex.create({
  '2xs': {
    '--input-height': sizes._5,
  },
  xs: {
    '--input-height': sizes._6,
  },
  sm: {
    '--input-height': sizes._7,
  },
  md: {
    '--input-height': sizes._8,
  },
  lg: {
    '--input-height': sizes._10,
  },
  xl: {
    '--input-height': sizes._12,
  },
  '2xl': {
    '--input-height': sizes._16,
  },
})

const colorPickerTriggerVariants = stylex.create({
  outline: {
    borderWidth: '1px',
  },
  subtle: {
    borderWidth: '1px',
    borderColor: 'transparent',
    backgroundColor: semanticColors.bgMuted,
  },
})

const colorPickerContent = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'column',
    width: sizes._64,
    padding: spacing._4,
    gap: spacing._3,
    '--color-picker-z-index': zIndices.layer3,
    zIndex: 'calc(var(--color-picker-z-index) + var(--layer-index, 0))',
  },
})

const colorPickerArea = stylex.create({
  base: {
    height: '180px',
    borderRadius: semanticRadii.controlMd,
    overflow: 'hidden',
  },
})

const colorPickerAreaSizes = stylex.create({
  '2xs': {
    '--thumb-size': sizes._3,
  },
  xs: {
    '--thumb-size': sizes._3_5,
  },
  sm: {
    '--thumb-size': sizes._3_5,
  },
  md: {
    '--thumb-size': sizes._3_5,
  },
  lg: {
    '--thumb-size': sizes._3_5,
  },
  xl: {
    '--thumb-size': sizes._3_5,
  },
  '2xl': {
    '--thumb-size': sizes._3_5,
  },
})

const colorPickerAreaThumb = stylex.create({
  base: {
    borderRadius: radii.full,
    height: 'var(--thumb-size)',
    width: 'var(--thumb-size)',
    borderWidth: '2px',
    borderColor: colors.white,
    boxShadow: semanticShadows.sm,
    '--focus-ring-color': colors.white,
  },
})

const colorPickerAreaBackground = stylex.create({
  base: {
    height: sizes.full,
  },
})

const colorPickerChannelSlider = stylex.create({
  base: {
    borderRadius: semanticRadii.controlMd,
    flex: 1,
  },
})

const colorPickerChannelSliderSizes = stylex.create({
  '2xs': {
    '--slider-height': sizes._3,
    '--thumb-size': sizes._3,
  },
  xs: {
    '--slider-height': sizes._3_5,
    '--thumb-size': sizes._3_5,
  },
  sm: {
    '--slider-height': sizes._3_5,
    '--thumb-size': sizes._3_5,
  },
  md: {
    '--slider-height': sizes._3_5,
    '--thumb-size': sizes._3_5,
  },
  lg: {
    '--slider-height': sizes._3_5,
    '--thumb-size': sizes._3_5,
  },
  xl: {
    '--slider-height': sizes._3_5,
    '--thumb-size': sizes._3_5,
  },
  '2xl': {
    '--slider-height': sizes._3_5,
    '--thumb-size': sizes._3_5,
  },
})

const colorPickerChannelSliderTrack = stylex.create({
  base: {
    height: 'var(--slider-height)',
    borderRadius: 'inherit',
    boxShadow: 'inset 0 0 0 1px rgba(0,0,0,0.1)',
  },
})

const colorPickerChannelText = stylex.create({
  base: {
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
    color: semanticColors.fgMuted,
    fontWeight: fontWeights.medium,
    textTransform: 'capitalize',
  },
})

const colorPickerSwatchGroup = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'row',
    flexWrap: 'wrap',
    gap: spacing._2,
  },
})

const colorPickerSwatch = stylex.create({
  base: {
    width: 'var(--swatch-size)',
    height: 'var(--swatch-size)',
    boxShadow: 'inset 0 0 0 1px rgba(0, 0, 0, 0.1)',
    '--checker-size': '8px',
    '--checker-bg': semanticColors.bg,
    '--checker-fg': semanticColors.bgEmphasized,
    // Color over a checkerboard. Longhands: position/size in backgroundImage
    // (shorthand syntax) made the whole declaration invalid, so swatches
    // rendered blank.
    backgroundImage:
      'linear-gradient(var(--color), var(--color)), repeating-conic-gradient(var(--checker-fg) 0%, var(--checker-fg) 25%, var(--checker-bg) 0%, var(--checker-bg) 50%)',
    backgroundPosition: '0 0, 0% 50%',
    backgroundSize: 'auto, var(--checker-size) var(--checker-size)',
    display: 'inline-flex',
    alignItems: 'center',
    justifyContent: 'center',
    flexShrink: 0,
    borderRadius: semanticRadii.controlSm,
  },
})

const colorPickerSwatchSizes = stylex.create({
  '2xs': {
    '--swatch-size': sizes._4_5,
  },
  xs: {
    '--swatch-size': sizes._5,
  },
  sm: {
    '--swatch-size': sizes._6,
  },
  md: {
    '--swatch-size': sizes._7,
  },
  lg: {
    '--swatch-size': sizes._7,
  },
  xl: {
    '--swatch-size': sizes._8,
  },
  '2xl': {
    '--swatch-size': sizes._10,
  },
})

const colorPickerSwatchIndicator = stylex.create({
  base: {
    color: colors.white,
    borderRadius: radii.full,
  },
})

const colorPickerChannelSliderThumb = stylex.create({
  base: {
    borderRadius: radii.full,
    height: 'var(--thumb-size)',
    width: 'var(--thumb-size)',
    borderWidth: '2px',
    borderColor: colors.white,
    boxShadow: semanticShadows.sm,
    transform: 'translate(-50%, -50%)',
    outlineWidth: {
      default: null,
      ':focus-visible': '1px',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '2px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
    focusRingOffset: '1px',
  },
})

const colorPickerChannelInput = stylex.create({
  base: {
    width: '100%',
    minWidth: 'var(--input-height)',
    outline: 'none',
    position: 'relative',
    appearance: 'none',
    textAlign: 'start',
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
    cursor: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 'not-allowed',
    },
    height: 'var(--input-height)',
    '--focus-color': colorPalette.focusRing,
    '--error-color': semanticColors.borderError,
    '--focus-ring-color': {
      default: null,
      ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
    },
    borderColor: {
      default: null,
      ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
    },
  },
})

const colorPickerChannelInputSizes = stylex.create({
  '2xs': {
    borderRadius: semanticRadii.control,
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
    paddingInline: spacing._2,
    '--input-height': sizes.controlXs,
  },
  xs: {
    borderRadius: semanticRadii.control,
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
    paddingInline: spacing._2,
    '--input-height': sizes.controlXs,
  },
  sm: {
    borderRadius: semanticRadii.control,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    paddingInline: spacing._2_5,
    '--input-height': sizes.controlSm,
  },
  md: {
    borderRadius: semanticRadii.control,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    paddingInline: spacing._3,
    '--input-height': sizes.controlMd,
  },
  lg: {
    borderRadius: semanticRadii.control,
    fontSize: fontSizes.md,
    lineHeight: lineHeights.md,
    paddingInline: spacing._4_5,
    '--input-height': sizes.controlLg,
  },
  xl: {
    borderRadius: semanticRadii.control,
    fontSize: fontSizes.md,
    lineHeight: lineHeights.md,
    paddingInline: spacing._6,
    '--input-height': sizes.controlXl,
  },
  '2xl': {
    borderRadius: semanticRadii.control,
    fontSize: fontSizes.md,
    lineHeight: lineHeights.md,
    paddingInline: spacing._6,
    '--input-height': sizes.controlXl,
  },
})

const colorPickerChannelInputVariants = stylex.create({
  outline: {
    backgroundColor: semanticColors.bg,
    borderWidth: '1px',
    borderColor: {
      default: semanticColors.border,
      ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
      ':hover': {
        default: semanticColors.borderEmphasized,
        ':focus-visible': 'var(--focus-ring-color)',
      },
    },
    outlineWidth: {
      default: null,
      ':focus-visible': 'var(--focus-ring-width, 0)',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '0px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
    focusRingWidth: 0,
  },
  subtle: {
    borderWidth: '1px',
    borderColor: {
      default: 'transparent',
      ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
    },
    backgroundColor: semanticColors.bgMuted,
    outlineWidth: {
      default: null,
      ':focus-visible': 'var(--focus-ring-width, 0)',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '0px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
  },
})

const colorPickerFormatSelect = stylex.create({
  base: {
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
    textTransform: 'uppercase',
    borderWidth: '1px',
    minHeight: sizes._6,
    outlineWidth: {
      default: null,
      ':focus': 'var(--focus-ring-width, 0)',
    },
    outlineOffset: {
      default: null,
      ':focus': '0px',
    },
    outlineStyle: {
      default: null,
      ':focus': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus': colorPalette.focusRing,
    },
    borderRadius: semanticRadii.controlMd,
  },
})

const colorPickerTransparencyGrid = stylex.create({
  base: {
    borderRadius: semanticRadii.controlMd,
  },
})

const colorPickerView = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'column',
    gap: spacing._2,
  },
})

export type ColorPickerSize = keyof typeof colorPickerTriggerSizes

export type ColorPickerVariant = keyof typeof colorPickerTriggerVariants

const colorPickerRecipe = defineSlotRecipe({
  slots: {
    root: { styles: colorPickerRoot },
    label: { styles: colorPickerLabel },
    valueText: { styles: colorPickerValueText },
    control: { styles: colorPickerControl },
    swatchTrigger: { styles: colorPickerSwatchTrigger },
    trigger: {
      styles: colorPickerTrigger,
      sizes: colorPickerTriggerSizes,
      variants: colorPickerTriggerVariants,
    },
    swatchGroup: { styles: colorPickerSwatchGroup },
    swatch: { styles: colorPickerSwatch, sizes: colorPickerSwatchSizes },
    swatchIndicator: { styles: colorPickerSwatchIndicator },
  },
  defaultVariants: { size: "md", variant: "outline" },
});

const chrome = stylex.create({
  btnReset: {
    appearance: "none",
    backgroundColor: "transparent",
    borderWidth: 0,
    borderStyle: "none",
    padding: 0,
    margin: 0,
    fontFamily: "inherit",
    fontSize: "inherit",
    color: "inherit",
    cursor: "pointer",
    textAlign: "inherit",
  },
  swatchRing: {
    outlineWidth: { default: null, ":is([data-checked])": "2px" },
    outlineStyle: { default: null, ":is([data-checked])": "solid" },
    outlineColor: { default: null, ":is([data-checked])": semanticColors.fg },
    outlineOffset: { default: null, ":is([data-checked])": "1px" },
  },
  swatchPaint: {
    backgroundImage:
      "linear-gradient(var(--color), var(--color)), repeating-conic-gradient(var(--checker-fg) 0% 25%, var(--checker-bg) 0% 50%)",
    backgroundSize: "100% 100%, var(--checker-size) var(--checker-size)",
    backgroundPosition: "0 0, 0% 50%",
  },
});

function CheckIcon(props: React.ComponentProps<"svg">) {
  return (
    <svg width="12" height="12" viewBox="0 0 16 16" fill="none" aria-hidden="true" {...props}>
      <path d="m3.5 8.5 3 3 6-7" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );
}

interface ColorPickerContextValue {
  value: string;
  select: (value: string) => void;
}

const ColorPickerContext = createContext<ColorPickerContextValue>({
  value: "#000000",
  select: () => {},
});

const SwatchTriggerContext = createContext<{ value: string; selected: boolean }>({
  value: "",
  selected: false,
});

const { withProvider, withContext } = createSlotRecipeContext(colorPickerRecipe);

export type ColorPickerVariantProps = Partial<SlotRecipeVariants<typeof colorPickerRecipe>>;

function RootNode({
  render,
  value,
  defaultValue = "#000000",
  onValueChange,
  ...rest
}: useRender.ComponentProps<"div"> & {
  value?: string;
  defaultValue?: string;
  onValueChange?: (value: string) => void;
}) {
  const [inner, setInner] = useState(defaultValue);
  const current = value !== undefined ? value : inner;
  const ctx = useMemo<ColorPickerContextValue>(
    () => ({
      value: current,
      select: (next: string) => {
        setInner(next);
        onValueChange?.(next);
      },
    }),
    [current, onValueChange],
  );
  const element = useRender({
    defaultTagName: "div",
    render: render as useRender.RenderProp,
    props: rest,
  });
  return <ColorPickerContext.Provider value={ctx}>{element}</ColorPickerContext.Provider>;
}

function TriggerNode({ render, ...rest }: useRender.ComponentProps<"button">) {
  return useRender({
    defaultTagName: "button",
    render: render as useRender.RenderProp,
    props: mergeProps({ type: "button" as const }, rest),
  });
}

function ValueTextNode({
  render,
  children,
  ...rest
}: useRender.ComponentProps<"span">) {
  const ctx = useContext(ColorPickerContext);
  return useRender({
    defaultTagName: "span",
    render: render as useRender.RenderProp,
    props: mergeProps({ children: children ?? ctx.value }, rest),
  });
}

function SwatchNode({
  render,
  value,
  style,
  ...rest
}: useRender.ComponentProps<"span"> & { value?: string }) {
  const ctx = useContext(ColorPickerContext);
  const trigger = useContext(SwatchTriggerContext);
  const color = value ?? (trigger.value || ctx.value);
  return useRender({
    defaultTagName: "span",
    render: render as useRender.RenderProp,
    props: mergeProps(
      {
        "data-checked": trigger.selected ? "" : undefined,
        style: { ...(style as React.CSSProperties), "--color": color } as React.CSSProperties,
      },
      rest,
    ),
  });
}

function SwatchTriggerNode({
  render,
  value,
  onClick,
  ...rest
}: useRender.ComponentProps<"button"> & { value: string }) {
  const ctx = useContext(ColorPickerContext);
  const selected = ctx.value.toLowerCase() === value.toLowerCase();
  const triggerCtx = useMemo(() => ({ value, selected }), [value, selected]);
  const element = useRender({
    defaultTagName: "button",
    render: render as useRender.RenderProp,
    props: mergeProps(
      {
        type: "button" as const,
        "aria-pressed": selected,
        "data-checked": selected ? "" : undefined,
        onClick: (event: React.MouseEvent<HTMLButtonElement>) => {
          (onClick as ((event: React.MouseEvent<HTMLButtonElement>) => void) | undefined)?.(event);
          ctx.select(value);
        },
      },
      rest,
    ),
  });
  return (
    <SwatchTriggerContext.Provider value={triggerCtx}>{element}</SwatchTriggerContext.Provider>
  );
}

function SwatchIndicatorNode({
  children,
  ...props
}: React.ComponentProps<"span">) {
  const { selected } = useContext(SwatchTriggerContext);
  if (!selected) return null;
  return <span {...props}>{children ?? <CheckIcon />}</span>;
}

/**
 * ColorPicker — swatch-based color selection. Base UI ships no color-picker
 * machine, so Root owns the color value (controlled via
 * `value`/`onValueChange` or uncontrolled via `defaultValue`).
 * `<ColorPicker.Root variant size>` resolves the recipe once; every part
 * inherits it through context. The selected SwatchTrigger stamps
 * `data-checked` so Swatch rings via `:is([data-checked])` and
 * SwatchIndicator reveals. Composition per part via `render`.
 */
export const ColorPicker = {
  Root: withProvider(RootNode, "root", "ColorPickerRoot"),
  Label: withContext("span", "label", "ColorPickerLabel"),
  Control: withContext("div", "control", "ColorPickerControl"),
  Trigger: withContext(
    TriggerNode,
    (v) => [chrome.btnReset, ...resolveSlotStyles(colorPickerRecipe.slots.trigger, v)],
    "ColorPickerTrigger",
  ),
  ValueText: withContext(ValueTextNode, "valueText", "ColorPickerValueText"),
  Swatch: withContext(
    SwatchNode,
    (v) => [
      ...resolveSlotStyles(colorPickerRecipe.slots.swatch, v),
      chrome.swatchPaint,
      chrome.swatchRing,
    ],
    "ColorPickerSwatch",
  ),
  SwatchGroup: withContext("div", "swatchGroup", "ColorPickerSwatchGroup"),
  SwatchTrigger: withContext(
    SwatchTriggerNode,
    (v) => [chrome.btnReset, ...resolveSlotStyles(colorPickerRecipe.slots.swatchTrigger, v)],
    "ColorPickerSwatchTrigger",
  ),
  SwatchIndicator: withContext(
    SwatchIndicatorNode,
    "swatchIndicator",
    "ColorPickerSwatchIndicator",
  ),
};

export type ColorPickerRootProps = React.ComponentProps<typeof ColorPicker.Root>;
export type ColorPickerSwatchProps = React.ComponentProps<typeof ColorPicker.Swatch>;
export type ColorPickerSwatchTriggerProps = React.ComponentProps<typeof ColorPicker.SwatchTrigger>;
```
