@kolkrabbi/kol-component 0.263.0 → 0.264.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kolkrabbi/kol-component",
3
- "version": "0.263.0",
3
+ "version": "0.264.0",
4
4
  "description": "The core component library",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -38,7 +38,7 @@
38
38
  "react-dom": "^18.3.0 || ^19.0.0"
39
39
  },
40
40
  "devDependencies": {
41
- "@kolkrabbi/kol-icons": "^0.38.0"
41
+ "@kolkrabbi/kol-icons": "^0.39.0"
42
42
  },
43
43
  "files": [
44
44
  "src",
@@ -65,6 +65,31 @@ export function rgbToHex(r, g, b) {
65
65
  .toUpperCase()
66
66
  }
67
67
 
68
+ /* ── HSVA / hex — the picker's model (ColorPickerPopover, kol-monitor 2026-10-17) ── */
69
+
70
+ /** '#RRGGBB' | '#RRGGBBAA' → { hue 0–360, sat 0–100, val 0–100, alpha 0–1 }; null when unparsable. */
71
+ export function hexToHsva(hex) {
72
+ const m = /^#?([0-9a-f]{6})([0-9a-f]{2})?$/i.exec(hex ?? '')
73
+ if (!m) return null
74
+ const [r, g, b] = [0, 2, 4].map((i) => parseInt(m[1].slice(i, i + 2), 16) / 255)
75
+ const max = Math.max(r, g, b)
76
+ const d = max - Math.min(r, g, b)
77
+ const hue = d === 0 ? 0
78
+ : max === r ? 60 * (((g - b) / d) % 6)
79
+ : max === g ? 60 * ((b - r) / d + 2)
80
+ : 60 * ((r - g) / d + 4)
81
+ return { hue: normHue(hue), sat: max ? (d / max) * 100 : 0, val: max * 100, alpha: m[2] ? parseInt(m[2], 16) / 255 : 1 }
82
+ }
83
+
84
+ /** { hue, sat, val, alpha } → uppercase '#RRGGBB', or '#RRGGBBAA' when alpha < 1. */
85
+ export function hsvaToHex({ hue, sat, val, alpha = 1 }) {
86
+ const s = sat / 100
87
+ const v = val / 100
88
+ const f = (n) => { const k = (n + hue / 60) % 6; return (v - v * s * Math.max(0, Math.min(k, 4 - k, 1))) * 255 }
89
+ const hex = rgbToHex(f(5), f(3), f(1))
90
+ return alpha < 1 ? hex + Math.round(Math.max(0, alpha) * 255).toString(16).padStart(2, '0').toUpperCase() : hex
91
+ }
92
+
68
93
  /** Wrap any hue into [0, 360). */
69
94
  export const normHue = (h) => ((h % 360) + 360) % 360
70
95
 
package/src/index.js CHANGED
@@ -130,7 +130,8 @@ export { default as ShellSearchOverlay } from './organisms/ShellSearchOverlay.js
130
130
  export { default as ShortcutsOverlay } from './organisms/ShortcutsOverlay.jsx'
131
131
  export { default as Slider } from './molecules/Slider.jsx'
132
132
  export { default as SpecList } from './molecules/SpecList.jsx'
133
- export { default as SpectrumControls, HueStrip, SBSquare, WheelTriangle } from './organisms/SpectrumControls.jsx'
133
+ export { default as SpectrumControls, HueStrip, SBSquare, WheelTriangle, AlphaStrip } from './organisms/SpectrumControls.jsx'
134
+ export { default as ColorPickerPopover } from './organisms/ColorPickerPopover.jsx'
134
135
  export { default as SplitToolButton } from './molecules/SplitToolButton.jsx'
135
136
  export { default as ToolPalette } from './organisms/ToolPalette.jsx'
136
137
  export { default as DocumentEditor } from './organisms/DocumentEditor.jsx'
@@ -263,7 +264,7 @@ export { resolveCssVar, resolveCssColor, isLight } from './hooks/cssVar.js'
263
264
 
264
265
  // color math (support module — HSL/hex conversion + harmony generation)
265
266
  export {
266
- hexToHsl, hslToHex, rgbToHex, normHue,
267
+ hexToHsl, hslToHex, rgbToHex, normHue, hexToHsva, hsvaToHex,
267
268
  HARMONIES, harmonyById, harmonyColors, generateHarmony,
268
269
  SEED_MODE_IDS, seedHarmony,
269
270
  } from './hooks/colorMath.js'
@@ -0,0 +1,127 @@
1
+ import { useState } from 'react'
2
+ import ColorSwatch from '../molecules/ColorSwatch.jsx'
3
+ import Input from '../atoms/Input'
4
+ import { usePopover, PopoverPanel } from '../utilities/Popover.jsx'
5
+ import { HueStrip, SBSquare, AlphaStrip } from './SpectrumControls.jsx'
6
+ import { hexToHsva, hsvaToHex } from '../hooks/colorMath.js'
7
+ import { resolveCssColor } from '../hooks/cssVar.js'
8
+
9
+ /* taxonomy-ok: organism — composes the SpectrumControls family (an organism) with a swatch and the hex Input */
10
+
11
+ const SWATCH = { xs: 16, sm: 24 }
12
+ const PANEL_W = 180
13
+
14
+ /* the browser's computed `rgb()` / `rgba()` → '#RRGGBB(AA)' — for `currentColor` and tokens */
15
+ function cssToHex(css) {
16
+ const n = css.match(/[\d.]+/g)
17
+ if (!n || n.length < 3) return '#000000'
18
+ return hsvaToHex({ ...hexToHsva('#' + n.slice(0, 3).map((v) => Math.round(+v).toString(16).padStart(2, '0')).join('')), alpha: n[3] != null ? +n[3] : 1 })
19
+ }
20
+
21
+ /**
22
+ * ColorPickerPopover — A swatch that opens a colour picker. Click the swatch: a popover with the
23
+ * saturation square, hue and opacity strips, and the hex under them (ColorPickerPopover, kol-monitor
24
+ * 2026-10-17 — the estate's only click-a-colour-in picker was monitor's local react-colorful atom).
25
+ * Built on the KOL's own `SpectrumControls` family plus `AlphaStrip`, so no picker dependency.
26
+ *
27
+ * The swatch paints `color` live — a `var(--kol-*)` or `currentColor` follows the theme by CSS, no
28
+ * observer. Opening resolves it once against the swatch for the picker. Placement is `usePopover`'s:
29
+ * right-aligned under the swatch, flipping up where there is no room. Esc and an outside press close
30
+ * it; ⌥-click on the swatch resets to `defaultValue` instead of opening.
31
+ *
32
+ * @param {string} color '#RRGGBB' · '#RRGGBBAA' · 'transparent' / empty (none) · any CSS colour
33
+ * @param {Function} onChange (hex) => void — every drag, and every complete 6- or 8-digit hex typed.
34
+ * Uppercase; 8 digits only when opacity < 100%
35
+ * @param {string} [defaultValue] ⌥-click on the swatch resets to it
36
+ * @param {'xs'|'sm'} [size='xs'] swatch 16 · 24
37
+ * @param {string} [label='Color'] the swatch's accessible name
38
+ */
39
+ export default function ColorPickerPopover({ color, onChange, defaultValue, size = 'xs', label = 'Color', className = '' }) {
40
+ const isNone = !color || color === 'transparent'
41
+ const [open, setOpen] = useState(false)
42
+ const [hsva, setHsva] = useState(null)
43
+ const [draft, setDraft] = useState('')
44
+
45
+ const popover = usePopover({
46
+ open,
47
+ placement: 'bottom-end',
48
+ offset: 4,
49
+ onOpenChange: (next, event) => {
50
+ if (next && event?.altKey) {
51
+ if (defaultValue !== undefined) onChange?.(defaultValue)
52
+ return
53
+ }
54
+ if (next) {
55
+ /* the working copy is HSVA, kept while open — so the hue survives a drag to grey */
56
+ const literal = isNone ? '#000000'
57
+ : hexToHsva(color) ? color
58
+ : cssToHex(resolveCssColor(color, popover.refs.reference.current))
59
+ const h = hexToHsva(literal) ?? { hue: 0, sat: 0, val: 0, alpha: 1 }
60
+ setHsva(h)
61
+ setDraft(hsvaToHex(h).slice(1))
62
+ }
63
+ setOpen(next)
64
+ },
65
+ })
66
+
67
+ const commit = (next) => {
68
+ setHsva(next)
69
+ const hex = hsvaToHex(next)
70
+ setDraft(hex.slice(1))
71
+ onChange?.(hex)
72
+ }
73
+
74
+ const onHex = (e) => {
75
+ const digits = e.target.value.replace(/[^0-9a-f]/gi, '').slice(0, 8).toUpperCase()
76
+ setDraft(digits)
77
+ const next = (digits.length === 6 || digits.length === 8) && hexToHsva(digits)
78
+ if (next) { setHsva(next); onChange?.('#' + digits) }
79
+ }
80
+
81
+ const opaque = hsva && hsvaToHex({ ...hsva, alpha: 1 })
82
+
83
+ return (
84
+ <>
85
+ <button
86
+ type="button"
87
+ ref={popover.refs.setReference}
88
+ {...popover.getReferenceProps()}
89
+ aria-label={`${label}: ${isNone ? 'none' : color}`}
90
+ aria-expanded={open}
91
+ className={`inline-flex shrink-0 rounded focus-visible:outline-1 focus-visible:outline-offset-2 ${className}`}
92
+ >
93
+ <ColorSwatch
94
+ hex={isNone ? null : color}
95
+ size={SWATCH[size] ?? SWATCH.xs}
96
+ showTransparent={isNone}
97
+ transparentTone="error"
98
+ hoverable={false}
99
+ className="border border-[var(--kol-border-default)]"
100
+ />
101
+ </button>
102
+ {open && hsva && (
103
+ <PopoverPanel
104
+ popover={popover}
105
+ panel={false}
106
+ className="bg-surface-secondary border border-fg-08 rounded p-2 flex flex-col gap-2 shadow-lg"
107
+ style={{ width: PANEL_W + 16 }}
108
+ >
109
+ <div className="rounded-[var(--kol-radius-xs)] overflow-hidden" style={{ width: PANEL_W, height: 140 }}>
110
+ <SBSquare hue={hsva.hue} sat={hsva.sat} val={hsva.val} onChange={(sat, val) => commit({ ...hsva, sat, val })} />
111
+ </div>
112
+ <HueStrip hue={hsva.hue} onChange={(hue) => commit({ ...hsva, hue })} />
113
+ <AlphaStrip alpha={hsva.alpha} color={opaque} onChange={(alpha) => commit({ ...hsva, alpha })} />
114
+ <Input
115
+ variant="filled"
116
+ size="sm"
117
+ prefix="#"
118
+ maxLength={8}
119
+ value={draft}
120
+ onChange={onHex}
121
+ aria-label={`${label} hex`}
122
+ />
123
+ </PopoverPanel>
124
+ )}
125
+ </>
126
+ )
127
+ }
@@ -115,6 +115,74 @@ export function HueStrip({ hue, onChange }) {
115
115
  )
116
116
  }
117
117
 
118
+ /* the transparency checker behind an alpha ramp — literal greys, like the spectra (not themeable) */
119
+ const CHECKER = 'repeating-conic-gradient(#ccc 0% 25%, #fff 0% 50%) 0 0 / 8px 8px'
120
+
121
+ /**
122
+ * AlphaStrip — An opacity slider. HueStrip's twin for alpha: the colour ramped from transparent to
123
+ * opaque over a checker, same knob, same inset (ColorPickerPopover, kol-monitor 2026-10-17). Focus +
124
+ * Left/Right (or Down/Up) nudge alpha by 1% (role="slider").
125
+ *
126
+ * @param {number} alpha controlled alpha, 0–1
127
+ * @param {string} color the opaque colour the ramp ends on (any CSS colour)
128
+ * @param {Function} onChange (alpha: number) => void — fires on press, drag, and arrow keys
129
+ */
130
+ export function AlphaStrip({ alpha, color, onChange }) {
131
+ const ref = useRef(null)
132
+ const dragging = useRef(false)
133
+
134
+ const update = useCallback((clientX) => {
135
+ const node = ref.current
136
+ if (!node) return
137
+ const rect = node.getBoundingClientRect()
138
+ const inner = rect.width - 2 * HANDLE_R
139
+ if (inner <= 0) return
140
+ onChange(Math.max(0, Math.min(1, (clientX - rect.left - HANDLE_R) / inner)))
141
+ }, [onChange])
142
+
143
+ useEffect(() => {
144
+ const onMove = (e) => { if (dragging.current) update(e.clientX) }
145
+ const onUp = () => { dragging.current = false }
146
+ window.addEventListener('pointermove', onMove)
147
+ window.addEventListener('pointerup', onUp)
148
+ window.addEventListener('pointercancel', onUp)
149
+ return () => {
150
+ window.removeEventListener('pointermove', onMove)
151
+ window.removeEventListener('pointerup', onUp)
152
+ window.removeEventListener('pointercancel', onUp)
153
+ }
154
+ }, [update])
155
+
156
+ const onKeyDown = (e) => {
157
+ let next = null
158
+ if (e.key === 'ArrowRight' || e.key === 'ArrowUp') next = Math.min(1, alpha + 0.01)
159
+ else if (e.key === 'ArrowLeft' || e.key === 'ArrowDown') next = Math.max(0, alpha - 0.01)
160
+ if (next === null) return
161
+ e.preventDefault()
162
+ onChange(next)
163
+ }
164
+
165
+ return (
166
+ <div
167
+ ref={ref}
168
+ role="slider"
169
+ tabIndex={0}
170
+ aria-label="Opacity"
171
+ aria-valuemin={0}
172
+ aria-valuemax={100}
173
+ aria-valuenow={Math.round(alpha * 100)}
174
+ onPointerDown={(e) => { dragging.current = true; update(e.clientX); e.preventDefault() }}
175
+ onKeyDown={onKeyDown}
176
+ className="relative rounded-[var(--kol-radius-xs)] cursor-pointer touch-none"
177
+ style={{ height: 12, background: `linear-gradient(to right, transparent, ${color}), ${CHECKER}` }}
178
+ >
179
+ <div className="absolute inset-y-0" style={{ left: HANDLE_R, right: HANDLE_R }}>
180
+ <Handle left={`${alpha * 100}%`} top="50%" />
181
+ </div>
182
+ </div>
183
+ )
184
+ }
185
+
118
186
  /**
119
187
  * SBSquare — Pick saturation and value on a square. 2D saturation/value picker: white→hue horizontal gradient with
120
188
  * a black overlay fading upward, crosshair handle. Fills its container