2021-12-12 16:50:58 +00:00
|
|
|
import React, { FormEventHandler } from "react";
|
2021-12-13 22:15:11 +00:00
|
|
|
import Palette, { palettes } from "./Palette";
|
2021-12-15 19:05:25 +00:00
|
|
|
import PalettePreview from "./PalettePreview";
|
2021-12-15 17:46:05 +00:00
|
|
|
import PaletteSelect from "./PaletteSelect";
|
2021-12-12 16:50:58 +00:00
|
|
|
|
|
|
|
interface Props {
|
2021-12-13 22:15:11 +00:00
|
|
|
onImageSubmit: (image: Blob, palette: Palette) => void;
|
2021-12-12 16:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ImageInput({ onImageSubmit }: Props) {
|
|
|
|
const fileInputRef = React.useRef<HTMLInputElement>(null);
|
2021-12-15 17:46:05 +00:00
|
|
|
const [palette, setPalette] = React.useState<Palette>(palettes[0]);
|
2021-12-12 16:50:58 +00:00
|
|
|
|
|
|
|
const handleSubmit: FormEventHandler = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
if (
|
|
|
|
!fileInputRef.current ||
|
|
|
|
!fileInputRef.current.files ||
|
|
|
|
fileInputRef.current.files.length === 0
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-15 17:46:05 +00:00
|
|
|
onImageSubmit(fileInputRef.current.files[0], palette);
|
2021-12-13 22:15:11 +00:00
|
|
|
};
|
|
|
|
|
2021-12-15 17:46:05 +00:00
|
|
|
const handlePaletteChange = (palette: Palette) => {
|
|
|
|
setPalette(palette);
|
2021-12-12 16:50:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form
|
|
|
|
className="grid grid-cols-1 max-w-md mx-auto my-4 px-2"
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
>
|
|
|
|
<label className="block">
|
|
|
|
<span>Select an image:</span>
|
|
|
|
<input
|
|
|
|
type="file"
|
|
|
|
accept="image/png, image/jpeg"
|
|
|
|
ref={fileInputRef}
|
2022-05-08 10:42:43 +00:00
|
|
|
className={
|
|
|
|
"block " +
|
|
|
|
"w-full " +
|
|
|
|
"mt-1 " +
|
|
|
|
"text-sm " +
|
|
|
|
"text-nord-0 " +
|
|
|
|
"dark:text-nord-5 " +
|
|
|
|
"file:mr-4 " +
|
|
|
|
"file:py-2 " +
|
|
|
|
"file:px-4 " +
|
|
|
|
"file:border-0 " +
|
|
|
|
"file:text-sm " +
|
|
|
|
"file:font-semibold " +
|
|
|
|
"file:bg-nord-4 " +
|
|
|
|
"dark:file:bg-nord-1 " +
|
|
|
|
"file:text-nord-0 " +
|
|
|
|
"dark:file:text-nord-5 " +
|
|
|
|
"hover:file:bg-nord-5 " +
|
|
|
|
"dark:hover:file:bg-nord-2"
|
|
|
|
}
|
2021-12-12 16:50:58 +00:00
|
|
|
/>
|
|
|
|
</label>
|
2021-12-13 22:15:11 +00:00
|
|
|
<label>
|
2021-12-12 16:50:58 +00:00
|
|
|
<span className="block">Select a color palette:</span>
|
2021-12-15 17:46:05 +00:00
|
|
|
<PaletteSelect value={palette} onChange={handlePaletteChange} />
|
2021-12-13 22:15:11 +00:00
|
|
|
</label>
|
2021-12-15 19:05:25 +00:00
|
|
|
<PalettePreview palette={palette}></PalettePreview>
|
2021-12-12 16:50:58 +00:00
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
value="Go"
|
2023-06-07 10:20:55 +00:00
|
|
|
data-umami-event="image submit"
|
2022-05-08 10:42:43 +00:00
|
|
|
className={
|
|
|
|
"block " +
|
|
|
|
"w-32 " +
|
|
|
|
"mx-auto " +
|
|
|
|
"mt-1 " +
|
|
|
|
"py-2 " +
|
|
|
|
"px-4 " +
|
|
|
|
"text-sm " +
|
|
|
|
"text-nord-0 " +
|
|
|
|
"dark:text-nord-5 " +
|
|
|
|
"bg-nord-4 " +
|
|
|
|
"dark:bg-nord-1 " +
|
|
|
|
"hover:bg-nord-5 " +
|
|
|
|
"dark:hover:bg-nord-2"
|
|
|
|
}
|
2021-12-12 16:50:58 +00:00
|
|
|
>
|
|
|
|
Go
|
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ImageInput;
|