Skip to content

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

handle transparent values for the color wheel

BaptisteAuscher545c02b5bbfa
diff --git a/src/components/ui/color-picker.tsx b/src/components/ui/color-picker.tsx
index ea5eb3021..eed2f0240 100644
--- a/src/components/ui/color-picker.tsx
+++ b/src/components/ui/color-picker.tsx
@@ -1,3 +1,4 @@
+import { HsvaColor, hexToHsva } from "@uiw/color-convert";
import Block from "@uiw/react-color-block";
import Colorful from "@uiw/react-color-colorful";
import { useEffect, useState } from "react";
@@ -20,6 +21,12 @@ export default function ColorPicker({
}) {
const [colorMode, setColorMode] = useState<"wheel" | "palette">("wheel");
const [hexInput, setHexInput] = useState(selectedColor);
+ const [transparentColorHSVA, setTransparentColorHSVA] = useState<HsvaColor>({
+ h: 0,
+ s: 0,
+ v: 0,
+ a: 0,
+ });
useEffect(() => {
setHexInput(selectedColor);
@@ -55,6 +62,12 @@ export default function ColorPicker({
onUpdateColor(normalized);
}
};
+
+ const toTransparent = (color: string) => {
+ const hsva = hexToHsva(color);
+ hsva.a = 0;
+ return hsva;
+ };
return (
<div className="p-1 flex flex-col gap-4 items-center">
<div className="flex items-center gap-2 w-full">
@@ -94,7 +107,7 @@ export default function ColorPicker({
<span style={{ color: getTextColor(selectedColor) }}>{selectedColor}</span>
</div>
<Colorful
- color={selectedColor}
+ color={selectedColor !== "transparent" ? selectedColor : transparentColorHSVA}
onChange={(color) => {
onUpdateColor(color.hex);
}}
@@ -130,6 +143,8 @@ export default function ColorPicker({
size="sm"
className="w-full mt-2 text-xs h-7 hover:bg-white/5 text-slate-400"
onClick={() => {
+ const hsva = toTransparent(selectedColor);
+ setTransparentColorHSVA(hsva);
onUpdateColor("transparent");
}}
>