Colors

Color Swatch Picker

Choose from a predefined set of colors.

Rust
1use herogpui::components::color_picker::ColorSwatchPicker;

Usage

Rust
1use herogpui::prelude::{ColorSwatchPicker, PickerColor, SizeXl};
2use gpui::prelude::*;
3use gpui::px;
4
5gpui::div()
6    .flex()
7    .flex_col()
8    .items_start()
9    .gap(px(12.))
10    .children(vec![
11        ColorSwatchPicker::new("color-swatch-picker-main", palette())
12            .value(selected)
13            .size(SizeXl::Lg)
14            .on_change(color_cb(cx.listener(|this, c: &PickerColor, _, cx| {
15                this.swatch_selected = *c;
16                cx.notify();
17            })))
18            .into_any_element(),
19        para(&format!("Selected: {}", selected.to_hex()), cx),
20    ])

Live HeroGPUI compiled to WebAssembly. Select any example without loading another WASM instance.

Anatomy

Rust types

ColorSwatchPicker

Color Swatch Picker is assembled from these builders. The API reference lists each one.

Customization

Appearance builders and theme tokens Color Swatch Picker uses.

Styling

Color Swatch Picker styling
StyleDescription
flex + items_center + gap(8) + flex_wrap + max_w(280)Grid alignment, eight-pixel gap, wrapping, and 280px desktop cap match.
flex_col + Up/Down navigationStack layout is vertical and its keyboard direction follows the visual axis.
SizeXl::swatch_px + border_widthEvery size drives paint, hit testing, indicator size, wrapping, and grid stride.
item_radius + inner radiusPinned item and inner-swatch radii match, including the selected small-square exception.
computed inner edge + hover sizeFinal geometry and selected-over-hover precedence match; interpolation is not implemented.
indicator / CHECKPosition, size, and luminance contrast match; custom content uses the same centered owner.

API reference

Builders

Color Swatch Picker builders
BuilderTypeDefaultDescription
value(PickerColor)PickerColorControlled selection changes only when the owner supplies the reported color.
default_value(PickerColor)PickerColorSeeds picker-owned selection once.
on_change(callback)Fn(value: Color) -> ()Reports pointer and keyboard selection changes.
size(SizeXl)SizeXlSizeXl::MdDrives item edge, border, radius, indicator, hit geometry, wrapping, and grid navigation stride.
shape(SwatchShape)SwatchShapeSwatchShape::CircleSelects the per-size item and swatch radii.
layout(SwatchLayout)SwatchLayoutSwatchLayout::GridGrid wraps and uses geometric row navigation; stack is vertical and uses Up/Down.
new(id, Vec<PickerColor>)id, Vec<PickerColor>The Rust constructor receives a concrete palette and composes one item per color.
new(id, Vec<PickerColor>) / item_contentid, Vec<PickerColor> / string | ColorrequiredEach palette color is supplied by index and reaches item render state.
disabled_keys(indices)boolfalsePer-index disabled items dim, leave navigation, and mask interaction state.
item_content(render)AnyElement | render closureSwatch + IndicatorReplacement content receives color, hover, press, selection, focus, focus-visible, and disabled state.
indicator(render)AnyElement | render closurecheckmarkReplacement indicator content receives the complete item state.

Parts

Color Swatch Picker parts
PartDescription
ColorSwatchPickerSelection owner and wrapping grid or vertical stack.
ColorSwatchPickerStable focus, pointer, selection, and render-state owner.
ColorSwatchPickerCheckerboard-backed color visual with selected and hover geometry.
ColorSwatchPickerCentered selected marker with luminance-aware default color or caller content.

States

Color Swatch Picker states
BuilderStateDescription
interaction + item_contentHoveredEnabled unselected swatches grow to 1.1 and item content receives hover.
interaction + item_contentPressedEnabled item content receives live pointer and keyboard press state.
resolved value + item_content / indicatorSelectedThe color border is revealed by a 0.77 inner swatch and item/indicator state updates.
cursor + tab_stop_handleFocusedExactly one enabled item owns the roving focus handle.
with_focus_ring + item_contentFocus visibleKeyboard-origin focus draws the shared ring and reaches item state.
is_disabled / disabled_keysDisabledWhole-picker and per-item disabling dim, suppress events, remove navigation stops, and mask state.