@wordpress/components
Version:
UI components for WordPress.
1 lines • 7.8 kB
Source Map (JSON)
{"version":3,"file":"picker.cjs","names":["useState","useRef","HsvaColorPicker","HsvColorPicker"],"sources":["../../src/color-picker/picker.tsx"],"sourcesContent":["import type { PointerEvent } from 'react';\nimport { useEffect, useRef, useState } from '@wordpress/element';\nimport { HsvColorPicker, HsvaColorPicker } from 'react-colorful';\nimport { colord } from 'colord';\nimport type { HslaColor, HsvaColor } from 'react-colorful';\nimport type { PickerProps } from './types';\n\nfunction toHsva( hsla: PickerProps[ 'hsla' ] ): HsvaColor {\n\treturn {\n\t\t...colord( hsla ).toHsv(),\n\t\t// HSL and HSV share the hue angle. Color conversion collapses achromatic\n\t\t// hue to 0, but HSLA retains the user's latent hue.\n\t\th: hsla.h,\n\t\ta: hsla.a,\n\t};\n}\n\nfunction isSameHsla( a: HslaColor, b: HslaColor ): boolean {\n\treturn a.h === b.h && a.s === b.s && a.l === b.l && a.a === b.a;\n}\n\n/**\n * Convert parent HSLA into HSVA for prop sync.\n *\n * At black, RGB round-trips collapse saturation to 0. Preserve the native\n * HSVA saturation coordinate unless the HSL saturation channel itself\n * changed (sibling hue/alpha edits must not snap the pointer).\n *\n * At white, only `v: 100, s: 0` is visually white — preserving a prior\n * chromatic saturation would leave the pointer at the top-right and make\n * the first keyboard step jump from white to a saturated color.\n */\nfunction toHsvaFromHsla(\n\thsla: HslaColor,\n\tprevHsva: HsvaColor,\n\tprevHsla: HslaColor | null\n): HsvaColor {\n\tconst converted = toHsva( hsla );\n\n\t// White and chromatic colors use the converted value as-is.\n\tif ( hsla.l !== 0 ) {\n\t\treturn converted;\n\t}\n\n\tconst saturationChanged = prevHsla !== null && prevHsla.s !== hsla.s;\n\n\treturn {\n\t\th: hsla.h,\n\t\ts: saturationChanged ? hsla.s : prevHsva.s,\n\t\tv: 0,\n\t\ta: hsla.a,\n\t};\n}\n\n/**\n * Visual color surface.\n *\n * Uses HSVA (react-colorful's native model) and keeps that value in local\n * state so HSLA↔hex round-trips cannot move the pointer\n * Parent ColorPicker still speaks HSLA for\n * inputs and controlled value sync; conversion happens only at the boundary.\n *\n * Prop sync from parent HSLA is suppressed for:\n * - pointer/touch drags (interaction flag), and\n * - any picker-originated update including keyboard (HSLA origin token).\n *\n * Achromatic HSL sibling edits (hue/alpha while at black) preserve native\n * HSVA saturation unless saturation itself changed. White always syncs to\n * the converted `s: 0` visual coordinate.\n */\nexport const Picker = ( {\n\thsla,\n\tenableAlpha,\n\tonChange,\n\tonInteractionStart,\n\tonInteractionEnd,\n}: PickerProps ) => {\n\tconst [ hsva, setHsva ] = useState< HsvaColor >( () => toHsva( hsla ) );\n\t// Last HSLA emitted by this picker — skip echoing it back into HSVA.\n\tconst pickerOriginHslaRef = useRef< HslaColor | null >( null );\n\t// Previous parent HSLA — detect which channel changed on achromatic sync.\n\tconst prevHslaRef = useRef< HslaColor >( hsla );\n\t// Pointer/touch drag: never sync from HSLA mid-gesture (origin match alone\n\t// can fail across rapid frames when parent gradient state re-renders).\n\tconst isPointerInteractingRef = useRef( false );\n\n\tuseEffect( () => {\n\t\tif ( isPointerInteractingRef.current ) {\n\t\t\t// Keep prevHsla in lockstep with parent updates during the gesture\n\t\t\t// so a post-drag HSL sibling edit is not compared against pre-drag\n\t\t\t// HSLA and mistaken for a saturation change.\n\t\t\tprevHslaRef.current = hsla;\n\t\t\treturn;\n\t\t}\n\t\tif (\n\t\t\tpickerOriginHslaRef.current &&\n\t\t\tisSameHsla( pickerOriginHslaRef.current, hsla )\n\t\t) {\n\t\t\tprevHslaRef.current = hsla;\n\t\t\treturn;\n\t\t}\n\t\tpickerOriginHslaRef.current = null;\n\t\tsetHsva( ( prev ) =>\n\t\t\ttoHsvaFromHsla( hsla, prev, prevHslaRef.current )\n\t\t);\n\t\tprevHslaRef.current = hsla;\n\t}, [ hsla ] );\n\n\t// Inline handlers so refs are not passed into a helper during render\n\t// (react-hooks/refs). Pointer capture also keeps drag working over iframes.\n\tconst pointerCaptureProps = {\n\t\tonPointerDown( { currentTarget, pointerId }: PointerEvent ) {\n\t\t\tisPointerInteractingRef.current = true;\n\t\t\tonInteractionStart?.();\n\t\t\tcurrentTarget.setPointerCapture( pointerId );\n\t\t},\n\t\tonPointerUp( { currentTarget, pointerId }: PointerEvent ) {\n\t\t\tcurrentTarget.releasePointerCapture( pointerId );\n\t\t\tisPointerInteractingRef.current = false;\n\t\t\tonInteractionEnd?.();\n\t\t},\n\t\tonPointerCancel( { currentTarget, pointerId }: PointerEvent ) {\n\t\t\tcurrentTarget.releasePointerCapture( pointerId );\n\t\t\tisPointerInteractingRef.current = false;\n\t\t\tonInteractionEnd?.();\n\t\t},\n\t};\n\n\tconst handleChange = ( next: HsvaColor ) => {\n\t\tsetHsva( next );\n\t\t// An alpha-only change keeps the current HSLA channels. Converting the\n\t\t// HSVA to HSLA again rounds each channel and shifts the RGB values.\n\t\tconst isAlphaOnlyChange =\n\t\t\tnext.h === hsva.h && next.s === hsva.s && next.v === hsva.v;\n\t\tconst nextHsla: HslaColor = isAlphaOnlyChange\n\t\t\t? { ...hsla, a: next.a }\n\t\t\t: { ...colord( next ).toHsl(), a: next.a };\n\t\tpickerOriginHslaRef.current = nextHsla;\n\t\tonChange( nextHsla );\n\t};\n\n\tif ( enableAlpha ) {\n\t\treturn (\n\t\t\t<HsvaColorPicker\n\t\t\t\tcolor={ hsva }\n\t\t\t\tonChange={ handleChange }\n\t\t\t\t{ ...pointerCaptureProps }\n\t\t\t/>\n\t\t);\n\t}\n\n\t// HsvColorPicker's equality checks enumerate own keys — never pass `a`,\n\t// or every parent re-render looks like an external color change and the\n\t// pointer jitters while dragging.\n\tconst hsv = { h: hsva.h, s: hsva.s, v: hsva.v };\n\n\treturn (\n\t\t<HsvColorPicker\n\t\t\tcolor={ hsv }\n\t\t\tonChange={ ( next ) => {\n\t\t\t\thandleChange( { ...next, a: hsva.a } );\n\t\t\t} }\n\t\t\t{ ...pointerCaptureProps }\n\t\t/>\n\t);\n};\n"],"mappings":";;;;;;;;;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