Skip to content

Instantly share code, notes, and snippets.

@pkayokay
Last active October 23, 2024 19:15
Show Gist options
  • Save pkayokay/ed66aae19a7cea934d8816176f871163 to your computer and use it in GitHub Desktop.
Save pkayokay/ed66aae19a7cea934d8816176f871163 to your computer and use it in GitHub Desktop.
export const ColorInput = ({
id,
label,
value,
onChange,
}: {
id: string;
label: string;
description?: string;
placeholder?: string;
value?: string;
onChange: (value: string) => void;
}) => {
const colorRef = useRef<HTMLInputElement>(null);
const textRef = useRef<HTMLInputElement>(null);
const handleInputClick = () => {
colorRef?.current?.click();
};
return (
<>
<label htmlFor={id} className={FORM_LABEL_CLASSES}>
{label}
</label>
<div className="flex relative items-center">
<input
ref={colorRef}
id={id}
type="color"
value={value}
className="absolute left-2 top-[6px] border"
onChange={(e) => onChange(e.target.value)}
onFocus={() => {
textRef?.current?.focus();
textRef?.current?.click();
}}
/>
<input
ref={textRef}
id={id}
type="text"
className={clsx(FORM_INPUT_CLASSES, "!pl-10")}
value={value}
onKeyDown={handleInputClick}
onClick={handleInputClick}
onChange={(e) => onChange(e.target.value)}
/>
</div>
</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment