Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix: address maintainer review feedback on gradient editor

psychosomatf8b891ed2f55
diff --git a/src/components/ui/gradient-editor.test.tsx b/src/components/ui/gradient-editor.test.tsx
new file mode 100644
index 000000000..0d54a26ea
--- /dev/null
+++ b/src/components/ui/gradient-editor.test.tsx
@@ -0,0 +1,69 @@
+import "@testing-library/jest-dom";
+import { fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+import type { GradientEditorState } from "./gradient-editor";
+import GradientEditor from "./gradient-editor";
+
+const getLastState = (onChange: ReturnType<typeof vi.fn>): GradientEditorState => {
+ const calls = onChange.mock.calls;
+ expect(calls.length).toBeGreaterThan(0);
+ return calls[calls.length - 1][0] as GradientEditorState;
+};
+
+describe("GradientEditor", () => {
+ it("emits unique point IDs after an interaction", async () => {
+ const onChange = vi.fn();
+ render(<GradientEditor onChange={onChange} />);
+
+ const removeButton = screen.getByRole("button", { name: "Remove color" });
+ fireEvent.click(removeButton);
+ await waitFor(() => expect(onChange).toHaveBeenCalled());
+
+ const state = getLastState(onChange);
+ const ids = state.points.map((p) => p.id);
+ expect(new Set(ids).size).toBe(ids.length);
+ expect(ids).toContain("main");
+ expect(ids).toHaveLength(2);
+ });
+
+ it("generates a fresh ID after remove→add instead of reusing an existing one", async () => {
+ const onChange = vi.fn();
+ render(<GradientEditor onChange={onChange} />);
+
+ fireEvent.click(screen.getByRole("button", { name: "Remove color" }));
+ await waitFor(() => expect(getLastState(onChange).points).toHaveLength(2));
+ expect(getLastState(onChange).points.map((p) => p.id)).toEqual(["main", "o1"]);
+
+ fireEvent.click(screen.getByRole("button", { name: "Add color" }));
+ await waitFor(() => expect(getLastState(onChange).points).toHaveLength(3));
+ const finalIds = getLastState(onChange).points.map((p) => p.id);
+ expect(new Set(finalIds).size).toBe(finalIds.length);
+ expect(finalIds).toContain("o3");
+ });
+
+ it("disables add at MAX_COLORS and remove at one color", async () => {
+ render(<GradientEditor onChange={vi.fn()} />);
+
+ const addButton = screen.getByRole("button", { name: "Add color" });
+ const removeButton = screen.getByRole("button", { name: "Remove color" });
+
+ expect(addButton).toBeDisabled();
+ expect(removeButton).not.toBeDisabled();
+
+ fireEvent.click(removeButton);
+ await waitFor(() => expect(addButton).not.toBeDisabled());
+
+ fireEvent.click(removeButton);
+ await waitFor(() => expect(removeButton).toBeDisabled());
+ expect(addButton).not.toBeDisabled();
+ });
+
+ it("cycles harmony when eligible", async () => {
+ const onChange = vi.fn();
+ render(<GradientEditor onChange={onChange} />);
+
+ fireEvent.click(screen.getByRole("button", { name: "Cycle harmony" }));
+ await waitFor(() => expect(onChange).toHaveBeenCalled());
+ expect(getLastState(onChange).harmonyType).not.toBe("splitComplementary");
+ });
+});
diff --git a/src/components/ui/gradient-editor.tsx b/src/components/ui/gradient-editor.tsx
index bf28652ab..19bb98603 100644
--- a/src/components/ui/gradient-editor.tsx
+++ b/src/components/ui/gradient-editor.tsx
@@ -27,7 +27,6 @@ const clamp = (v: number, min: number, max: number) => Math.min(max, Math.max(mi
const MAX_COLORS = 3;
const MAIN_ID = "main";
const MAIN_MAX_RADIUS = 40;
-let idCounter = 1;
/* ---------- color-wheel harmony math ---------- */
type ColorHarmony = {
@@ -194,7 +193,11 @@ type GradientEditorProps = {
export default function GradientEditor({ onChange }: GradientEditorProps) {
const [mainAngle, setMainAngle] = useState(-35);
const [mainRadius, setMainRadius] = useState(20);
- const [orbitPoints, setOrbitPoints] = useState<{ id: string }[]>([{ id: "o1" }, { id: "o2" }]);
+ const idCounter = useRef(1);
+ const [orbitPoints, setOrbitPoints] = useState<{ id: string }[]>(() => [
+ { id: `o${idCounter.current++}` },
+ { id: `o${idCounter.current++}` },
+ ]);
const [harmonyType, setHarmonyType] = useState<HarmonyType>("splitComplementary");
const [brightness, setBrightness] = useState(55);
const [angle, setAngle] = useState(135);
@@ -202,7 +205,6 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
const canvasRef = useRef<HTMLDivElement>(null);
const trackRef = useRef<HTMLDivElement>(null);
const draggingMain = useRef(false);
- const movedRef = useRef(false);
const mountedRef = useRef(false);
const dragCleanup = useRef<(() => void) | null>(null);
@@ -275,7 +277,6 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
const onMainPointerDown = (e: ReactPointerEvent) => {
e.stopPropagation();
draggingMain.current = true;
- movedRef.current = false;
dragCleanup.current?.();
const onUp = () => {
draggingMain.current = false;
@@ -293,7 +294,6 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
const onMainPointerMove = useCallback(
(e: PointerEvent) => {
if (!draggingMain.current) return;
- movedRef.current = true;
updateMainFromPointer(e.clientX, e.clientY);
},
[updateMainFromPointer],
@@ -302,7 +302,7 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
/* ---------- add / remove / harmony ---------- */
const addPoint = () => {
if (totalColors >= MAX_COLORS) return;
- const id = `o${idCounter++}`;
+ const id = `o${idCounter.current++}`;
setOrbitPoints((prev) => [...prev, { id }]);
};
@@ -381,8 +381,9 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
window.addEventListener("pointercancel", onUp);
};
- // Brightness filter (dark theme only)
- const brightnessFilter = `brightness(${0.62 * (0.55 + (brightness / 100) * 0.9)}) saturate(0.95)`;
+ // Preview brightness must match the per-channel multiplier in
+ // applyBrightness so the wheel is WYSIWYG.
+ const brightnessFilter = `brightness(${brightness / 100})`;
const wavePath = useMemo(() => getInterpolatedWavePath(brightness / 100), [brightness]);
@@ -475,6 +476,7 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
{/* Controls on canvas */}
<div className="absolute left-3.5 bottom-3 flex gap-3">
<button
+ type="button"
onClick={addPoint}
disabled={totalColors >= MAX_COLORS}
className={`w-6 h-6 rounded-full flex items-center justify-center border-none bg-transparent transition-all text-white/60 hover:text-white ${
@@ -488,6 +490,7 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
</button>
<button
+ type="button"
onClick={removePoint}
disabled={totalColors <= 1}
className={`w-6 h-6 rounded-full flex items-center justify-center border-none bg-transparent transition-all text-white/60 hover:text-white ${
@@ -501,6 +504,7 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
</button>
<button
+ type="button"
onClick={cycleHarmony}
disabled={eligibleHarmonies.length <= 1}
className={`w-6 h-6 rounded-full flex items-center justify-center border-none bg-transparent transition-all text-white/60 hover:text-white ${
diff --git a/src/lib/wallpaper.test.ts b/src/lib/wallpaper.test.ts
index 3e8bdab42..7f764e5d6 100644
--- a/src/lib/wallpaper.test.ts
+++ b/src/lib/wallpaper.test.ts
@@ -139,6 +139,22 @@ describe("classifyWallpaper", () => {
value: "repeating-linear-gradient(45deg, red, blue)",
});
});
+
+ it("tolerates missing or extra whitespace around the comma in composite backgrounds", () => {
+ const noSpace =
+ 'url("data:image/svg+xml;utf8,<svg/>") repeat,linear-gradient(135deg, red, blue)';
+ expect(classifyWallpaper(noSpace)).toEqual({
+ kind: "gradient",
+ value: "linear-gradient(135deg, red, blue)",
+ });
+
+ const extraSpace =
+ 'url("data:image/svg+xml;utf8,<svg/>") repeat, linear-gradient(135deg, red, blue)';
+ expect(classifyWallpaper(extraSpace)).toEqual({
+ kind: "gradient",
+ value: "linear-gradient(135deg, red, blue)",
+ });
+ });
});
describe("resolveImageWallpaperUrl", () => {
diff --git a/src/lib/wallpaper.ts b/src/lib/wallpaper.ts
index 9eefa5265..010f47c2f 100644
--- a/src/lib/wallpaper.ts
+++ b/src/lib/wallpaper.ts
@@ -44,21 +44,8 @@ export function classifyWallpaper(value: string): WallpaperClassification {
}
function extractTrailingGradient(value: string): string | null {
- const matches: number[] = [];
- for (const type of [
- "linear",
- "radial",
- "conic",
- "repeating-linear",
- "repeating-radial",
- "repeating-conic",
- ]) {
- const idx = value.lastIndexOf(`, ${type}-gradient(`);
- if (idx >= 0) matches.push(idx);
- }
- if (matches.length === 0) return null;
- const start = Math.max(...matches) + 2;
- return value.slice(start);
+ const match = value.match(/,\s*((?:repeating-)?(?:linear|radial|conic)-gradient\(.*\))\s*$/);
+ return match?.[1] ?? null;
}
const ALLOWED_IMAGE_PREFIX = "/wallpapers/";