|
| 1 | +import { useEffect, useState } from 'react' |
| 2 | +import { filterHexInput, hexToRgb, parseColor, rgbToHex } from '../utils/color-utils' |
| 3 | + |
| 4 | +interface UseColorPickerStateProps { |
| 5 | + value: string |
| 6 | + onChange?: (color: string) => void |
| 7 | + onOpacityChange?: (opacity: number) => void |
| 8 | +} |
| 9 | + |
| 10 | +export const useColorPickerState = ({ |
| 11 | + value, |
| 12 | + onChange, |
| 13 | + onOpacityChange, |
| 14 | +}: UseColorPickerStateProps) => { |
| 15 | + const { hex, opacity } = parseColor(value) |
| 16 | + const [color, setColor] = useState(value) |
| 17 | + const [inputValue, setInputValue] = useState(hex) |
| 18 | + const [livePreviewColor, setLivePreviewColor] = useState(value) |
| 19 | + const [colorType, setColorType] = useState<'color' | 'gradient'>('color') |
| 20 | + const [opacityValue, setOpacityValue] = useState(isNaN(opacity) ? 100 : opacity) |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + const { hex: newHex, opacity: newOpacity } = parseColor(value) |
| 24 | + setInputValue(newHex) |
| 25 | + setColor(value) |
| 26 | + setLivePreviewColor(value) |
| 27 | + setOpacityValue(isNaN(newOpacity) ? 100 : newOpacity) |
| 28 | + }, [value]) |
| 29 | + |
| 30 | + const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 31 | + const finalValue = filterHexInput(e.target.value) |
| 32 | + setInputValue(finalValue) |
| 33 | + |
| 34 | + if (finalValue.length === 3 || finalValue.length === 6) { |
| 35 | + const newHex = `#${finalValue}` |
| 36 | + const rgb = hexToRgb(newHex) |
| 37 | + if (rgb) { |
| 38 | + const newRgbaColor = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${ |
| 39 | + opacityValue / 100 |
| 40 | + })` |
| 41 | + setLivePreviewColor(newRgbaColor) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + const handleInputFocus = () => { |
| 47 | + if (inputValue.startsWith('#')) { |
| 48 | + setInputValue(inputValue.substring(1)) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + const handleHexChange = () => { |
| 53 | + if (hex === 'Mixed') return |
| 54 | + |
| 55 | + let newHex = inputValue.trim() |
| 56 | + if (!newHex.startsWith('#')) { |
| 57 | + newHex = `#${newHex}` |
| 58 | + } |
| 59 | + |
| 60 | + const rgb = hexToRgb(newHex) |
| 61 | + |
| 62 | + if (rgb) { |
| 63 | + const newRgbaColor = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${ |
| 64 | + opacityValue / 100 |
| 65 | + })` |
| 66 | + onChange?.(newRgbaColor) |
| 67 | + setInputValue(newHex.toUpperCase()) |
| 68 | + } else { |
| 69 | + const { hex: originalHex } = parseColor(value) |
| 70 | + setInputValue(originalHex) |
| 71 | + setLivePreviewColor(value) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + const handleOpacityChange = (newOpacity: number) => { |
| 76 | + // Проверка на валидность значения |
| 77 | + const validOpacity = isNaN(newOpacity) ? 100 : newOpacity |
| 78 | + onOpacityChange?.(validOpacity) |
| 79 | + setOpacityValue(validOpacity) |
| 80 | + |
| 81 | + if (color.includes('gradient')) { |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + const rgbMatch = color.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/) |
| 86 | + if (rgbMatch) { |
| 87 | + const r = rgbMatch[1] |
| 88 | + const g = rgbMatch[2] |
| 89 | + const b = rgbMatch[3] |
| 90 | + const newRgbaColor = `rgba(${r}, ${g}, ${b}, ${validOpacity / 100})` |
| 91 | + setColor(newRgbaColor) |
| 92 | + onChange?.(newRgbaColor) |
| 93 | + } else { |
| 94 | + const rgb = hexToRgb(color) |
| 95 | + if (rgb) { |
| 96 | + const newRgbaColor = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${ |
| 97 | + validOpacity / 100 |
| 98 | + })` |
| 99 | + setColor(newRgbaColor) |
| 100 | + onChange?.(newRgbaColor) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + const handleColorChange = (newColor: string) => { |
| 106 | + onChange?.(newColor) |
| 107 | + setColor(newColor) |
| 108 | + |
| 109 | + if (newColor.includes('gradient')) { |
| 110 | + setLivePreviewColor(newColor) |
| 111 | + |
| 112 | + if (colorType === 'color') { |
| 113 | + setColorType('gradient') |
| 114 | + } |
| 115 | + |
| 116 | + if (newColor.includes('linear-gradient')) { |
| 117 | + setInputValue('Linear') |
| 118 | + } else if (newColor.includes('radial-gradient')) { |
| 119 | + setInputValue('Radial') |
| 120 | + } else { |
| 121 | + setInputValue('Gradient') |
| 122 | + } |
| 123 | + |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + if (colorType === 'gradient') { |
| 128 | + setColorType('color') |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + const rgbaMatch = newColor.match( |
| 133 | + /rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/ |
| 134 | + ) |
| 135 | + if (rgbaMatch) { |
| 136 | + const alpha = rgbaMatch[4] !== undefined ? parseFloat(rgbaMatch[4]) : 1 |
| 137 | + const newOpacity = Math.round(alpha * 100) |
| 138 | + setOpacityValue(isNaN(newOpacity) ? 100 : newOpacity) |
| 139 | + } |
| 140 | + |
| 141 | + setLivePreviewColor(newColor) |
| 142 | + setInputValue(rgbToHex(newColor)) |
| 143 | + } |
| 144 | + |
| 145 | + return { |
| 146 | + hex, |
| 147 | + color, |
| 148 | + inputValue, |
| 149 | + livePreviewColor, |
| 150 | + colorType, |
| 151 | + opacityValue, |
| 152 | + handleInputChange, |
| 153 | + handleInputFocus, |
| 154 | + handleHexChange, |
| 155 | + handleOpacityChange, |
| 156 | + handleColorChange, |
| 157 | + } |
| 158 | +} |
0 commit comments