Skip to content

Commit 804c556

Browse files
zernoniaclaude
andauthored
feat(Color)!: Color object model by default (#2789 Blocker B) (#3064)
* refactor(Color)!: one convert-on-demand color engine channel.ts re-implemented every rgb/hsl/hsb conversion from convert.ts. It now reads and writes channels through convert.ts only: - getChannelValue reads a channel from the color's own space when it has one (hue and saturation of an HSB color no longer go through HSL) - setChannelValues sets every channel in one space that holds them all, preferring the color's own space; getChannelColorSpace resolves it - convertToHsl/convertToHsb convert HSL<->HSB directly, keeping the hue of a grey; convertToSpace is the shared entry point - isEqualColor is symmetric, accepts strings and is exported - area gradients take a dir argument for right-to-left - RGB slider gradients use the real channels of HSL/HSB colors - parseColor accepts hsv() as documented Adds direct unit tests for parse, convert and channel (#2789). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * feat(Color)!: emit Color objects from ColorArea, ColorField and ColorSlider The model-of-record is now the plain `Color` object (#2789 Blocker B). `modelValue`/`defaultValue` accept `string | Color`; a string is parsed once. `update:modelValue`, `beforeUpdate:modelValue`, `update:color`, `change` and `changeEnd` carry a `Color` in the component's color space, and the new `update:hex` event (`onHexUpdate`) is the opt-in lossy hex output. Forms still submit hex through the hidden input. useControllableState compares with `isEqual: isEqualColor`, so the shadow-precision machinery is gone: ColorArea's `hueValue`, `isUpdating` guards and `precisionColor`/`precisionChanged`, ColorSlider's `internalColor`, 2% threshold and achromatic drift handling, and the same-hex `update:modelValue` exception. Channel values are derived from the model, so a controlled ColorArea no longer desyncs its thumb from a parent that rejects an update. - ColorField edits a channel in the space that holds it: saturation steps on #050404 no longer stick, and a hue typed for #808080 is kept - ColorField emits `change` and `changeEnd`; `changeEnd` fires only after an interaction that changed the color, on every component - ColorArea gets a `dir` prop (ConfigProvider-aware): mirrored pointer x, swapped ArrowLeft/ArrowRight, mirrored thumb and gradient - ColorSwatchPicker root and items accept `string | Color` and compare structurally Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs(Color): document the Color object model and its migration - component pages: a "Color model" section (v-model emits `Color`, `update:hex` for hex, `isEqualColor`), ColorArea right-to-left, and ColorSwatchPicker `string | Color` values - demos and the color picker example share one `v-model` and derive their hex label with `colorToHex` - migration guide and agent guide: new color section; the "known exception" about same-hex emits is removed - regenerated docs/content/meta Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(Color): report changeEnd only once the model holds the change ColorSlider tracked the uncommitted color only until the next commit, and SliderRoot sends no valueCommit after a drag that ends where it started, so changeEnd lagged one keyboard step behind from then on. A controlled parent that rejected updates caused the same lag, and ColorArea reported changeEnd for updates such a parent rejected. A shared tracker now records each interaction's start and last color and reports it only when the model holds the last color and it differs from the start, waiting a tick for a controlled parent to re-render. A keyboard valueCommit drops leftover state from an uncommitted drag. ColorField now watches the formatted value, so an in-place mutation of the parent's Color updates the input. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs(Color): note repeated update:hex strings and changeEnd timing Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs(ColorField): regenerate meta for the changeEnd description Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 8a3bd22 commit 804c556

51 files changed

Lines changed: 1606 additions & 1022 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/components/demo/ColorArea/css/index.vue‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
<script setup lang="ts">
2+
import type { Color } from 'reka-ui'
23
import {
34
ColorAreaArea,
45
ColorAreaRoot,
56
ColorAreaThumb,
7+
colorToHex,
8+
normalizeColor,
69
} from 'reka-ui'
7-
import { ref } from 'vue'
10+
import { computed, ref } from 'vue'
811
import './styles.css'
912
10-
const color = ref('#56d799')
13+
// v-model accepts a string and emits a Color object.
14+
const color = ref<string | Color>('#56d799')
15+
const hex = computed(() => colorToHex(normalizeColor(color.value)))
1116
</script>
1217

1318
<template>
1419
<div class="ColorAreaDemo">
1520
<div class="ColorAreaValue">
16-
Current: {{ color }}
21+
Current: {{ hex }}
1722
</div>
1823

1924
<ColorAreaRoot

‎docs/components/demo/ColorArea/tailwind/index.vue‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
<script setup lang="ts">
2+
import type { Color } from 'reka-ui'
23
import {
34
ColorAreaArea,
45
ColorAreaRoot,
56
ColorAreaThumb,
7+
colorToHex,
8+
normalizeColor,
69
} from 'reka-ui'
7-
import { ref } from 'vue'
10+
import { computed, ref } from 'vue'
811
9-
const color = ref('#56d799')
12+
// v-model accepts a string and emits a Color object.
13+
const color = ref<string | Color>('#56d799')
14+
const hex = computed(() => colorToHex(normalizeColor(color.value)))
1015
</script>
1116

1217
<template>
1318
<div class="flex flex-col gap-4">
1419
<div class="text-sm text-gray-600 dark:text-gray-400">
15-
Current: {{ color }}
20+
Current: {{ hex }}
1621
</div>
1722

1823
<ColorAreaRoot

‎docs/components/demo/ColorField/css/index.vue‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
<script setup lang="ts">
2+
import type { Color } from 'reka-ui'
23
import {
34
ColorFieldInput,
45
ColorFieldRoot,
6+
colorToHex,
7+
normalizeColor,
58
} from 'reka-ui'
6-
import { ref } from 'vue'
9+
import { computed, ref } from 'vue'
710
import './styles.css'
811
9-
const color = ref('#56d799')
12+
// v-model accepts a string and emits a Color object.
13+
const color = ref<string | Color>('#56d799')
14+
const hex = computed(() => colorToHex(normalizeColor(color.value)))
1015
</script>
1116

1217
<template>
@@ -21,7 +26,7 @@ const color = ref('#56d799')
2126
<div class="ColorFieldControl">
2227
<div
2328
class="ColorFieldSwatch"
24-
:style="{ background: color }"
29+
:style="{ background: hex }"
2530
/>
2631
<ColorFieldInput class="ColorFieldInput" />
2732
</div>

‎docs/components/demo/ColorField/tailwind/index.vue‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<script setup lang="ts">
2+
import type { Color } from 'reka-ui'
23
import {
34
ColorFieldInput,
45
ColorFieldRoot,
6+
colorToHex,
7+
normalizeColor,
58
} from 'reka-ui'
6-
import { ref } from 'vue'
9+
import { computed, ref } from 'vue'
710
8-
const color = ref('#56d799')
11+
// v-model accepts a string and emits a Color object.
12+
const color = ref<string | Color>('#56d799')
13+
const hex = computed(() => colorToHex(normalizeColor(color.value)))
914
</script>
1015

1116
<template>
@@ -20,7 +25,7 @@ const color = ref('#56d799')
2025
<div class="flex items-center gap-2">
2126
<div
2227
class="w-8 h-8 rounded border border-muted shrink-0"
23-
:style="{ background: color }"
28+
:style="{ background: hex }"
2429
/>
2530
<ColorFieldInput
2631
class="px-3 py-2 border border-foreground/10 bg-muted text-foreground rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-green-500 focus:border-green-500 disabled:opacity-50 disabled:cursor-not-allowed w-32"

‎docs/components/demo/ColorSlider/css/index.vue‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
<script setup lang="ts">
2+
import type { Color } from 'reka-ui'
23
import {
34
ColorSliderRoot,
45
ColorSliderThumb,
56
ColorSliderTrack,
7+
colorToHex,
8+
normalizeColor,
69
} from 'reka-ui'
7-
import { ref } from 'vue'
10+
import { computed, ref } from 'vue'
811
import './styles.css'
912
10-
const color = ref('#56d799')
13+
// v-model accepts a string and emits a Color object.
14+
const color = ref<string | Color>('#56d799')
15+
const hex = computed(() => colorToHex(normalizeColor(color.value)))
1116
</script>
1217

1318
<template>
@@ -26,9 +31,9 @@ const color = ref('#56d799')
2631
<div class="ColorSliderValue">
2732
<div
2833
class="ColorSliderSwatch"
29-
:style="{ background: color }"
34+
:style="{ background: hex }"
3035
/>
31-
<span class="ColorSliderCode">{{ color }}</span>
36+
<span class="ColorSliderCode">{{ hex }}</span>
3237
</div>
3338
</div>
3439
</template>

‎docs/components/demo/ColorSlider/tailwind/index.vue‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
<script setup lang="ts">
2+
import type { Color } from 'reka-ui'
23
import {
34
ColorSliderRoot,
45
ColorSliderThumb,
56
ColorSliderTrack,
7+
colorToHex,
8+
normalizeColor,
69
} from 'reka-ui'
7-
import { ref } from 'vue'
10+
import { computed, ref } from 'vue'
811
9-
const color = ref('#56d799')
12+
// v-model accepts a string and emits a Color object.
13+
const color = ref<string | Color>('#56d799')
14+
const hex = computed(() => colorToHex(normalizeColor(color.value)))
1015
</script>
1116

1217
<template>
@@ -29,9 +34,9 @@ const color = ref('#56d799')
2934
<div class="flex items-center gap-2 text-xs text-gray-500">
3035
<div
3136
class="w-5 h-5 rounded border border-gray-200 shrink-0"
32-
:style="{ background: color }"
37+
:style="{ background: hex }"
3338
/>
34-
<span class="font-mono">{{ color }}</span>
39+
<span class="font-mono">{{ hex }}</span>
3540
</div>
3641
</div>
3742
</template>

‎docs/components/examples/ColorPicker/index.vue‎

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ import { computed, ref } from 'vue'
2121
2222
const colorObj = ref<Color>(normalizeColor('#56d799'))
2323
const hexColor = computed(() => colorToString(colorObj.value, 'hex'))
24-
25-
function handleColorUpdate(newColor: Color) {
26-
colorObj.value = newColor
27-
}
28-
29-
function handleHexUpdate(hex: string) {
30-
colorObj.value = normalizeColor(hex)
31-
}
3224
</script>
3325

3426
<template>
@@ -67,12 +59,11 @@ function handleHexUpdate(hex: string) {
6759
<!-- 2D Color Area (Saturation/Lightness) -->
6860
<ColorAreaRoot
6961
v-slot="{ style }"
70-
:model-value="colorObj"
62+
v-model="colorObj"
7163
color-space="hsl"
7264
x-channel="saturation"
7365
y-channel="lightness"
7466
class="relative"
75-
@update:color="handleColorUpdate"
7667
>
7768
<ColorAreaArea
7869
class="relative w-full h-[140px] rounded-md overflow-hidden focus:outline-none focus:ring-2 focus:ring-primary"
@@ -86,11 +77,10 @@ function handleHexUpdate(hex: string) {
8677
<div class="flex flex-col gap-2">
8778
<label class="text-xs font-medium text-muted-foreground">Hue</label>
8879
<ColorSliderRoot
89-
:model-value="colorObj"
80+
v-model="colorObj"
9081
channel="hue"
9182
color-space="hsl"
9283
class="relative flex items-center w-full h-4"
93-
@update:color="handleColorUpdate"
9484
>
9585
<ColorSliderTrack class="relative flex-1 rounded-full h-2">
9686
<div class="absolute inset-0 rounded-full hue-gradient" />
@@ -103,11 +93,10 @@ function handleHexUpdate(hex: string) {
10393
<div class="flex flex-col gap-2">
10494
<label class="text-xs font-medium text-muted-foreground">Alpha</label>
10595
<ColorSliderRoot
106-
:model-value="colorObj"
96+
v-model="colorObj"
10797
channel="alpha"
10898
color-space="hsl"
10999
class="relative flex items-center w-full h-4"
110-
@update:color="handleColorUpdate"
111100
>
112101
<ColorSliderTrack class="relative flex-1 rounded-full h-2 checkerboard-bg">
113102
<div class="absolute inset-0 rounded-full" />
@@ -121,45 +110,41 @@ function handleHexUpdate(hex: string) {
121110
<label class="text-xs font-medium text-muted-foreground">Values</label>
122111
<div class="flex gap-2">
123112
<ColorFieldRoot
124-
:model-value="hexColor"
113+
v-model="colorObj"
125114
class="flex-[2]"
126-
@update:model-value="handleHexUpdate"
127115
>
128116
<ColorFieldInput
129117
class="w-full px-2 py-1.5 text-sm border border-muted bg-background rounded-md text-foreground focus:outline-none focus:ring-2 focus:ring-primary focus:border-primary font-mono"
130118
placeholder="#000000"
131119
/>
132120
</ColorFieldRoot>
133121
<ColorFieldRoot
134-
:model-value="colorObj"
122+
v-model="colorObj"
135123
channel="hue"
136124
color-space="hsl"
137125
class="flex-1"
138-
@update:color="handleColorUpdate"
139126
>
140127
<ColorFieldInput
141128
class="w-full px-2 py-1.5 text-sm border border-muted bg-background rounded-md text-foreground focus:outline-none focus:ring-2 focus:ring-primary focus:border-primary text-center"
142129
placeholder="H"
143130
/>
144131
</ColorFieldRoot>
145132
<ColorFieldRoot
146-
:model-value="colorObj"
133+
v-model="colorObj"
147134
channel="saturation"
148135
color-space="hsl"
149136
class="flex-1"
150-
@update:color="handleColorUpdate"
151137
>
152138
<ColorFieldInput
153139
class="w-full px-2 py-1.5 text-sm border border-muted bg-background rounded-md text-foreground focus:outline-none focus:ring-2 focus:ring-primary focus:border-primary text-center"
154140
placeholder="S"
155141
/>
156142
</ColorFieldRoot>
157143
<ColorFieldRoot
158-
:model-value="colorObj"
144+
v-model="colorObj"
159145
channel="lightness"
160146
color-space="hsl"
161147
class="flex-1"
162-
@update:color="handleColorUpdate"
163148
>
164149
<ColorFieldInput
165150
class="w-full px-2 py-1.5 text-sm border border-muted bg-background rounded-md text-foreground focus:outline-none focus:ring-2 focus:ring-primary focus:border-primary text-center"

‎docs/content/docs/components/color-area.md‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ A two-dimensional area control that allows users to select color values by inter
2424
'Full keyboard navigation support.',
2525
'Pointer/touch interaction for value selection.',
2626
'Preserves hue information when saturation is 0.',
27+
'Emits a lossless Color object, with an opt-in hex output.',
28+
'Supports right-to-left direction.',
2729
'Form integration with hidden inputs.',
2830
]"
2931
/>
@@ -66,7 +68,7 @@ import {
6668

6769
These composables are experimental. Reactive options accept refs or getters. Pass a writable `modelValue` ref for ref-owned state, or use `onUpdate` to handle controlled updates. `onBeforeUpdate(value, details)` can synchronously call `details.cancel()` to prevent a change. Component users can use `@before-update:model-value` for the same behavior.
6870

69-
`useColorArea()` exposes the model, exact channel values, background styles, and shared root and thumb surfaces. Call it in component setup (or an effect scope) so its synchronization watchers are disposed. Create one area surface per element and bind `setThumbElement` to the thumb so pointer interaction can focus it.
71+
`useColorArea()` exposes the model, exact channel values, background styles, and shared root and thumb surfaces. The channel values and the thumb position are derived from the model, so a controlled parent that rejects an update keeps the thumb where the model is. Create one area surface per element and bind `setThumbElement` to the thumb so pointer interaction can focus it. Pass `dir: 'rtl'` to mirror the x axis.
7072

7173
```vue
7274
<script setup>
@@ -99,7 +101,37 @@ updateValues(75, 50)
99101

100102
Hidden form inputs remain part of `ColorAreaRoot`; add them yourself when rendering the composable directly.
101103

102-
`onColorUpdate` receives accepted color objects and `onChange` / `onChangeEnd` receive interaction and commit notifications. These callbacks work without a component `emit` function. Returned state refs are read-only; use the exposed actions to request changes.
104+
`onUpdate`, `onColorUpdate`, `onChange` and `onChangeEnd` receive `Color` objects, and `onHexUpdate` receives the hex serialization of each accepted color. `onChange` fires for every change, `onChangeEnd` once the pointer is released and after each keyboard step. These callbacks work without a component `emit` function. Returned state refs are read-only; use the exposed actions to request changes.
105+
106+
## Color model
107+
108+
`v-model` accepts a color string (`'#3b82f6'`, `'hsl(217, 91%, 60%)'`) or a `Color` object, and every emitted value is a `Color` object: a plain `{ space, ... }` object in the component's color space, such as `{ space: 'hsl', h: 217, s: 91, l: 60, alpha: 1 }`. A string is parsed once on the way in. Keeping the object keeps information a hex string cannot hold, such as the hue of a grey and fractional channel values, so several color components can share one `v-model` without drift.
109+
110+
`update:color` carries the same `Color` as `update:modelValue`. Listen to `update:hex` when you need a hex string, for example for an API or a text label. Hex is lossy: it drops the hue of greys and anything finer than one 8-bit step, so feed the `Color` back into `v-model`, not the hex. For the same reason `update:hex` can repeat the same string when the color moves by less than one step. A form submits the hex value through the hidden input either way.
111+
112+
```vue
113+
<script setup>
114+
import { ColorAreaArea, ColorAreaRoot, ColorAreaThumb } from 'reka-ui'
115+
import { ref } from 'vue'
116+
117+
const color = ref('#3b82f6') // becomes a Color object after the first change
118+
const hex = ref('#3b82f6')
119+
</script>
120+
121+
<template>
122+
<ColorAreaRoot
123+
v-slot="{ style }"
124+
v-model="color"
125+
@update:hex="hex = $event"
126+
>
127+
<ColorAreaArea :style="style">
128+
<ColorAreaThumb />
129+
</ColorAreaArea>
130+
</ColorAreaRoot>
131+
</template>
132+
```
133+
134+
Use `isEqualColor(a, b)` to compare two colors (strings or objects) structurally, and `colorToHex` or `colorToString` to format one.
103135

104136
## API Reference
105137

@@ -227,5 +259,7 @@ const color = ref('#3b82f6')
227259
| `Shift + ArrowKey` | Changes values by 10 steps at a time. |
228260
| `PageUp` | Increases the y-axis channel value by a larger step. |
229261
| `PageDown` | Decreases the y-axis channel value by a larger step. |
230-
| `Home` | Jumps left (decreases x-axis value). |
231-
| `End` | Jumps right (increases x-axis value). |
262+
| `Home` | Decreases the x-axis channel value by a larger step. |
263+
| `End` | Increases the x-axis channel value by a larger step. |
264+
265+
In right-to-left mode (`dir="rtl"`, or inherited from `ConfigProvider`) the x axis runs from right to left: `ArrowLeft` increases and `ArrowRight` decreases the x-axis value, and the thumb and the background gradient are mirrored.

‎docs/content/docs/components/color-field.md‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ An input field component for entering color values. Supports hex color input or
2727
'Home/End keys to jump to min/max values.',
2828
'Form integration with hidden inputs.',
2929
'Support for HSL, HSB, and RGB color spaces.',
30+
'Emits a lossless Color object, with an opt-in hex output.',
3031
]"
3132
/>
3233

@@ -86,7 +87,36 @@ const input = createInputSurface()
8687

8788
The return also exposes `updateValue`, `commit`, `increment`, `decrement`, page stepping, and min/max actions. Hidden form input rendering remains in `ColorFieldRoot`.
8889

89-
`onColorUpdate` receives accepted color objects. These callbacks work without a component `emit` function. Returned state refs are read-only; use the exposed actions to request changes.
90+
`onUpdate`, `onColorUpdate`, `onChange` and `onChangeEnd` receive `Color` objects, and `onHexUpdate` receives the hex serialization of each accepted color. A field changes only when an edit is committed (Enter or blur) or a value is stepped, so `onChange` and `onChangeEnd` fire together for each change. These callbacks work without a component `emit` function. Returned state refs are read-only; use the exposed actions to request changes.
91+
92+
## Color model
93+
94+
`v-model` accepts a color string (`'#3b82f6'`, `'hsl(217, 91%, 60%)'`) or a `Color` object, and every emitted value is a `Color` object: a plain `{ space, ... }` object in the component's color space, such as `{ space: 'hsl', h: 217, s: 91, l: 60, alpha: 1 }`. A string is parsed once on the way in. Keeping the object keeps information a hex string cannot hold, such as the hue of a grey and fractional channel values, so several color components can share one `v-model` without drift.
95+
96+
`update:color` carries the same `Color` as `update:modelValue`. Listen to `update:hex` when you need a hex string, for example for an API or a text label. Hex is lossy: it drops the hue of greys and anything finer than one 8-bit step, so feed the `Color` back into `v-model`, not the hex. For the same reason `update:hex` can repeat the same string when the color moves by less than one step. A form submits the hex value through the hidden input either way.
97+
98+
```vue
99+
<script setup>
100+
import { ColorFieldInput, ColorFieldRoot } from 'reka-ui'
101+
import { ref } from 'vue'
102+
103+
const color = ref('#3b82f6') // becomes a Color object after the first change
104+
const hex = ref('#3b82f6')
105+
</script>
106+
107+
<template>
108+
<ColorFieldRoot
109+
v-model="color"
110+
@update:hex="hex = $event"
111+
>
112+
<ColorFieldInput />
113+
</ColorFieldRoot>
114+
</template>
115+
```
116+
117+
Use `isEqualColor(a, b)` to compare two colors (strings or objects) structurally, and `colorToHex` or `colorToString` to format one.
118+
119+
A channel field edits the color in the space that holds its channel (the `color-space` prop when it does). So with `channel="hue"`, typing a hue for a grey keeps it, and stepping `saturation` moves by whole steps even when a hex input rounded the channel to a fraction.
90120

91121
## API Reference
92122

0 commit comments

Comments
 (0)