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 review — a11y, listener cleanup, repeating gradients, import order

psychosomat4de687742fd2
diff --git a/src/components/ui/gradient-editor.tsx b/src/components/ui/gradient-editor.tsx
index 1a175eea4..bf28652ab 100644
--- a/src/components/ui/gradient-editor.tsx
+++ b/src/components/ui/gradient-editor.tsx
@@ -1,4 +1,5 @@
-import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
+import type { PointerEvent as ReactPointerEvent } from "react";
+import { useCallback, useEffect, useMemo, useRef, useState } from "react";
/* ---------- icons ---------- */
const PlusIcon = () => (
@@ -203,6 +204,14 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
const draggingMain = useRef(false);
const movedRef = useRef(false);
const mountedRef = useRef(false);
+ const dragCleanup = useRef<(() => void) | null>(null);
+
+ useEffect(() => {
+ return () => {
+ dragCleanup.current?.();
+ dragCleanup.current = null;
+ };
+ }, []);
const totalColors = 1 + orbitPoints.length;
@@ -263,12 +272,22 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
setMainRadius(radius);
}, []);
- const onMainPointerDown = (e: React.PointerEvent) => {
+ const onMainPointerDown = (e: ReactPointerEvent) => {
e.stopPropagation();
draggingMain.current = true;
movedRef.current = false;
+ dragCleanup.current?.();
+ const onUp = () => {
+ draggingMain.current = false;
+ window.removeEventListener("pointermove", onMainPointerMove);
+ window.removeEventListener("pointerup", onUp);
+ window.removeEventListener("pointercancel", onUp);
+ dragCleanup.current = null;
+ };
+ dragCleanup.current = onUp;
window.addEventListener("pointermove", onMainPointerMove);
- window.addEventListener("pointerup", onMainPointerUp);
+ window.addEventListener("pointerup", onUp);
+ window.addEventListener("pointercancel", onUp);
};
const onMainPointerMove = useCallback(
@@ -280,12 +299,6 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
[updateMainFromPointer],
);
- const onMainPointerUp = useCallback(() => {
- draggingMain.current = false;
- window.removeEventListener("pointermove", onMainPointerMove);
- window.removeEventListener("pointerup", onMainPointerUp);
- }, [onMainPointerMove]);
-
/* ---------- add / remove / harmony ---------- */
const addPoint = () => {
if (totalColors >= MAX_COLORS) return;
@@ -321,15 +334,20 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
const WAVE_END_PCT = (367.037 / 420) * 100;
const clampedBrightness = clamp(brightness, WAVE_START_PCT, WAVE_END_PCT);
- const onSliderPointerDown = (e: React.PointerEvent) => {
+ const onSliderPointerDown = (e: ReactPointerEvent) => {
setBrightnessFromClientX(e.clientX);
+ dragCleanup.current?.();
const move = (ev: PointerEvent) => setBrightnessFromClientX(ev.clientX);
- const up = () => {
+ const onUp = () => {
window.removeEventListener("pointermove", move);
- window.removeEventListener("pointerup", up);
+ window.removeEventListener("pointerup", onUp);
+ window.removeEventListener("pointercancel", onUp);
+ dragCleanup.current = null;
};
+ dragCleanup.current = onUp;
window.addEventListener("pointermove", move);
- window.addEventListener("pointerup", up);
+ window.addEventListener("pointerup", onUp);
+ window.addEventListener("pointercancel", onUp);
};
/* ---------- angle knob ---------- */
@@ -346,16 +364,21 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
if (a < 0) a += 360;
setAngle(Math.round(a));
};
- const onAngleKnobPointerDown = (e: React.PointerEvent) => {
+ const onAngleKnobPointerDown = (e: ReactPointerEvent) => {
e.stopPropagation();
setAngleFromClientPos(e.clientX, e.clientY);
+ dragCleanup.current?.();
const move = (ev: PointerEvent) => setAngleFromClientPos(ev.clientX, ev.clientY);
- const up = () => {
+ const onUp = () => {
window.removeEventListener("pointermove", move);
- window.removeEventListener("pointerup", up);
+ window.removeEventListener("pointerup", onUp);
+ window.removeEventListener("pointercancel", onUp);
+ dragCleanup.current = null;
};
+ dragCleanup.current = onUp;
window.addEventListener("pointermove", move);
- window.addEventListener("pointerup", up);
+ window.addEventListener("pointerup", onUp);
+ window.addEventListener("pointercancel", onUp);
};
// Brightness filter (dark theme only)
@@ -363,6 +386,40 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
const wavePath = useMemo(() => getInterpolatedWavePath(brightness / 100), [brightness]);
+ /* ---------- keyboard handlers ---------- */
+ const onMainKeyDown = useCallback((e: React.KeyboardEvent) => {
+ const step = e.shiftKey ? 10 : 5;
+ if (e.key === "ArrowLeft" || e.key === "ArrowDown") {
+ e.preventDefault();
+ setMainAngle((a) => a - step);
+ } else if (e.key === "ArrowRight" || e.key === "ArrowUp") {
+ e.preventDefault();
+ setMainAngle((a) => a + step);
+ }
+ }, []);
+
+ const onSliderKeyDown = useCallback((e: React.KeyboardEvent) => {
+ const step = e.shiftKey ? 10 : 2;
+ if (e.key === "ArrowLeft" || e.key === "ArrowDown") {
+ e.preventDefault();
+ setBrightness((b) => clamp(b - step, 0, 100));
+ } else if (e.key === "ArrowRight" || e.key === "ArrowUp") {
+ e.preventDefault();
+ setBrightness((b) => clamp(b + step, 0, 100));
+ }
+ }, []);
+
+ const onAngleKeyDown = useCallback((e: React.KeyboardEvent) => {
+ const step = e.shiftKey ? 15 : 5;
+ if (e.key === "ArrowLeft" || e.key === "ArrowDown") {
+ e.preventDefault();
+ setAngle((a) => (a - step + 360) % 360);
+ } else if (e.key === "ArrowRight" || e.key === "ArrowUp") {
+ e.preventDefault();
+ setAngle((a) => (a + step) % 360);
+ }
+ }, []);
+
return (
<div className="w-[240px] max-w-full mx-auto rounded-[18px] p-[10px] box-border font-sans text-[#f2f2f2] select-none">
{/* Color Wheel Canvas */}
@@ -398,7 +455,14 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
{/* Main draggable point */}
<div
onPointerDown={onMainPointerDown}
- className="absolute w-11 h-11 rounded-full cursor-grab shadow-[0_0_0_4px_#f5f5f5,0_4px_14px_rgba(0,0,0,0.35)] transition-shadow z-10 active:cursor-grabbing"
+ onKeyDown={onMainKeyDown}
+ tabIndex={0}
+ role="slider"
+ aria-label="Color hue"
+ aria-valuemin={0}
+ aria-valuemax={360}
+ aria-valuenow={Math.round(((mainAngle % 360) + 360) % 360)}
+ className="absolute w-11 h-11 rounded-full cursor-grab shadow-[0_0_0_4px_#f5f5f5,0_4px_14px_rgba(0,0,0,0.35)] transition-shadow z-10 active:cursor-grabbing focus:outline-none focus:ring-2 focus:ring-[#34B27B]"
style={{
left: `${mainX}%`,
top: `${mainY}%`,
@@ -457,7 +521,14 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
<div
ref={trackRef}
onPointerDown={onSliderPointerDown}
- className="relative flex-1 h-10 flex items-center cursor-pointer"
+ onKeyDown={onSliderKeyDown}
+ tabIndex={0}
+ role="slider"
+ aria-label="Brightness"
+ aria-valuemin={0}
+ aria-valuemax={100}
+ aria-valuenow={Math.round(brightness)}
+ className="relative flex-1 h-10 flex items-center cursor-pointer focus:outline-none focus:ring-2 focus:ring-[#34B27B] rounded"
>
<svg
width="100%"
@@ -490,7 +561,14 @@ export default function GradientEditor({ onChange }: GradientEditorProps) {
<div
ref={angleKnobRef}
onPointerDown={onAngleKnobPointerDown}
- className="relative w-[42px] h-[42px] rounded-full bg-[radial-gradient(circle_at_35%_30%,#2b2b2b,#0c0c0c)] shadow-[0_3px_10px_rgba(0,0,0,0.4)] cursor-grab flex-shrink-0 active:cursor-grabbing"
+ onKeyDown={onAngleKeyDown}
+ tabIndex={0}
+ role="slider"
+ aria-label="Gradient angle"
+ aria-valuemin={0}
+ aria-valuemax={360}
+ aria-valuenow={angle}
+ className="relative w-[42px] h-[42px] rounded-full bg-[radial-gradient(circle_at_35%_30%,#2b2b2b,#0c0c0c)] shadow-[0_3px_10px_rgba(0,0,0,0.4)] cursor-grab flex-shrink-0 active:cursor-grabbing focus:outline-none focus:ring-2 focus:ring-[#34B27B]"
title="Gradient angle"
>
<div className="absolute inset-0" style={{ transform: `rotate(${angleKnobAngle}deg)` }}>
diff --git a/src/lib/gradientBuilder.test.ts b/src/lib/gradientBuilder.test.ts
index c711d4ec9..d8f530cbb 100644
--- a/src/lib/gradientBuilder.test.ts
+++ b/src/lib/gradientBuilder.test.ts
@@ -8,6 +8,7 @@ const baseState = (overrides: Partial<GradientEditorState> = {}): GradientEditor
mainY: 40,
mainColor: "rgb(255, 0, 0)",
brightness: 100,
+ angle: 135,
harmonyType: "splitComplementary",
...overrides,
});
diff --git a/src/lib/wallpaper.test.ts b/src/lib/wallpaper.test.ts
index 3e03a1664..3e8bdab42 100644
--- a/src/lib/wallpaper.test.ts
+++ b/src/lib/wallpaper.test.ts
@@ -130,6 +130,15 @@ describe("classifyWallpaper", () => {
value: "linear-gradient(135deg, rgb(255,0,0) 0%, rgb(0,0,255) 100%)",
});
});
+
+ it("handles repeating-gradient variants in composite backgrounds", () => {
+ const composite =
+ 'url("data:image/svg+xml;utf8,<svg/>") repeat, repeating-linear-gradient(45deg, red, blue)';
+ expect(classifyWallpaper(composite)).toEqual({
+ kind: "gradient",
+ value: "repeating-linear-gradient(45deg, red, blue)",
+ });
+ });
});
describe("resolveImageWallpaperUrl", () => {
diff --git a/src/lib/wallpaper.ts b/src/lib/wallpaper.ts
index df739c27f..9eefa5265 100644
--- a/src/lib/wallpaper.ts
+++ b/src/lib/wallpaper.ts
@@ -45,7 +45,14 @@ export function classifyWallpaper(value: string): WallpaperClassification {
function extractTrailingGradient(value: string): string | null {
const matches: number[] = [];
- for (const type of ["linear", "radial", "conic"]) {
+ 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);
}