UNPKG

@wordpress/components

Version:
129 lines (128 loc) • 4.11 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let react_colorful = require("react-colorful"); let colord = require("colord"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/components/src/color-picker/picker.tsx function toHsva(hsla) { return { ...(0, colord.colord)(hsla).toHsv(), h: hsla.h, a: hsla.a }; } function isSameHsla(a, b) { return a.h === b.h && a.s === b.s && a.l === b.l && a.a === b.a; } /** * Convert parent HSLA into HSVA for prop sync. * * At black, RGB round-trips collapse saturation to 0. Preserve the native * HSVA saturation coordinate unless the HSL saturation channel itself * changed (sibling hue/alpha edits must not snap the pointer). * * At white, only `v: 100, s: 0` is visually white — preserving a prior * chromatic saturation would leave the pointer at the top-right and make * the first keyboard step jump from white to a saturated color. */ function toHsvaFromHsla(hsla, prevHsva, prevHsla) { const converted = toHsva(hsla); if (hsla.l !== 0) return converted; const saturationChanged = prevHsla !== null && prevHsla.s !== hsla.s; return { h: hsla.h, s: saturationChanged ? hsla.s : prevHsva.s, v: 0, a: hsla.a }; } /** * Visual color surface. * * Uses HSVA (react-colorful's native model) and keeps that value in local * state so HSLA↔hex round-trips cannot move the pointer * Parent ColorPicker still speaks HSLA for * inputs and controlled value sync; conversion happens only at the boundary. * * Prop sync from parent HSLA is suppressed for: * - pointer/touch drags (interaction flag), and * - any picker-originated update including keyboard (HSLA origin token). * * Achromatic HSL sibling edits (hue/alpha while at black) preserve native * HSVA saturation unless saturation itself changed. White always syncs to * the converted `s: 0` visual coordinate. */ const Picker = ({ hsla, enableAlpha, onChange, onInteractionStart, onInteractionEnd }) => { const [hsva, setHsva] = (0, _wordpress_element.useState)(() => toHsva(hsla)); const pickerOriginHslaRef = (0, _wordpress_element.useRef)(null); const prevHslaRef = (0, _wordpress_element.useRef)(hsla); const isPointerInteractingRef = (0, _wordpress_element.useRef)(false); (0, _wordpress_element.useEffect)(() => { if (isPointerInteractingRef.current) { prevHslaRef.current = hsla; return; } if (pickerOriginHslaRef.current && isSameHsla(pickerOriginHslaRef.current, hsla)) { prevHslaRef.current = hsla; return; } pickerOriginHslaRef.current = null; setHsva((prev) => toHsvaFromHsla(hsla, prev, prevHslaRef.current)); prevHslaRef.current = hsla; }, [hsla]); const pointerCaptureProps = { onPointerDown({ currentTarget, pointerId }) { isPointerInteractingRef.current = true; onInteractionStart?.(); currentTarget.setPointerCapture(pointerId); }, onPointerUp({ currentTarget, pointerId }) { currentTarget.releasePointerCapture(pointerId); isPointerInteractingRef.current = false; onInteractionEnd?.(); }, onPointerCancel({ currentTarget, pointerId }) { currentTarget.releasePointerCapture(pointerId); isPointerInteractingRef.current = false; onInteractionEnd?.(); } }; const handleChange = (next) => { setHsva(next); const nextHsla = next.h === hsva.h && next.s === hsva.s && next.v === hsva.v ? { ...hsla, a: next.a } : { ...(0, colord.colord)(next).toHsl(), a: next.a }; pickerOriginHslaRef.current = nextHsla; onChange(nextHsla); }; if (enableAlpha) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_colorful.HsvaColorPicker, { color: hsva, onChange: handleChange, ...pointerCaptureProps }); const hsv = { h: hsva.h, s: hsva.s, v: hsva.v }; return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_colorful.HsvColorPicker, { color: hsv, onChange: (next) => { handleChange({ ...next, a: hsva.a }); }, ...pointerCaptureProps }); }; exports.Picker = Picker; //# sourceMappingURL=picker.cjs.map