|
| 1 | +import * as React from 'react' |
| 2 | +import { hexToHsva, hsvaToHex } from '@uiw/color-convert' |
| 3 | +import ShadeSlider from '@uiw/react-color-shade-slider' |
| 4 | +import Wheel from '@uiw/react-color-wheel' |
| 5 | +import { useOnClickOutside } from '@/utils/hooks/useOnClickOutside' |
| 6 | +import './slider.css' |
| 7 | + |
| 8 | +type ColorInputPropsT = { |
| 9 | + defaultValue: number |
| 10 | + setValue: any |
| 11 | +} & React.DetailedHTMLProps< |
| 12 | + React.InputHTMLAttributes<HTMLInputElement>, |
| 13 | + HTMLInputElement |
| 14 | +> |
| 15 | + |
| 16 | +export function ColorInput({ |
| 17 | + defaultValue, |
| 18 | + setValue, |
| 19 | +}: ColorInputPropsT): JSX.Element { |
| 20 | + const [sharedValue, setSharedValue] = React.useState<any>(defaultValue) |
| 21 | + const [isClicked, setIsClicked] = React.useState<boolean>(false) |
| 22 | + const colorPickerRef = React.useRef<HTMLDivElement>(null) |
| 23 | + const triggerRef = React.useRef<HTMLDivElement>(null) |
| 24 | + |
| 25 | + // React.useEffect(() => { |
| 26 | + // setSharedValue(defaultValue) // init once with the passed value (from search params) |
| 27 | + // }, []) |
| 28 | + |
| 29 | + // React.useEffect(() => { |
| 30 | + // setValue(sharedValue) |
| 31 | + // }, [sharedValue]) |
| 32 | + |
| 33 | + React.useEffect(() => { |
| 34 | + setSharedValue(defaultValue) // init once with the passed value (from search params) |
| 35 | + }, []) |
| 36 | + |
| 37 | + React.useEffect(() => { |
| 38 | + setValue(sharedValue) |
| 39 | + }, [sharedValue]) |
| 40 | + |
| 41 | + React.useEffect(() => { |
| 42 | + setSharedValue(defaultValue) |
| 43 | + }, [defaultValue]) |
| 44 | + |
| 45 | + React.useEffect(() => { |
| 46 | + const observer = new IntersectionObserver( |
| 47 | + ([entry]) => { |
| 48 | + if (!entry.isIntersecting && isClicked) { |
| 49 | + setIsClicked(false) |
| 50 | + } |
| 51 | + }, |
| 52 | + { threshold: 0.5 } // Trigger when any part of the element is not visible |
| 53 | + ) |
| 54 | + |
| 55 | + if (triggerRef.current) { |
| 56 | + observer.observe(triggerRef.current) |
| 57 | + } |
| 58 | + |
| 59 | + return () => { |
| 60 | + if (triggerRef.current) { |
| 61 | + observer.unobserve(triggerRef.current) |
| 62 | + } |
| 63 | + } |
| 64 | + }, [isClicked]) |
| 65 | + |
| 66 | + const updateColorWheelPosition = React.useCallback(() => { |
| 67 | + if (isClicked && colorPickerRef.current && triggerRef.current) { |
| 68 | + const triggerRect = triggerRef.current.getBoundingClientRect() |
| 69 | + const colorWheelRect = colorPickerRef.current.getBoundingClientRect() |
| 70 | + |
| 71 | + // Center horizontally relative to trigger |
| 72 | + const left = |
| 73 | + triggerRect.left + triggerRect.width / 2 - colorWheelRect.width / 2 |
| 74 | + // Position above trigger with 20px gap |
| 75 | + const top = triggerRect.top - colorWheelRect.height - 5 |
| 76 | + |
| 77 | + colorPickerRef.current.style.left = `${left}px` |
| 78 | + colorPickerRef.current.style.top = `${top}px` |
| 79 | + } |
| 80 | + }, [isClicked]) |
| 81 | + |
| 82 | + useOnClickOutside(colorPickerRef, () => setIsClicked(false)) |
| 83 | + |
| 84 | + React.useEffect(() => { |
| 85 | + updateColorWheelPosition() |
| 86 | + |
| 87 | + // Add scroll event listener to update position |
| 88 | + const handleScroll = () => { |
| 89 | + updateColorWheelPosition() |
| 90 | + } |
| 91 | + |
| 92 | + if (isClicked) { |
| 93 | + window.addEventListener('scroll', handleScroll, true) // true for capture phase |
| 94 | + } |
| 95 | + |
| 96 | + return () => { |
| 97 | + window.removeEventListener('scroll', handleScroll, true) |
| 98 | + } |
| 99 | + }, [isClicked, updateColorWheelPosition]) |
| 100 | + |
| 101 | + return ( |
| 102 | + <div className='flex items-center w-full h-full flex-row gap-2'> |
| 103 | + <div className='flex items-center gap-2 w-full relative h-full'> |
| 104 | + <div |
| 105 | + ref={triggerRef} |
| 106 | + className='w-full h-[26px] rounded-md cursor-pointer' |
| 107 | + style={{ |
| 108 | + background: sharedValue, |
| 109 | + border: |
| 110 | + sharedValue === '#ffffff' |
| 111 | + ? '1px solid #F2F2F2' |
| 112 | + : '0px solid transparent', |
| 113 | + }} |
| 114 | + onClick={() => { |
| 115 | + setIsClicked(!isClicked) |
| 116 | + }} |
| 117 | + ></div> |
| 118 | + |
| 119 | + {/* color control */} |
| 120 | + <div |
| 121 | + ref={colorPickerRef} |
| 122 | + id='colorwheel' |
| 123 | + style={{ |
| 124 | + width: 'fit-content', |
| 125 | + height: 'fit-content', |
| 126 | + position: 'fixed', |
| 127 | + zIndex: 100, |
| 128 | + display: isClicked === true ? 'block' : 'none', |
| 129 | + }} |
| 130 | + > |
| 131 | + <div |
| 132 | + style={{ |
| 133 | + display: 'flex', |
| 134 | + width: 'fit-content', |
| 135 | + height: 'fit-content', |
| 136 | + background: 'white', |
| 137 | + padding: 16, |
| 138 | + flexDirection: 'column', |
| 139 | + justifyContent: 'center', |
| 140 | + alignItems: 'center', |
| 141 | + gap: 16, |
| 142 | + borderRadius: 5, |
| 143 | + filter: 'drop-shadow(0px 0px 10px rgba(0,0,0,0.10))', |
| 144 | + }} |
| 145 | + > |
| 146 | + <Wheel |
| 147 | + color={sharedValue} |
| 148 | + onChange={(color) => { |
| 149 | + setSharedValue(color.hex) |
| 150 | + }} |
| 151 | + width={200} |
| 152 | + height={200} |
| 153 | + ></Wheel> |
| 154 | + <ShadeSlider |
| 155 | + width={200} |
| 156 | + radius={4} |
| 157 | + style={{ display: 'flex', alignItems: 'center' }} |
| 158 | + hsva={hexToHsva(sharedValue)} |
| 159 | + onChange={(color) => { |
| 160 | + setSharedValue( |
| 161 | + hsvaToHex({ |
| 162 | + h: hexToHsva(sharedValue).h, |
| 163 | + // @ts-ignore |
| 164 | + s: color.s, |
| 165 | + v: color.v, |
| 166 | + a: 1, |
| 167 | + }) |
| 168 | + ) |
| 169 | + }} |
| 170 | + /> |
| 171 | + <div |
| 172 | + style={{ |
| 173 | + width: 16, |
| 174 | + height: 16, |
| 175 | + background: 'white', |
| 176 | + position: 'absolute', |
| 177 | + borderRadius: 3, |
| 178 | + bottom: -5, |
| 179 | + transform: 'rotate(45deg)', |
| 180 | + }} |
| 181 | + ></div> |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + </div> |
| 185 | + <input |
| 186 | + type='text' |
| 187 | + value={sharedValue} |
| 188 | + onChange={(e) => setSharedValue(e.target.value)} |
| 189 | + className='w-[84px] h-[26px] outline-none text-center bg-[#F2F2F2] rounded-md flex items-center justify-center' |
| 190 | + /> |
| 191 | + </div> |
| 192 | + ) |
| 193 | +} |
0 commit comments