1+ import { useEffect , useMemo , useState } from "react" ;
12import tw , { styled } from "twin.macro" ;
23
34import { PRESET_COLORS } from "~/common/constants" ;
@@ -24,7 +25,7 @@ const Button = styled.button<ButtonProps>`
2425` ;
2526
2627const ColorInput = styled ( Input ) `
27- ${ tw `w-32` }
28+ ${ tw `font-mono w-32` }
2829` ;
2930
3031export interface ColorPickerProps {
@@ -36,6 +37,18 @@ export interface ColorPickerProps {
3637}
3738
3839function ColorPicker ( props : ColorPickerProps ) {
40+ const [ inputValue , setInputValue ] = useState ( "" ) ;
41+
42+ const computedValue = useMemo ( ( ) => inputValue || props . value , [ inputValue , props . value ] ) ;
43+ const isValid = useMemo ( ( ) => CSS . supports ( "color" , computedValue ) , [ computedValue ] ) ;
44+
45+ useEffect ( ( ) => setInputValue ( "" ) , [ props . value ] ) ;
46+ useEffect ( ( ) => {
47+ if ( isValid && inputValue . length > 0 ) {
48+ props . onChange ( inputValue ) ;
49+ }
50+ } , [ isValid , inputValue ] ) ;
51+
3952 return (
4053 < Wrapper className = { props . className } disabled = { props . disabled } >
4154 { PRESET_COLORS . map ( ( color , index ) => (
@@ -48,7 +61,7 @@ function ColorPicker(props: ColorPickerProps) {
4861 />
4962 ) ) }
5063
51- < ColorInput value = { props . value } onChange = { props . onChange } />
64+ < ColorInput error = { ! isValid } value = { computedValue } onChange = { setInputValue } />
5265 </ Wrapper >
5366 ) ;
5467}
0 commit comments