import React, { useState, useRef, useEffect } from 'react' import { Styles, Config } from '../shared/types.js' import { getHandleValue } from '../utils/utils.js' import { usePicker } from '../context.js' import { usePaintSat, usePaintLight, usePaintBright, } from '../hooks/usePaintHue.js' import tinycolor from 'tinycolor2' const AdvBar = ({ value, reffy, label, config, callback, squareWidth, openAdvanced, defaultStyles, pickerIdSuffix, }: { reffy: any value: number label: string config: Config squareWidth: number openAdvanced: boolean defaultStyles: Styles pickerIdSuffix: string callback: (arg0: number) => void }) => { const { barSize } = config const [dragging, setDragging] = useState(false) const [handleTop, setHandleTop] = useState(2) const left = value * (squareWidth - 18) useEffect(() => { setHandleTop(reffy?.current?.offsetTop - 2) }, [openAdvanced, reffy]) const stopDragging = () => { setDragging(false) } const handleMove = (e: any) => { if (dragging) { callback(getHandleValue(e, barSize)) } } const handleClick = (e: any) => { if (!dragging) { callback(getHandleValue(e, barSize)) } } const handleDown = () => { setDragging(true) } useEffect(() => { const handleUp = () => { stopDragging() } window.addEventListener('mouseup', handleUp) return () => { window.removeEventListener('mouseup', handleUp) } }, []) return (
handleMove(e)} // className="rbgcp-advanced-bar-wrap" style={{ cursor: 'resize', position: 'relative' }} id={`rbgcp-advanced-bar-${label}-wrapper${pickerIdSuffix}`} >
handleMove(e)} onClick={(e) => handleClick(e)} tabIndex={0} role="button" onKeyDown={() => { return }} > {label}
handleClick(e)} // className="rbgcp-advanced-bar-canvas" style={{ position: 'relative', borderRadius: 14 }} id={`rbgcp-advanced-bar-${label}-canvas${pickerIdSuffix}`} />
) } const AdvancedControls = ({ openAdvanced }: { openAdvanced: boolean }) => { const { config, tinyColor, handleChange, squareWidth, hc, defaultStyles, pickerIdSuffix } = usePicker() const { s, l } = tinyColor.toHsl() const satRef = useRef(null) const lightRef = useRef(null) const brightRef = useRef(null) usePaintSat(satRef, hc?.h, l * 100, squareWidth) usePaintLight(lightRef, hc?.h, s * 100, squareWidth) usePaintBright(brightRef, hc?.h, s * 100, squareWidth) const satDesat = (value: number) => { const { r, g, b } = tinycolor({ h: hc?.h, s: value / 100, l }).toRgb() handleChange(`rgba(${r},${g},${b},${hc?.a})`) } const setLight = (value: number) => { const { r, g, b } = tinycolor({ h: hc?.h, s, l: value / 100 }).toRgb() handleChange(`rgba(${r},${g},${b},${hc?.a})`) } const setBright = (value: number) => { const { r, g, b } = tinycolor({ h: hc?.h, s: hc?.s * 100, v: value, }).toRgb() handleChange(`rgba(${r},${g},${b},${hc?.a})`) } return (
) } export default AdvancedControls