Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(ui): destructive red meets WCAG text contrast

Raulad6531ebd706
diff --git a/packages/react/src/Context.tsx b/packages/react/src/Context.tsx
index 51ca2a2c1f..19906829da 100644
--- a/packages/react/src/Context.tsx
+++ b/packages/react/src/Context.tsx
@@ -128,7 +128,7 @@ const DesktopContextMenuItem = forwardRef<
inset && "pl-8",
shortcut && !asChild && "whitespace-nowrap",
destructive &&
- "text-red-500 focus:text-red-500 hover:bg-destructive/20 active:bg-destructive/20",
+ "text-destructive focus:text-destructive hover:bg-destructive/20 active:bg-destructive/20",
className
)}
{...props}
diff --git a/packages/react/src/Dropdown.tsx b/packages/react/src/Dropdown.tsx
index 6a45178971..ed978867b9 100644
--- a/packages/react/src/Dropdown.tsx
+++ b/packages/react/src/Dropdown.tsx
@@ -143,7 +143,7 @@ const DesktopDropdownMenuItem = forwardRef<
inset && "pl-8",
shortcut && !asChild && "whitespace-nowrap",
destructive &&
- "text-red-500 focus:text-red-500 hover:bg-destructive/20 active:bg-destructive/20",
+ "text-destructive focus:text-destructive hover:bg-destructive/20 active:bg-destructive/20",
className
)}
{...props}
diff --git a/packages/utils/src/themes.test.ts b/packages/utils/src/themes.test.ts
new file mode 100644
index 0000000000..175bcd0ae6
--- /dev/null
+++ b/packages/utils/src/themes.test.ts
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
+// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+
+import { describe, expect, it } from "vitest";
+import { themes } from "./themes";
+
+// WCAG 2.1 contrast ratio between two theme values written as "H S% L%".
+function contrast(a: string, b: string) {
+ const la = relativeLuminance(a);
+ const lb = relativeLuminance(b);
+ return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
+}
+
+function relativeLuminance(value: string) {
+ const match = value.match(/^([\d.]+)\s+([\d.]+)%?\s+([\d.]+)%$/);
+ if (!match) throw new Error(`Not an HSL theme value: ${value}`);
+ const h = Number(match[1]);
+ const s = Number(match[2]) / 100;
+ const l = Number(match[3]) / 100;
+ const k = (n: number) => (n + h / 30) % 12;
+ const a = s * Math.min(l, 1 - l);
+ const linear = (n: number) => {
+ const c = l - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1));
+ return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
+ };
+ return 0.2126 * linear(0) + 0.7152 * linear(8) + 0.0722 * linear(4);
+}
+
+const TEXT = 4.5;
+const SURFACES = ["card", "background", "muted", "popover"] as const;
+
+describe("destructive colour contrast", () => {
+ it.each(
+ themes.flatMap((theme) =>
+ (["light", "dark"] as const).map((mode) => [theme.name, mode] as const)
+ )
+ )("%s %s: destructive-foreground on destructive", (name, mode) => {
+ const vars = themes.find((t) => t.name === name)!.cssVars[mode];
+ expect(
+ contrast(vars.destructive, vars["destructive-foreground"])
+ ).toBeGreaterThanOrEqual(TEXT);
+ });
+
+ it.each(themes.map((theme) => theme.name))(
+ "%s light: destructive text on every surface",
+ (name) => {
+ const vars = themes.find((t) => t.name === name)!.cssVars.light;
+ for (const surface of SURFACES) {
+ expect(
+ contrast(vars.destructive, vars[surface]),
+ surface
+ ).toBeGreaterThanOrEqual(TEXT);
+ }
+ }
+ );
+
+ it("zinc dark: destructive text on every surface", () => {
+ const vars = themes.find((t) => t.name === "zinc")!.cssVars.dark;
+ for (const surface of SURFACES) {
+ expect(
+ contrast(vars.destructive, vars[surface]),
+ surface
+ ).toBeGreaterThanOrEqual(TEXT);
+ }
+ });
+});
diff --git a/packages/utils/src/themes.ts b/packages/utils/src/themes.ts
index fc877704e0..1164fc3ceb 100644
--- a/packages/utils/src/themes.ts
+++ b/packages/utils/src/themes.ts
@@ -30,7 +30,7 @@ export const themes = [
"muted-foreground": "220 3.8% 46.1%",
accent: "220 4.8% 95.9%",
"accent-foreground": "220 5.9% 10%",
- destructive: "0 84.2% 60.2%",
+ destructive: "0 74% 42%",
"destructive-foreground": "0 0% 98%",
border: "220 5.9% 90%",
input: "220 5.9% 90%",
@@ -57,7 +57,7 @@ export const themes = [
accent: "0 0% 10%",
"accent-foreground": "0 0% 93%",
destructive: "0 100% 64%",
- "destructive-foreground": "0 0% 100%",
+ "destructive-foreground": "0 0% 0%",
border: "0 0% 15%",
input: "0 0% 15%",
ring: "0 0% 35%",
@@ -91,7 +91,7 @@ export const themes = [
"muted-foreground": "0 0% 45.1%",
accent: "0 0% 96%",
"accent-foreground": "0 0% 9%",
- destructive: "0 84.2% 60.2%",
+ destructive: "0 74% 42%",
"destructive-foreground": "0 0% 98%",
border: "0 0% 80%",
input: "0 0% 80%",
@@ -151,7 +151,7 @@ export const themes = [
"muted-foreground": "0 0% 45.1%",
accent: "0 0% 96.1%",
"accent-foreground": "0 0% 9%",
- destructive: "0 84.2% 60.2%",
+ destructive: "0 74% 42%",
"destructive-foreground": "0 0% 98%",
border: "0 0% 89.8%",
input: "0 0% 89.8%",
@@ -211,7 +211,7 @@ export const themes = [
"muted-foreground": "25 5.3% 44.7%",
accent: "24 4.8% 95.9%",
"accent-foreground": "24 9.8% 10%",
- destructive: "0 84.2% 60.2%",
+ destructive: "0 74% 42%",
"destructive-foreground": "60 9.1% 97.8%",
border: "20 5.9% 90%",
input: "20 5.9% 90%",
@@ -271,7 +271,7 @@ export const themes = [
"muted-foreground": "25 5.3% 44.7%",
accent: "65 4.8% 95.9%",
"accent-foreground": "24 9.8% 10%",
- destructive: "0 84.2% 60.2%",
+ destructive: "0 74% 42%",
"destructive-foreground": "65 9.1% 97.8%",
border: "20 5.9% 90%",
input: "20 5.9% 90%",
@@ -333,7 +333,7 @@ export const themes = [
"muted-foreground": "171 3.8% 46.1%",
accent: "171 4.8% 95.9%",
"accent-foreground": "171 5.9% 10%",
- destructive: "0 84.2% 60.2%",
+ destructive: "0 74% 42%",
"destructive-foreground": "0 0% 98%",
border: "171 5.9% 90%",
input: "171 5.9% 90%",
@@ -393,7 +393,7 @@ export const themes = [
"muted-foreground": "215.4 16.3% 46.9%",
accent: "237 40% 96.1%",
"accent-foreground": "237.2 47.4% 11.2%",
- destructive: "0 84.2% 60.2%",
+ destructive: "0 74% 42%",
"destructive-foreground": "210 40% 98%",
border: "214.3 31.8% 91.4%",
input: "214.3 31.8% 91.4%",
@@ -453,7 +453,7 @@ export const themes = [
"muted-foreground": "238 8.9% 46.1%",
accent: "238 33.3% 95.9%",
"accent-foreground": "220.9 39.3% 11%",
- destructive: "0 84.2% 60.2%",
+ destructive: "0 74% 42%",
"destructive-foreground": "210 20% 98%",
border: "220 13% 91%",
input: "220 13% 91%",