Pick a color from a square and sliders, as hex, RGB or HSL, with the eyedropper, swatches and tokens.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import { Schema } from "effect"import { ColorPicker } from "./ui/uicontext/components/canvas/ColorPicker.ts"const Message = UiSchema.TaggedEnum({ RequestedColor: { color: Schema.String, token: Schema.NullOr(Schema.String) } })const tokens = [ { name: "color/accent", color: "#3D5AFE" }, { name: "color/ink", color: "#16181D" }, { name: "color/surface", color: "#F4F5F7" }, { name: "color/streak", color: "#FF8A3D" },]const swatches = [{ name: "Sun", color: "#FFD166" }, { name: "Hill", color: "#B8F2C9" }, { name: "Pink note", color: "#FFC8DD" }]// The value, and the token it came from, belong to the app.export const FillColor = Html.component({ name: "FillColor", model: Schema.Struct({ color: Schema.String, token: Schema.NullOr(Schema.String) }), message: Message,}).make({ init: () => ({ color: "#3D5AFE", token: "color/accent" }), update: { RequestedColor: ($, { color, token }) => $.evolve({ color, token }) }, view: (h, m) => ColorPicker(h, { props: { value: m.color, token: m.token, swatches, tokens }, onEmit: { RequestedColor: ({ color }) => Message.RequestedColor({ color, token: null }), RequestedToken: ({ token, color }) => Message.RequestedColor({ color, token }), }, }),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/color-pickerUsage
import { ColorPicker } from "./ui/uicontext/components/canvas/ColorPicker.ts"const Message = UiSchema.TaggedEnum({ RequestedFill: { color: Schema.String, live: Schema.Boolean } })export const Fill = (h: Html.H<typeof Message.Type>, color: string) => ColorPicker(h, { props: { value: color }, onEmit: { RequestedColor: ({ color: next, live }) => Message.RequestedFill({ color: next, live }), RequestedToken: ({ color: next }) => Message.RequestedFill({ color: next, live: false }), }, })API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
valuerequired | string | — | — |
token | string | null | — | The token the value comes from, if any: its swatch shows picked |
swatches | ColorSwatch[] | — | Colors already in the document |
tokens | ColorSwatch[] | — | Design tokens, by name |
alpha | boolean | on | Show the alpha slider and field; on by default |
Events
| Event | Payload | Description |
|---|---|---|
RequestedColor | { color: string; live: boolean } | — |
RequestedToken | { token: string; color: string } | A token was picked: store its name with the color |