import React, { useRef, useState, useEffect } from 'react' import { usePicker } from '../context.js' import usePaintHue from '../hooks/usePaintHue.js' import { getHandleValue } from '../utils/utils.js' import tinycolor from 'tinycolor2' const Hue = () => { const barRef = useRef(null) const { config, handleChange, squareWidth, hc, setHc, pickerIdSuffix } = usePicker() const [dragging, setDragging] = useState(false) const { barSize } = config usePaintHue(barRef, squareWidth) const stopDragging = () => { setDragging(false) } const handleDown = () => { setDragging(true) } const handleHue = (e: any) => { const newHue = getHandleValue(e, barSize) * 3.6 const tinyHsv = tinycolor({ h: newHue, s: hc?.s, v: hc?.v }) const { r, g, b } = tinyHsv.toRgb() handleChange(`rgba(${r}, ${g}, ${b}, ${hc.a})`) setHc({ ...hc, h: newHue }) } const handleMove = (e: any) => { if (dragging) { handleHue(e) } } const handleClick = (e: any) => { if (!dragging) { handleHue(e) } } useEffect(() => { const handleUp = () => { stopDragging() } window.addEventListener('mouseup', handleUp) return () => { window.removeEventListener('mouseup', handleUp) } }, []) return (
handleMove(e)} id={`rbgcp-hue-wrap${pickerIdSuffix}`} // className="rbgcp-hue-wrap" >
handleClick(e)} id={`rbgcp-hue-bar${pickerIdSuffix}`} style={{ borderRadius: 14, position: 'relative', verticalAlign: 'top', }} />
) } export default Hue