@wordpress/components
Version:
UI components for WordPress.
124 lines (123 loc) • 3.77 kB
JavaScript
import { useEffect, useRef, useState } from "@wordpress/element";
import { HsvColorPicker, HsvaColorPicker } from "react-colorful";
import { colord } from "colord";
import { jsx } from "react/jsx-runtime";
// packages/components/src/color-picker/picker.tsx
function toHsva(hsla) {
return {
...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] = useState(() => toHsva(hsla));
const pickerOriginHslaRef = useRef(null);
const prevHslaRef = useRef(hsla);
const isPointerInteractingRef = useRef(false);
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
} : {
...colord(next).toHsl(),
a: next.a
};
pickerOriginHslaRef.current = nextHsla;
onChange(nextHsla);
};
if (enableAlpha) return /* @__PURE__ */ jsx(HsvaColorPicker, {
color: hsva,
onChange: handleChange,
...pointerCaptureProps
});
const hsv = {
h: hsva.h,
s: hsva.s,
v: hsva.v
};
return /* @__PURE__ */ jsx(HsvColorPicker, {
color: hsv,
onChange: (next) => {
handleChange({
...next,
a: hsva.a
});
},
...pointerCaptureProps
});
};
export { Picker };
//# sourceMappingURL=picker.mjs.map