---
title: "Color Picker · UI SDK — StyleX Blocks"
description: "Swatch trigger and checkerboard swatch grid with a selection indicator, from 2xs to 2xl."
url: "https://stylexblocks.com/components/ui-sdk/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: UI SDK
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/color-picker.md), [Base UI](https://stylexblocks.com/components/base-ui/color-picker.md)
- Category: Form
- Variants: outline, subtle
- Sizes: 2xs, xs, sm, md, lg, xl, 2xl
- HTML page: https://stylexblocks.com/components/ui-sdk/color-picker/

## Install

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

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

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/ui-sdk/color-picker.json

## Usage

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

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

export function Example() {
  return (
    // UI SDK's color picker: CSS color strings; SwatchGroup is one tab stop
    // with arrow keys between presets, and the selected SwatchTrigger rings
    // its Swatch and reveals its SwatchIndicator
    <ColorPicker.Root defaultValue="#6366f1" format="hex" variant="outline" size="md">
      <ColorPicker.Label>Brand color</ColorPicker.Label>
      <ColorPicker.Control>
        <ColorPicker.Trigger>
          <ColorPicker.ValueSwatch />
          <ColorPicker.ValueText />
        </ColorPicker.Trigger>
      </ColorPicker.Control>
      <ColorPicker.SwatchGroup aria-label="Presets">
        {swatches.map((hex) => (
          <ColorPicker.SwatchTrigger key={hex} value={hex}>
            <ColorPicker.Swatch>
              <ColorPicker.SwatchIndicator />
            </ColorPicker.Swatch>
          </ColorPicker.SwatchTrigger>
        ))}
      </ColorPicker.SwatchGroup>
      {/* Trigger opens Content in a top-layer popover */}
      <ColorPicker.Content>
        <ColorPicker.Area>
          <ColorPicker.AreaThumb />
        </ColorPicker.Area>
        <ColorPicker.ChannelSlider channel="hue">
          <ColorPicker.ChannelSliderTrack />
          <ColorPicker.ChannelSliderThumb />
        </ColorPicker.ChannelSlider>
        <ColorPicker.Input aria-label="Hex" />
      </ColorPicker.Content>
    </ColorPicker.Root>
  );
}

// controlled: <ColorPicker.Root value={color} onValueChange={(value) => setColor(value)}>
// save once a drag ends: <ColorPicker.Root onValueCommit={(value) => save(value)}>
```

## Source

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

```tsx
import * as stylex from "@stylexjs/stylex";
import { createContext, useContext, useId } from "react";
import {
  ColorPicker as UiSdkColorPicker,
  ColorPickerArea,
  ColorPickerAreaThumb,
  ColorPickerChannelInput,
  ColorPickerEyeDropper,
  ColorPickerInput,
  ColorPickerSlider,
  ColorPickerSliderThumb,
  ColorPickerSwatch,
  ColorPickerSwatches,
  ColorPickerSwatchItem,
  type ColorPickerState,
  type ColorPickerSwatchItemState,
} from "@ui-sdk/react/color-picker";
import { Popover, PopoverContent, PopoverTrigger } from "@ui-sdk/react/popover";
import { mergeProps, renderElement, type PartProps } from "@ui-sdk/react/primitive";
import { createSlotRecipeContext } from "#lib/ui-sdk/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,
    },
    content: { styles: colorPickerContent },
    area: { styles: colorPickerArea, sizes: colorPickerAreaSizes },
    areaThumb: { styles: colorPickerAreaThumb },
    areaBackground: { styles: colorPickerAreaBackground },
    channelSlider: {
      styles: colorPickerChannelSlider,
      sizes: colorPickerChannelSliderSizes,
    },
    channelSliderTrack: { styles: colorPickerChannelSliderTrack },
    swatchGroup: { styles: colorPickerSwatchGroup },
    swatch: { styles: colorPickerSwatch, sizes: colorPickerSwatchSizes },
    swatchIndicator: { styles: colorPickerSwatchIndicator },
    channelSliderThumb: { styles: colorPickerChannelSliderThumb },
    channelInput: {
      styles: colorPickerChannelInput,
      sizes: colorPickerChannelInputSizes,
      variants: colorPickerChannelInputVariants,
    },
    transparencyGrid: { styles: colorPickerTransparencyGrid },
    view: { styles: colorPickerView },
  },
  defaultVariants: { size: "md", variant: "outline" },
});

// chrome the generated recipe leaves to the composition layer: unstyled
// trigger buttons, the popup panel surface (a top-layer popover that keeps
// the browser's own border), and the paint UI SDK leaves to CSS variables:
// every part writes `--color-picker-color`, the area
// `--color-area-background`, sliders `--color-slider-background`; thumbs
// get only their absolute position inline.
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",
  },
  panel: {
    display: {
      default: null,
      ":popover-open": "flex",
    },
    borderWidth: 0,
    borderStyle: "none",
    margin: 0,
    color: "inherit",
    backgroundColor: semanticColors.bgPanel,
    borderRadius: semanticRadii.panel,
    boxShadow: semanticShadows.overlay,
  },
  // the swatch reads its color from `--color`: the part's own color when
  // no `value` is given (a swatch item's color, or the picker's value)
  swatchColor: { "--color": "var(--color-picker-color)" },
  // the recipe's background-image carries `/ size !important`, which is
  // background-shorthand syntax and drops the declaration; paint the color
  // over the checkerboard with longhands instead
  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%",
  },
  // the selected SwatchTrigger rings its Swatch (Ark/Base: data-checked)
  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" },
  },
  area: {
    position: "relative",
    touchAction: "none",
    backgroundImage: "var(--color-area-background)",
  },
  areaThumb: {
    borderStyle: "solid",
    backgroundColor: "var(--color-picker-color)",
    translate: "-50% -50%",
    outline: "none",
  },
  slider: {
    position: "relative",
    display: "flex",
    alignItems: "center",
    touchAction: "none",
  },
  sliderTrack: {
    flexGrow: 1,
    backgroundImage: "var(--color-slider-background)",
  },
  sliderThumb: {
    borderStyle: "solid",
    backgroundColor: "var(--color-picker-color)",
    top: "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>
  );
}

const StateContext = createContext<ColorPickerState | null>(null);
const LabelIdContext = createContext<string | undefined>(undefined);
const SwatchSelectedContext = createContext(false);

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

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

function RootNode({ children, ...rest }: React.ComponentProps<typeof UiSdkColorPicker>) {
  const labelId = useId();
  return (
    <LabelIdContext.Provider value={labelId}>
      <UiSdkColorPicker aria-labelledby={labelId} {...rest}>
        {(state: ColorPickerState) => (
          <StateContext.Provider value={state}>
            {/* Trigger and Content share one popover */}
            <Popover>
              {typeof children === "function"
                ? (children as (state: ColorPickerState) => React.ReactNode)(state)
                : children}
            </Popover>
          </StateContext.Provider>
        )}
      </UiSdkColorPicker>
    </LabelIdContext.Provider>
  );
}

function LabelNode(props: React.ComponentProps<"span">) {
  const labelId = useContext(LabelIdContext);
  return <span id={labelId} {...props} />;
}

function ValueTextNode({ render, children, ...rest }: PartProps<"span">) {
  const state = useContext(StateContext);
  return renderElement(
    "span",
    mergeProps({ children: children ?? state?.value }, rest),
    render as never,
    {},
  );
}

/** A color chip: `value`, or the part's own color (a SwatchTrigger's, else the picker's). */
function SwatchNode({ render, value, style, ...rest }: PartProps<"span"> & { value?: string }) {
  const selected = useContext(SwatchSelectedContext);
  return renderElement(
    "span",
    mergeProps(
      {
        "data-checked": selected ? "" : undefined,
        style: value
          ? ({ ...(style as React.CSSProperties), "--color": value } as React.CSSProperties)
          : (style as React.CSSProperties),
      },
      rest,
    ),
    render as never,
    {},
  );
}

/** A preset: picks its `value` on press; the group is one tab stop with arrow keys between. */
function SwatchTriggerNode({
  children,
  ...rest
}: React.ComponentProps<typeof ColorPickerSwatchItem>) {
  return (
    <ColorPickerSwatchItem {...rest}>
      {(state: ColorPickerSwatchItemState) => (
        <SwatchSelectedContext.Provider value={state.selected}>
          {typeof children === "function"
            ? (children as (state: ColorPickerSwatchItemState) => React.ReactNode)(state)
            : children}
        </SwatchSelectedContext.Provider>
      )}
    </ColorPickerSwatchItem>
  );
}

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

function ContentNode(props: React.ComponentProps<typeof PopoverContent>) {
  return <PopoverContent sideOffset={4} {...props} />;
}

const BackgroundLayer = (props: PartProps<"div">) =>
  renderElement("div", props, props.render as never, {});

/**
 * ColorPicker — UI SDK's color-picker primitive styled with the StyleX slot
 * recipe. Values are CSS color strings (`"#6366f1"`, or `format="oklch"`).
 * `<ColorPicker.Root variant size>` resolves the recipe once; every part
 * inherits it through context. SwatchGroup + SwatchTrigger are UI SDK's
 * presets (selected by color, however either is written): the selected
 * trigger rings its Swatch and reveals its SwatchIndicator. Trigger opens
 * Content in a top-layer popover holding the Area, ChannelSliders and
 * inputs. There is no HiddenInput part: Root submits the value under `name`.
 */
export const ColorPicker = {
  Root: withProvider(RootNode, "root", "ColorPickerRoot"),
  Label: withContext(LabelNode, "label", "ColorPickerLabel"),
  Control: withContext("div", "control", "ColorPickerControl"),
  Trigger: withContext(
    PopoverTrigger,
    (v) => [chrome.btnReset, ...resolveSlotStyles(colorPickerRecipe.slots.trigger, v)],
    "ColorPickerTrigger",
  ),
  ValueText: withContext(ValueTextNode, "valueText", "ColorPickerValueText"),
  /** The picker's current color, named for screen readers. */
  ValueSwatch: withContext(
    ColorPickerSwatch,
    (v) => [...resolveSlotStyles(colorPickerRecipe.slots.swatch, v), chrome.swatchColor, chrome.swatchPaint],
    "ColorPickerValueSwatch",
  ),
  Swatch: withContext(
    SwatchNode,
    (v) => [
      ...resolveSlotStyles(colorPickerRecipe.slots.swatch, v),
      chrome.swatchColor,
      chrome.swatchPaint,
      chrome.swatchRing,
    ],
    "ColorPickerSwatch",
  ),
  SwatchGroup: withContext(ColorPickerSwatches, "swatchGroup", "ColorPickerSwatchGroup"),
  SwatchTrigger: withContext(
    SwatchTriggerNode,
    (v) => [chrome.btnReset, ...resolveSlotStyles(colorPickerRecipe.slots.swatchTrigger, v)],
    "ColorPickerSwatchTrigger",
  ),
  SwatchIndicator: withContext(
    SwatchIndicatorNode,
    "swatchIndicator",
    "ColorPickerSwatchIndicator",
  ),
  Content: withContext(
    ContentNode,
    (v) => [...resolveSlotStyles(colorPickerRecipe.slots.content, v), chrome.panel],
    "ColorPickerContent",
  ),
  Area: withContext(
    ColorPickerArea,
    (v) => [...resolveSlotStyles(colorPickerRecipe.slots.area, v), chrome.area],
    "ColorPickerArea",
  ),
  /** Optional: the area already paints `--color-area-background` itself. */
  AreaBackground: withContext(BackgroundLayer, "areaBackground", "ColorPickerAreaBackground"),
  AreaThumb: withContext(
    ColorPickerAreaThumb,
    (v) => [...resolveSlotStyles(colorPickerRecipe.slots.areaThumb, v), chrome.areaThumb],
    "ColorPickerAreaThumb",
  ),
  ChannelSlider: withContext(
    ColorPickerSlider,
    (v) => [...resolveSlotStyles(colorPickerRecipe.slots.channelSlider, v), chrome.slider],
    "ColorPickerChannelSlider",
  ),
  ChannelSliderTrack: withContext(
    BackgroundLayer,
    (v) => [...resolveSlotStyles(colorPickerRecipe.slots.channelSliderTrack, v), chrome.sliderTrack],
    "ColorPickerChannelSliderTrack",
  ),
  ChannelSliderThumb: withContext(
    ColorPickerSliderThumb,
    (v) => [
      ...resolveSlotStyles(colorPickerRecipe.slots.channelSliderThumb, v),
      chrome.sliderThumb,
    ],
    "ColorPickerChannelSliderThumb",
  ),
  ChannelInput: withContext(ColorPickerChannelInput, "channelInput", "ColorPickerChannelInput"),
  /** Any CSS color, committed on Enter or blur. */
  Input: withContext(ColorPickerInput, "channelInput", "ColorPickerInput"),
  EyeDropper: withContext(
    ColorPickerEyeDropper,
    (v) => [chrome.btnReset, ...resolveSlotStyles(colorPickerRecipe.slots.trigger, v)],
    "ColorPickerEyeDropper",
  ),
  TransparencyGrid: withContext(BackgroundLayer, "transparencyGrid", "ColorPickerTransparencyGrid"),
  View: withContext("div", "view", "ColorPickerView"),
};

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