SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

ColorPicker

A labeled color control composed from a caption, a ColorPicker.Group row joining a text input to a ColorPicker.Trigger.

View code
tsx
let { hue, saturation, brightness } = OPENING_COLOR;
let alpha = 1;
let hex = formatHex({ ...hsvToRgb({ h: hue, s: saturation, v: brightness }), a: alpha });

// channelSync() reports every channel together rather than one at a time, so the
// sliders and the area stay one color rather than two views of different ones.
function moveChannel(event: ColorChannelChangeEvent) {
	if (typeof event.values.saturation === "number") saturation = event.values.saturation;
	if (typeof event.values.alpha === "number") alpha = event.values.alpha;
	void handle.update();
}

<ColorPicker>
	<Label htmlFor="preview-brand-color">Brand color</Label>

	<ColorPicker.Group>
		<Input
			id="preview-brand-color"
			type="text"
			name="brandColor"
			value={hex}
			mix={[font("mono")]}
		/>
		<ColorPicker.Trigger
			commandfor="preview-brand-color-panel"
			command="toggle-popover"
			aria-label="Open the color picker"
			value={hex}
		/>
	</ColorPicker.Group>

	<ColorPicker.Dialog id="preview-brand-color-panel" mix={[is("16rem")]}>
		<ColorArea
			aria-label="Saturation and brightness"
			hue={hue}
			saturation={saturation}
			value={brightness}
			style={{ "--ui-color-area-size": "12rem" }}
			mix={[
				colorAreaDrag(),
				on<HTMLDivElement, "ui:color-area-change">("ui:color-area-change", (event) => {
					saturation = event.x;
					brightness = event.y;
					void handle.update();
				}),
			]}
		>
			<ColorArea.SaturationThumb data-color-area-axis="x" aria-label="Saturation" />
			<ColorArea.ValueThumb data-color-area-axis="y" aria-label="Brightness" />
		</ColorArea>

		<div mix={[hstack({ gap: 3, align: "center" })]}>
			<ColorWheel
				aria-label="Hue"
				value={hue}
				mix={[
					is("5rem"),
					bs("5rem"),
					colorWheelDrag(),
					on<HTMLDivElement, "ui:color-wheel-change">("ui:color-wheel-change", (event) => {
						hue = event.hue;
						void handle.update();
					}),
				]}
			/>
			<div
				mix={[
					vstack({ gap: 2, align: "stretch" }),
					basis(0),
					grow(),
					minIs(0),
					channelSync(),
					on<HTMLDivElement, "ui:color-channel-change">("ui:color-channel-change", moveChannel),
				]}
			>
				<ColorSlider channel="saturation" value={saturation}>
					<ColorSlider.Track hue={hue}>
						<ColorSlider.Thumb data-channel="saturation" aria-label="Saturation" />
					</ColorSlider.Track>
				</ColorSlider>
				<ColorSlider channel="alpha" value={alpha}>
					<ColorSlider.Track hue={hue}>
						<ColorSlider.Thumb data-channel="alpha" aria-label="Opacity" />
					</ColorSlider.Track>
				</ColorSlider>
			</div>
		</div>

		<Separator />

		<ColorSwatchPicker
			aria-label="Saved colors"
			name="brandColorPreset"
			mix={[on<HTMLDivElement, "change">("change", choosePreset)]}
		>
			{PRESETS.map((preset) => (
				<ColorSwatchPicker.Swatch
					key={preset.value}
					value={preset.value}
					aria-label={preset.label}
				/>
			))}
		</ColorSwatchPicker>

		<span mix={[font("mono"), text("xs"), fg("neutral")]}>{hex}</span>
	</ColorPicker.Dialog>
</ColorPicker>

Installation

An ordinary dependency: install it, import the theme once, and import the component where it is used.

Terminal
npm add @sdxc/ui remix
typescript
import "@sdxc/ui/theme.css";

Usage

typescript
import { ColorPicker } from "@sdxc/ui/color";

Renders ColorPicker's root: a column stacking the caption, the trigger row and the picking surface the caller writes, in the order written.

Composition

The parts the component publishes, each a static property of the host.

  • ColorPicker
  • ColorPicker.GroupRenders ColorPicker's control row: a plain flex host laying the field's own control and ColorPicker.Trigger side by side, with a keyboard focus ring on the whole row the moment focus lands anywhere inside.
  • ColorPicker.TriggerRenders ColorPicker's trailing trigger: a <button> housing a ColorSwatch that fills it as a live preview, wired via commandfor/command="toggle-popover" to ColorPicker.Dialog's id.
  • ColorPicker.DialogRenders ColorPicker's picking surface: a Popover, defaulting placement to bottom-start and role to "dialog", padding whatever picking controls a consumer composes as children in a single column.

Examples

Saved presets

View code
tsx
let color = "#3b82f6";

function pick(event: Event) {
	let next = (event.target as HTMLInputElement).value;
	if (parseColor(next) === null) return;
	color = next;
	void handle.update();
}

<ColorPicker>
	<Label htmlFor="brand-color">Brand color</Label>
	<ColorPicker.Group>
		<Input
			id="brand-color"
			type="text"
			name="brandColor"
			value={color}
			mix={[font("mono"), on<HTMLInputElement, "change">("change", pick)]}
		/>
		<ColorPicker.Trigger
			commandfor="brand-color-panel"
			command="toggle-popover"
			aria-label="Choose a saved brand color"
			value={color}
		/>
	</ColorPicker.Group>
	<ColorPicker.Dialog id="brand-color-panel">
		<ColorSwatchPicker
			aria-label="Saved brand colors"
			name="savedBrandColor"
			mix={[on<HTMLDivElement, "change">("change", pick)]}
		>
			{PRESETS.map((preset) => (
				<ColorSwatchPicker.Swatch
					key={preset.value}
					value={preset.value}
					aria-label={preset.label}
					checked={preset.value === color}
				/>
			))}
		</ColorSwatchPicker>
	</ColorPicker.Dialog>
</ColorPicker>

Props

Read from the component's own types, so every prop, every default and every allowed value is listed.

Also accepts everything in TagProps<"div">.

ColorPicker.Group

Also accepts everything in TagProps<"div">.

ColorPicker.Trigger

PropTypeDefaultRequiredDescription
valuestring—YesThe color this trigger previews, already resolved to a literal CSS color value — the same value its inner ColorSwatch paints. Required, since a trigger with nothing to preview has no reason to render.
shapeColorSwatch.Shape—NoShape variant for the inner ColorSwatch, mirrored onto the button host so its own corners match. Defaults to DEFAULT_TRIGGER_SHAPE.

Also accepts everything in Omit<TagProps<"button">, "children">.

ColorPicker.Dialog

Also accepts everything in Popover.Props.

@sdxc/ui

Can’t find it? Contact us