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.

feat: add ability to pass color to enumerable

Brad Barbin320b030673aa
diff --git a/apps/carbon/app/components/Enumerable.tsx b/apps/carbon/app/components/Enumerable.tsx
index a160522b1d..681c91ca54 100644
--- a/apps/carbon/app/components/Enumerable.tsx
+++ b/apps/carbon/app/components/Enumerable.tsx
@@ -1,4 +1,5 @@
-import { getColor } from "@carbon/utils";
+import type { Color } from "@carbon/utils";
+import { getColor, getColorByValue } from "@carbon/utils";
import type { BadgeProps } from "@carbon/react";
import { Badge } from "@carbon/react";
@@ -6,12 +7,14 @@ import { useMode } from "~/hooks/useMode";
type EnumerableProps = BadgeProps & {
value: string | null;
+ color?: Color;
};
-const Enumerable = ({ value, ...props }: EnumerableProps) => {
+const Enumerable = ({ value, color, ...props }: EnumerableProps) => {
const mode = useMode();
if (!value) return null;
- const style = getColor(value, mode);
+
+ const style = color ? getColor(color, mode) : getColorByValue(value, mode);
return (
<Badge style={style} {...props}>
{value}
diff --git a/packages/react/src/Avatar.tsx b/packages/react/src/Avatar.tsx
index 8683be22b5..d5961d2135 100644
--- a/packages/react/src/Avatar.tsx
+++ b/packages/react/src/Avatar.tsx
@@ -1,5 +1,5 @@
"use client";
-import { getColor } from "@carbon/utils";
+import { getColorByValue } from "@carbon/utils";
import type { VariantProps } from "class-variance-authority";
import { cva } from "class-variance-authority";
import type {
@@ -55,7 +55,7 @@ const Avatar = forwardRef<HTMLSpanElement, AvatarProps>(
const avatarInitials = getInitials(name ?? "");
const [error, setError] = useState(false);
- const { background, color } = getColor(name ?? "", "light");
+ const { background, color } = getColorByValue(name ?? "", "light");
return src && !error ? (
<img
diff --git a/packages/utils/src/color.ts b/packages/utils/src/color.ts
index 547f10b615..4666725f8e 100644
--- a/packages/utils/src/color.ts
+++ b/packages/utils/src/color.ts
@@ -2,6 +2,21 @@ import { getBucket } from "./hash";
const cache: Record<string, number> = {};
+const colorIndex = {
+ gray: 0,
+ lightGray: 1,
+ brown: 2,
+ orange: 3,
+ yellow: 4,
+ green: 5,
+ blue: 6,
+ purple: 7,
+ pink: 8,
+ red: 9,
+};
+
+export type Color = keyof typeof colorIndex;
+
const colors = [
{ background: "#e3e2e080", color: "#32302c" },
{ background: "#e3e2e0", color: "#32302c" },
@@ -28,7 +43,13 @@ const darkColors = [
{ background: "#6e3630", color: "#ffffff" },
];
-export function getColor(name: string, mode = "light") {
+export function getColor(color: Color, mode = "light") {
+ return mode === "dark"
+ ? darkColors[colorIndex[color]]
+ : colors[colorIndex[color]];
+}
+
+export function getColorByValue(name: string, mode = "light") {
if (cache[name])
return mode === "dark" ? darkColors[cache[name]!] : colors[cache[name]!];
const hash = getBucket(name, colors.length);