/* eslint-disable react/no-array-index-key */
/* eslint-disable react/jsx-no-leaked-render */
/* eslint-disable jsx-a11y/no-static-element-interactions */
import React, { useState, useEffect } from 'react'
import { getHandleValue } from '../utils/utils.js'
import { usePicker } from '../context.js'
import { low, high } from '../utils/formatters.js'
import { GradientProps } from '../shared/types.js'
export const Handle = ({
left,
i,
setDragging,
}: {
left?: number
i: number
setDragging: (arg0: boolean) => void
}) => {
const {
colors,
squareWidth,
selectedColor,
defaultStyles,
pickerIdSuffix,
createGradientStr,
} = usePicker()
const isSelected = selectedColor === i
const leftMultiplyer = (squareWidth - 18) / 100
const setSelectedColor = (index: number) => {
const newGradStr = colors?.map((cc: GradientProps, i: number) => ({
...cc,
value: i === index ? high(cc) : low(cc),
}))
createGradientStr(newGradStr)
}
const handleDown = (e: any) => {
e.stopPropagation()
setSelectedColor(i)
setDragging(true)
}
// const handleFocus = () => {
// setInFocus('gpoint')
// setSelectedColor(i)
// }
// const handleBlur = () => {
// setInFocus(null)
// }
return (
handleDown(e)}
id={`rbgcp-gradient-handle-${i}${pickerIdSuffix}`}
// className="rbgcp-gradient-handle-wrap"
style={{
...defaultStyles.rbgcpGradientHandleWrap,
left: (left ?? 0) * leftMultiplyer,
}}
>
)
}
const GradientBar = () => {
const {
value,
colors,
config,
squareWidth,
currentColor,
handleGradient,
pickerIdSuffix,
createGradientStr,
} = usePicker()
const { barSize } = config
const [dragging, setDragging] = useState(false)
// const [inFocus, setInFocus] = useState(null)
function force90degLinear(color: string) {
return color.replace(
/(radial|linear)-gradient\([^,]+,/,
'linear-gradient(90deg,'
)
}
const addPoint = (e: any) => {
const left = getHandleValue(e, barSize)
const newColors = [
...colors.map((c: any) => ({ ...c, value: low(c) })),
{ value: currentColor, left: left },
]?.sort((a, b) => a.left - b.left)
createGradientStr(newColors)
}
// useEffect(() => {
// const selectedEl = window?.document?.getElementById(
// `gradient-handle-${selectedColor}`
// )
// if (selectedEl) selectedEl.focus()
// }, [selectedColor])
const stopDragging = () => {
setDragging(false)
}
const handleDown = (e: any) => {
if (dragging) return;
addPoint(e)
setDragging(true)
}
const handleMove = (e: any) => {
if (dragging) handleGradient(currentColor, getHandleValue(e, barSize))
}
// const handleKeyboard = (e: any) => {
// if (isGradient) {
// if (e.keyCode === 8) {
// if (inFocus === 'gpoint') {
// deletePoint()
// }
// }
// }
// }
const handleUp = () => {
stopDragging()
}
useEffect(() => {
window.addEventListener('mouseup', handleUp)
// window?.addEventListener('keydown', handleKeyboard)
return () => {
window.removeEventListener('mouseup', handleUp)
// window?.removeEventListener('keydown', handleKeyboard)
}
})
return (
handleDown(e)}
onMouseMove={(e) => handleMove(e)}
id={`rbgcp-gradient-bar-canvas${pickerIdSuffix}`}
// className="rbgcp-gradient-bar-canvas"
/>
{colors?.map((c: any, i) => (
))}
)
}
export default GradientBar