Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom background color in square tool #28

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions src/app/(tools)/square-image/square-tool.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
"use client";

import React, { useState, useEffect, type ChangeEvent } from "react";
import React, { useState, useEffect, type ChangeEvent, useRef} from "react";
import { usePlausible } from "next-plausible";
import { useLocalStorage } from "@/hooks/use-local-storage";

const ColorPicker = ({
onChange,
value,
}: {
onChange: (event: ChangeEvent<HTMLInputElement>) => void;
value: string;
}) => {
const refInput = useRef<HTMLInputElement>(null);

return (
<>
<button
onClick={() => refInput.current?.click()}
FatahChan marked this conversation as resolved.
Show resolved Hide resolved
className="size-8 rounded-full border border-white shrink-0"
style={{ backgroundColor: value }}
>
{/** issue: input can't be styled */}
{/** https://stackoverflow.com/questions/48832432/rounded-input-type-color */}
<input
ref={refInput}
type="color"
className="invisible"
value={value}
onChange={onChange}
/>
</button>
</>
);
};
export const SquareTool: React.FC = () => {
const [imageFile, setImageFile] = useState<File | null>(null);
const [backgroundColor, setBackgroundColor] = useLocalStorage<
"black" | "white"
>("squareTool_backgroundColor", "white");

const [backgroundColor, setBackgroundColor] = useLocalStorage<string>("squareTool_backgroundColor", "white");
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
const [canvasDataUrl, setCanvasDataUrl] = useState<string | null>(null);
const [imageMetadata, setImageMetadata] = useState<{
Expand All @@ -30,7 +56,7 @@ export const SquareTool: React.FC = () => {
const handleBackgroundColorChange = (
event: ChangeEvent<HTMLInputElement>,
) => {
const color = event.target.value as "black" | "white";
const color = event.target.value;
setBackgroundColor(color);
};

Expand Down Expand Up @@ -164,6 +190,14 @@ export const SquareTool: React.FC = () => {
/>
<span className="ml-2">Black Background</span>
</label>
<label className="inline-flex items-center">
<ColorPicker
value={backgroundColor}
onChange={handleBackgroundColorChange}
/>

<span className="ml-2">Custom Color Background</span>
</label>
</div>

<div className="flex gap-2">
Expand Down
Loading