|
@@ -4,12 +4,31 @@ interface ColorInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
|
|
label: string
|
|
label: string
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export function ColorInput({ label, ...rest }: ColorInputProps) {
|
|
|
|
|
|
|
+export function ColorInput({ label, value, ...rest }: ColorInputProps) {
|
|
|
|
|
+ const ref = React.useRef<HTMLDivElement>(null)
|
|
|
|
|
+ const [computedValue, setComputedValue] = React.useState(value)
|
|
|
|
|
+
|
|
|
|
|
+ // TODO: listen to theme change?
|
|
|
|
|
+ React.useEffect(() => {
|
|
|
|
|
+ if (value?.toString().startsWith('var') && ref.current) {
|
|
|
|
|
+ const varName = /var\((.*)\)/.exec(value.toString())?.[1]
|
|
|
|
|
+ if (varName) {
|
|
|
|
|
+ setComputedValue(getComputedStyle(ref.current).getPropertyValue(varName).trim())
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }, [value])
|
|
|
|
|
+
|
|
|
return (
|
|
return (
|
|
|
- <div className="input">
|
|
|
|
|
|
|
+ <div className="input" ref={ref}>
|
|
|
<label htmlFor={`color-${label}`}>{label}</label>
|
|
<label htmlFor={`color-${label}`}>{label}</label>
|
|
|
<div className="color-input-wrapper">
|
|
<div className="color-input-wrapper">
|
|
|
- <input className="color-input" name={`color-${label}`} type="color" {...rest} />
|
|
|
|
|
|
|
+ <input
|
|
|
|
|
+ className="color-input"
|
|
|
|
|
+ name={`color-${label}`}
|
|
|
|
|
+ type="color"
|
|
|
|
|
+ value={computedValue}
|
|
|
|
|
+ {...rest}
|
|
|
|
|
+ />
|
|
|
</div>
|
|
</div>
|
|
|
</div>
|
|
</div>
|
|
|
)
|
|
)
|