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: simplify theme UI and amplify theme colors

Brad Barbinad9810e0deab
diff --git a/apps/carbon/app/components/Form/Color.tsx b/apps/carbon/app/components/Form/Color.tsx
index e9c58a14da..a3cd424f51 100644
--- a/apps/carbon/app/components/Form/Color.tsx
+++ b/apps/carbon/app/components/Form/Color.tsx
@@ -40,7 +40,7 @@ const ColorPicker = ({ name, label }: ColorFieldProps) => {
<HexColorInput
color={value}
onChange={setValue}
- className="py-0 px-2 h-9 rounded-md outline-2 outline-offset-2 outline-transparent border border-border z-50"
+ className="py-0 px-2 h-9 rounded-md outline-2 outline-offset-2 outline-transparent border border-border"
/>
</HStack>
{disclosure.isOpen && (
diff --git a/apps/carbon/app/jobs/update-exchange-rates.server.ts b/apps/carbon/app/jobs/update-exchange-rates.server.ts
index 09d0754fb8..f9c29e3dbe 100644
--- a/apps/carbon/app/jobs/update-exchange-rates.server.ts
+++ b/apps/carbon/app/jobs/update-exchange-rates.server.ts
@@ -55,8 +55,6 @@ const job = triggerClient.defineJob({
}))
.filter((currency) => currency.exchangeRate);
- console.log({ updates });
-
if (updates?.length === 0) return;
const { error } = await supabaseClient.from("currency").upsert(updates);
diff --git a/apps/carbon/app/modules/settings/settings.models.ts b/apps/carbon/app/modules/settings/settings.models.ts
index 0fea009af6..36eded7ca2 100644
--- a/apps/carbon/app/modules/settings/settings.models.ts
+++ b/apps/carbon/app/modules/settings/settings.models.ts
@@ -36,31 +36,21 @@ export const sequenceValidator = withZod(
})
);
+export const themes = [
+ "zinc",
+ "neutral",
+ "red",
+ "rose",
+ "orange",
+ "green",
+ "blue",
+ "yellow",
+ "violet",
+] as const;
+export type Theme = (typeof themes)[number];
+
export const themeValidator = withZod(
z.object({
- primaryBackgroundLight: z
- .string()
- .min(1, { message: "Primary background light is required" }),
- primaryForegroundLight: z
- .string()
- .min(1, { message: "Primary foreground light is required" }),
- accentBackgroundLight: z
- .string()
- .min(1, { message: "Accent background light is required" }),
- accentForegroundLight: z
- .string()
- .min(1, { message: "Accent foreground light is required" }),
- primaryBackgroundDark: z
- .string()
- .min(1, { message: "Primary background dark is required" }),
- primaryForegroundDark: z
- .string()
- .min(1, { message: "Primary foreground dark is required" }),
- accentBackgroundDark: z
- .string()
- .min(1, { message: "Accent background dark is required" }),
- accentForegroundDark: z
- .string()
- .min(1, { message: "Accent foreground dark is required" }),
+ theme: z.enum(themes),
})
);
diff --git a/apps/carbon/app/modules/settings/ui/Theme/ThemeForm.tsx b/apps/carbon/app/modules/settings/ui/Theme/ThemeForm.tsx
index 9dd6e8e1b1..8869529eeb 100644
--- a/apps/carbon/app/modules/settings/ui/Theme/ThemeForm.tsx
+++ b/apps/carbon/app/modules/settings/ui/Theme/ThemeForm.tsx
@@ -1,7 +1,11 @@
-import { VStack } from "@carbon/react";
+import { Button, VStack, cn, useMount } from "@carbon/react";
+import type { Theme } from "@carbon/utils";
+import { themes } from "@carbon/utils";
+import { useState } from "react";
+import { RxCheck } from "react-icons/rx";
import { ValidatedForm } from "remix-validated-form";
-import { Color, Submit } from "~/components/Form";
-import { SectionTitle } from "~/components/Layout";
+import { Hidden, Submit } from "~/components/Form";
+import type { Theme as ThemeValue } from "~/modules/settings";
import { themeValidator } from "~/modules/settings";
import type { TypeOfValidator } from "~/types/validators";
import { path } from "~/utils/path";
@@ -10,31 +14,66 @@ type ThemeFormProps = {
theme: TypeOfValidator<typeof themeValidator>;
};
-const ThemeForm = ({ theme }: ThemeFormProps) => {
+const ThemeForm = ({ theme: defaultValues }: ThemeFormProps) => {
+ const [theme, setTheme] = useState<ThemeValue>(defaultValues.theme);
+ const [isDark, setIsDark] = useState(false);
+
+ useMount(() => {
+ setIsDark(document.body.classList.contains("dark"));
+ });
+
+ const onThemeChange = (t: Theme) => {
+ setTheme(t.name);
+
+ const variables = isDark ? t.cssVars.dark : t.cssVars.light;
+
+ Object.entries(variables).forEach(([key, value]) => {
+ document.body.style.setProperty(`--${key}`, value);
+ });
+ };
+
return (
<div className="w-full">
<ValidatedForm
method="post"
action={path.to.theme}
validator={themeValidator}
- defaultValues={theme}
+ defaultValues={defaultValues}
>
<VStack spacing={4} className="max-w-[520px]">
- <SectionTitle>Light Mode</SectionTitle>
- <div className="grid grid-cols-1 md:grid-cols-2 gap-4 w-full">
- <Color name="primaryBackgroundLight" label="Primary Background" />
- <Color name="primaryForegroundLight" label="Primary Foreground" />
- <Color name="accentBackgroundLight" label="Accent Background" />
- <Color name="accentForegroundLight" label="Accent Foreground" />
- </div>
- <SectionTitle>Dark Mode</SectionTitle>
- <div className="grid grid-cols-1 md:grid-cols-2 gap-4 w-full">
- <Color name="primaryBackgroundDark" label="Primary Background" />
- <Color name="primaryForegroundDark" label="Primary Foreground" />
- <Color name="accentBackgroundDark" label="Accent Background Dark" />
- <Color name="accentForegroundDark" label="Accent Foreground Dark" />
+ <Hidden name="theme" value={theme} />
+ <div className="grid grid-cols-3 gap-4">
+ {themes.map((t) => {
+ const isActive = theme === t.name;
+ return (
+ <Button
+ key={t.name}
+ variant="secondary"
+ onClick={() => onThemeChange(t)}
+ className={cn(
+ "justify-start",
+ isActive && "border-2 border-primary"
+ )}
+ style={
+ {
+ "--theme-primary": `hsl(${
+ t?.activeColor[isDark ? "dark" : "light"]
+ })`,
+ } as React.CSSProperties
+ }
+ >
+ <span
+ className={cn(
+ "mr-1 flex h-5 w-5 shrink-0 -translate-x-1 items-center justify-center rounded-full bg-[--theme-primary]"
+ )}
+ >
+ {isActive && <RxCheck className="h-4 w-4 text-white" />}
+ </span>
+ {t.label}
+ </Button>
+ );
+ })}
</div>
-
<Submit>Save</Submit>
</VStack>
</ValidatedForm>
diff --git a/apps/carbon/app/routes/assets+/theme[.]css.ts b/apps/carbon/app/routes/assets+/theme[.]css.ts
index a0bb0340b4..b62aad84df 100644
--- a/apps/carbon/app/routes/assets+/theme[.]css.ts
+++ b/apps/carbon/app/routes/assets+/theme[.]css.ts
@@ -1,3 +1,4 @@
+import { getThemeCode, themes } from "@carbon/utils";
import type { LoaderFunctionArgs } from "@remix-run/node";
import { getTheme } from "~/modules/settings";
import { requirePermissions } from "~/services/auth";
@@ -14,34 +15,20 @@ export async function loader({ request }: LoaderFunctionArgs) {
});
}
- const {
- primaryBackgroundLight,
- primaryForegroundLight,
- accentBackgroundLight,
- accentForegroundLight,
- primaryBackgroundDark,
- primaryForegroundDark,
- accentBackgroundDark,
- accentForegroundDark,
- } = theme.data;
+ const selectedThemeName = theme.data?.theme || "zinc";
+ const selectedTheme = themes.find(
+ (theme) => theme.name === selectedThemeName
+ );
- console.log(theme.data);
-
- let css = `
- :root {
- --primary: ${primaryBackgroundLight};
- --primary-foreground: ${primaryForegroundLight};
- --accent: ${accentBackgroundLight};
- --accent-foreground: ${accentForegroundLight};
+ if (!selectedTheme) {
+ return new Response("", {
+ headers: {
+ "Content-Type": "text/css",
+ },
+ });
}
- .dark {
- --primary: ${primaryBackgroundDark};
- --primary-foreground: ${primaryForegroundDark};
- --accent: ${accentBackgroundDark};
- --accent-foreground: ${accentForegroundDark};
- }
- `;
+ const css = getThemeCode(selectedTheme);
return new Response(css, {
headers: {
diff --git a/apps/carbon/app/routes/x+/settings+/theme.tsx b/apps/carbon/app/routes/x+/settings+/theme.tsx
index ac04dc321a..69947459b6 100644
--- a/apps/carbon/app/routes/x+/settings+/theme.tsx
+++ b/apps/carbon/app/routes/x+/settings+/theme.tsx
@@ -1,10 +1,10 @@
import { VStack } from "@carbon/react";
-import { hexToHls, hslToHex } from "@carbon/utils";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import { validationError } from "remix-validated-form";
import { PageTitle } from "~/components/Layout";
+import type { Theme as ThemeValue } from "~/modules/settings";
import {
ThemeForm,
getTheme,
@@ -36,15 +36,8 @@ export async function loader({ request }: LoaderFunctionArgs) {
);
}
- const { id, updatedBy, updatedAt, ...themeData } = theme.data;
-
- Object.keys(themeData).forEach((key) => {
- // @ts-ignore
- themeData[key] = hslToHex(themeData[key]);
- });
-
return json({
- theme: themeData,
+ theme: theme.data,
});
}
@@ -61,12 +54,6 @@ export async function action({ request }: ActionFunctionArgs) {
return validationError(validation.error);
}
- const themeData = validation.data;
- Object.keys(themeData).forEach((key) => {
- // @ts-ignore
- themeData[key] = hexToHls(themeData[key]);
- });
-
const update = await updateTheme(client, {
...validation.data,
updatedBy: userId,
@@ -77,23 +64,23 @@ export async function action({ request }: ActionFunctionArgs) {
await flash(request, error(update.error, "Failed to update theme"))
);
- return json(
- {},
- await flash(request, success("Updated theme. Please refresh the page."))
- );
+ return json({}, await flash(request, success("Updated theme.")));
}
export default function Theme() {
const { theme } = useLoaderData<typeof loader>();
- const initialValues = theme;
+ const initialValues = {
+ theme: theme.theme as ThemeValue,
+ };
return (
<VStack spacing={0} className="bg-background p-8 h-full">
<PageTitle
title="Theme"
- subtitle="This information will be displayed on document headers."
+ subtitle="Pick a style and color scheme that suits you best."
/>
+
<ThemeForm theme={initialValues} />
</VStack>
);
diff --git a/package-lock.json b/package-lock.json
index 6a58d67ebb..042d111ff5 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -8958,6 +8958,15 @@
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.202.tgz",
"integrity": "sha512-OvlIYQK9tNneDlS0VN54LLd5uiPCBOp7gS5Z0f1mjoJYBrtStzgmJBxONW3U6OZqdtNzZPmn9BS/7WI7BFFcFQ=="
},
+ "node_modules/@types/lodash.template": {
+ "version": "4.5.3",
+ "resolved": "https://registry.npmjs.org/@types/lodash.template/-/lodash.template-4.5.3.tgz",
+ "integrity": "sha512-Mo0UYKLu1oXgkV9TVoXZLlXXjyIXlW7ZQRxi/4gQJmzJr63dmicE8gG0OkPjYTKBrBic852q0JzqrtNUWLBIyA==",
+ "dev": true,
+ "dependencies": {
+ "@types/lodash": "*"
+ }
+ },
"node_modules/@types/mdast": {
"version": "3.0.15",
"resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-3.0.15.tgz",
@@ -19991,6 +20000,11 @@
"resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz",
"integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw=="
},
+ "node_modules/lodash._reinterpolate": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz",
+ "integrity": "sha512-xYHt68QRoYGjeeM/XOE1uJtvXQAgvszfBhjV4yvsQH0u2i9I6cI6c6/eG4Hh3UAOVn0y/xAXwmTzEay49Q//HA=="
+ },
"node_modules/lodash.camelcase": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz",
@@ -20042,6 +20056,23 @@
"integrity": "sha512-HDWXG8isMntAyRF5vZ7xKuEvOhT4AhlRt/3czTSjvGUxjYCBVRQY48ViDHyfYz9VIoBkW4TMGQNapx+l3RUwdA==",
"dev": true
},
+ "node_modules/lodash.template": {
+ "version": "4.5.0",
+ "resolved": "https://registry.npmjs.org/lodash.template/-/lodash.template-4.5.0.tgz",
+ "integrity": "sha512-84vYFxIkmidUiFxidA/KjjH9pAycqW+h980j7Fuz5qxRtO9pgB7MDFTdys1N7A5mcucRiDyEq4fusljItR1T/A==",
+ "dependencies": {
+ "lodash._reinterpolate": "^3.0.0",
+ "lodash.templatesettings": "^4.0.0"
+ }
+ },
+ "node_modules/lodash.templatesettings": {
+ "version": "4.2.0",
+ "resolved": "https://registry.npmjs.org/lodash.templatesettings/-/lodash.templatesettings-4.2.0.tgz",
+ "integrity": "sha512-stgLz+i3Aa9mZgnjr/O+v9ruKZsPsndy7qPZOchbqk2cnTU1ZaldKK+v7m54WoKIyxiuMZTKT2H81F8BeAc3ZQ==",
+ "dependencies": {
+ "lodash._reinterpolate": "^3.0.0"
+ }
+ },
"node_modules/lodash.words": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/lodash.words/-/lodash.words-4.2.0.tgz",
@@ -32707,10 +32738,14 @@
"name": "@carbon/utils",
"version": "0.0.0",
"license": "MIT",
+ "dependencies": {
+ "lodash.template": "^4.5.0"
+ },
"devDependencies": {
"@carbon/jest": "*",
"@carbon/tsconfig": "*",
"@types/jest": "^26.0.22",
+ "@types/lodash.template": "4.5.3",
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6",
"eslint": "^8.23.1",
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 30a56bd5bc..aaa7792ae1 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -8376,41 +8376,20 @@ export interface Database {
};
theme: {
Row: {
- accentBackgroundDark: string;
- accentBackgroundLight: string;
- accentForegroundDark: string;
- accentForegroundLight: string;
id: boolean;
- primaryBackgroundDark: string;
- primaryBackgroundLight: string;
- primaryForegroundDark: string;
- primaryForegroundLight: string;
+ theme: string;
updatedAt: string;
updatedBy: string;
};
Insert: {
- accentBackgroundDark: string;
- accentBackgroundLight: string;
- accentForegroundDark: string;
- accentForegroundLight: string;
id?: boolean;
- primaryBackgroundDark: string;
- primaryBackgroundLight: string;
- primaryForegroundDark: string;
- primaryForegroundLight: string;
+ theme: string;
updatedAt?: string;
updatedBy: string;
};
Update: {
- accentBackgroundDark?: string;
- accentBackgroundLight?: string;
- accentForegroundDark?: string;
- accentForegroundLight?: string;
id?: boolean;
- primaryBackgroundDark?: string;
- primaryBackgroundLight?: string;
- primaryForegroundDark?: string;
- primaryForegroundLight?: string;
+ theme?: string;
updatedAt?: string;
updatedBy?: string;
};
diff --git a/packages/database/supabase/migrations/20240201042223_theme.sql b/packages/database/supabase/migrations/20240201042223_theme.sql
index c9b4389622..ad529b6623 100644
--- a/packages/database/supabase/migrations/20240201042223_theme.sql
+++ b/packages/database/supabase/migrations/20240201042223_theme.sql
@@ -1,13 +1,6 @@
CREATE TABLE "theme" (
"id" BOOLEAN NOT NULL DEFAULT TRUE,
- "primaryBackgroundLight" TEXT NOT NULL,
- "primaryForegroundLight" TEXT NOT NULL,
- "primaryBackgroundDark" TEXT NOT NULL,
- "primaryForegroundDark" TEXT NOT NULL,
- "accentBackgroundLight" TEXT NOT NULL,
- "accentForegroundLight" TEXT NOT NULL,
- "accentBackgroundDark" TEXT NOT NULL,
- "accentForegroundDark" TEXT NOT NULL,
+ "theme" TEXT NOT NULL,
"updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"updatedBy" TEXT NOT NULL,
@@ -18,11 +11,9 @@ CREATE TABLE "theme" (
ALTER TABLE "theme" ENABLE ROW LEVEL SECURITY;
-CREATE POLICY "Authenticated users can view themes" ON "theme"
+CREATE POLICY "Anyone can view the theme" ON "theme"
FOR SELECT
- USING (
- auth.role() = 'authenticated'
- );
+ USING (TRUE);
CREATE POLICY "Employees with settings_update can update themes" ON "theme"
FOR UPDATE
diff --git a/packages/database/supabase/seed.sql b/packages/database/supabase/seed.sql
index c9c2140f2c..25a7a0ea53 100644
--- a/packages/database/supabase/seed.sql
+++ b/packages/database/supabase/seed.sql
@@ -6,24 +6,10 @@ VALUES ('system', '[email protected]', 'System', 'Operation');
-- theme
INSERT INTO "theme" (
- "primaryBackgroundLight",
- "primaryForegroundLight",
- "primaryBackgroundDark",
- "primaryForegroundDark",
- "accentBackgroundLight",
- "accentForegroundLight",
- "accentBackgroundDark",
- "accentForegroundDark",
+ "theme",
"updatedBy"
) VALUES (
- '240 5.9% 10%',
- '0 0% 98%',
- '0 0% 98%',
- '240 5.9% 10%',
- '240 4.8% 89.9%',
- '240 5.9% 10%',
- '240 3.7% 15.9%',
- '0 0% 98%',
+ 'zinc',
'system'
);
diff --git a/packages/react/src/Button.tsx b/packages/react/src/Button.tsx
index e48a3dfc16..3325fc28f7 100644
--- a/packages/react/src/Button.tsx
+++ b/packages/react/src/Button.tsx
@@ -15,13 +15,13 @@ const buttonVariants = cva(
primary:
"bg-primary text-primary-foreground hover:bg-primary/90 shadow-sm",
secondary:
- "border border-input text-primary bg-background hover:bg-foreground/[0.05] shadow-sm",
- solid: "bg-accent text-accent-foreground hover:bg-accent/80",
+ "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
+ solid: "bg-primary/25 text-accent-foreground hover:bg-primary/35",
destructive:
"bg-destructive text-destructive-foreground hover:bg-destructive/90 shadow-sm",
ghost:
- "bg-transparent hover:bg-accent text-accent-foreground hover:text-accent-foreground/90",
- link: "text-primary hover:text-primary underline-offset-4 hover:underline",
+ "bg-transparent hover:bg-primary/10 text-accent-foreground hover:text-accent-foreground/90",
+ link: "text-foreground hover:text-foreground underline-offset-4 hover:underline",
},
size: {
md: "h-8 rounded-md text-sm",
diff --git a/packages/react/src/Hyperlink.tsx b/packages/react/src/Hyperlink.tsx
index 8c272f6a01..fac7dd35fe 100644
--- a/packages/react/src/Hyperlink.tsx
+++ b/packages/react/src/Hyperlink.tsx
@@ -10,7 +10,10 @@ const Hyperlink = forwardRef<ElementRef<"a">, ComponentPropsWithoutRef<"a">>(
{...props}
href={href}
ref={ref}
- className={cn("text-primary hover:underline cursor-pointer", className)}
+ className={cn(
+ "text-foreground hover:underline cursor-pointer",
+ className
+ )}
>
{children}
</a>
@@ -18,7 +21,10 @@ const Hyperlink = forwardRef<ElementRef<"a">, ComponentPropsWithoutRef<"a">>(
<span
{...props}
ref={ref}
- className={cn("text-primary hover:underline cursor-pointer", className)}
+ className={cn(
+ "text-foreground hover:underline cursor-pointer",
+ className
+ )}
>
{children}
</span>
diff --git a/packages/utils/package.json b/packages/utils/package.json
index 84ed0bf709..9710d06aff 100644
--- a/packages/utils/package.json
+++ b/packages/utils/package.json
@@ -20,10 +20,14 @@
"jest": {
"preset": "@carbon/jest/node"
},
+ "dependencies": {
+ "lodash.template": "^4.5.0"
+ },
"devDependencies": {
"@carbon/jest": "*",
"@carbon/tsconfig": "*",
"@types/jest": "^26.0.22",
+ "@types/lodash.template": "4.5.3",
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6",
"eslint": "^8.23.1",
diff --git a/packages/utils/src/color.ts b/packages/utils/src/color.ts
deleted file mode 100644
index 88c120e8dc..0000000000
--- a/packages/utils/src/color.ts
+++ /dev/null
@@ -1,86 +0,0 @@
-export function hexToHls(hex: string) {
- const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6}|[0-9a-fA-f]{8})$/;
- let sColor = hex.toLowerCase();
- if (sColor && reg.test(sColor)) {
- if (sColor.length === 4) {
- let sColorNew = "#";
- for (let i = 1; i < 4; i += 1) {
- sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
- }
- sColor = sColorNew;
- }
-
- const sColorChange = [];
- for (let i = 1; i < 7; i += 2) {
- sColorChange.push(parseInt(`0x${sColor.slice(i, i + 2)}`));
- }
- const r = sColorChange[0] / 255;
- const g = sColorChange[1] / 255;
- const b = sColorChange[2] / 255;
- const max = Math.max(r, g, b);
- const min = Math.min(r, g, b);
- let h = 0;
- let s = 0;
- const l = (max + min) / 2;
- if (max === min) {
- h = 0;
- s = 0;
- } else {
- const d = max - min;
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
- switch (max) {
- case r:
- h = (g - b) / d + (g < b ? 6 : 0);
- break;
- case g:
- h = (b - r) / d + 2;
- break;
- case b:
- h = (r - g) / d + 4;
- break;
- default:
- break;
- }
- h /= 6;
- }
- return `${Math.floor(h * 360)} ${Math.floor(s * 100)}% ${Math.floor(
- l * 100
- )}%)`;
- }
-}
-
-function hue2rgb(p: number, q: number, t: number) {
- if (t < 0) t += 1;
- if (t > 1) t -= 1;
- if (t < 1 / 6) return p + (q - p) * 6 * t;
- if (t < 1 / 2) return q;
- if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
- return p;
-}
-
-export function hslToHex(hsl: string) {
- let [h, s, l] = hsl
- .split(" ")
- .map((n) => n.split("%")[0])
- .map(Number);
-
- const h1 = h / 360;
- const s1 = s / 100;
- const l1 = l / 100;
- let r = 0;
- let g = 0;
- let b = 0;
- if (s1 === 0) {
- r = g = b = l1;
- } else {
- const q = l1 < 0.5 ? l1 * (1 + s1) : l1 + s1 - l1 * s1;
- const p = 2 * l1 - q;
- r = hue2rgb(p, q, h1 + 1 / 3);
- g = hue2rgb(p, q, h1);
- b = hue2rgb(p, q, h1 - 1 / 3);
- }
- const hex = `#${Math.floor(r * 255).toString(16)}${Math.floor(
- g * 255
- ).toString(16)}${Math.floor(b * 255).toString(16)}`;
- return hex;
-}
diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts
index 50050cb69c..ac9ee6afd8 100644
--- a/packages/utils/src/index.ts
+++ b/packages/utils/src/index.ts
@@ -1,9 +1,9 @@
export * from "./accounting";
-export * from "./color";
export * from "./date";
export * from "./file";
export * from "./geo";
export * from "./math";
export * from "./ssr";
+export * from "./themes";
export * from "./types";
export * from "./url";
diff --git a/packages/utils/src/themes.ts b/packages/utils/src/themes.ts
new file mode 100644
index 0000000000..3065a442b1
--- /dev/null
+++ b/packages/utils/src/themes.ts
@@ -0,0 +1,531 @@
+import template from "lodash.template";
+
+export const themes = [
+ {
+ name: "zinc",
+ label: "Zinc",
+ activeColor: {
+ light: "240 5.9% 10%",
+ dark: "240 5.2% 33.9%",
+ },
+ cssVars: {
+ light: {
+ background: "0 0% 100%",
+ foreground: "240 10% 3.9%",
+ card: "0 0% 100%",
+ "card-foreground": "240 10% 3.9%",
+ popover: "0 0% 100%",
+ "popover-foreground": "240 10% 3.9%",
+ primary: "240 5.9% 10%",
+ "primary-foreground": "0 0% 98%",
+ secondary: "240 4.8% 95.9%",
+ "secondary-foreground": "240 5.9% 10%",
+ muted: "240 4.8% 95.9%",
+ "muted-foreground": "240 3.8% 46.1%",
+ accent: "240 4.8% 95.9%",
+ "accent-foreground": "240 5.9% 10%",
+ destructive: "0 84.2% 60.2%",
+ "destructive-foreground": "0 0% 98%",
+ border: "240 5.9% 90%",
+ input: "240 5.9% 90%",
+ ring: "240 5.9% 10%",
+ },
+ dark: {
+ background: "240 10% 3.9%",
+ foreground: "0 0% 98%",
+ card: "240 10% 3.9%",
+ "card-foreground": "0 0% 98%",
+ popover: "240 10% 3.9%",
+ "popover-foreground": "0 0% 98%",
+ primary: "0 0% 98%",
+ "primary-foreground": "240 5.9% 10%",
+ secondary: "240 3.7% 15.9%",
+ "secondary-foreground": "0 0% 98%",
+ muted: "240 3.7% 15.9%",
+ "muted-foreground": "240 5% 64.9%",
+ accent: "240 3.7% 15.9%",
+ "accent-foreground": "0 0% 98%",
+ destructive: "0 62.8% 30.6%",
+ "destructive-foreground": "0 0% 98%",
+ border: "240 3.7% 15.9%",
+ input: "240 3.7% 15.9%",
+ ring: "240 4.9% 83.9%",
+ },
+ },
+ },
+ {
+ name: "neutral",
+ label: "Neutral",
+ activeColor: {
+ light: "0 0% 45.1%",
+ dark: "0 0% 32.2%",
+ },
+ cssVars: {
+ light: {
+ background: "0 0% 100%",
+ foreground: "0 0% 3.9%",
+ card: "0 0% 100%",
+ "card-foreground": "0 0% 3.9%",
+ popover: "0 0% 100%",
+ "popover-foreground": "0 0% 3.9%",
+ primary: "0 0% 9%",
+ "primary-foreground": "0 0% 98%",
+ secondary: "0 0% 96.1%",
+ "secondary-foreground": "0 0% 9%",
+ muted: "0 0% 96.1%",
+ "muted-foreground": "0 0% 45.1%",
+ accent: "0 0% 96.1%",
+ "accent-foreground": "0 0% 9%",
+ destructive: "0 84.2% 60.2%",
+ "destructive-foreground": "0 0% 98%",
+ border: "0 0% 89.8%",
+ input: "0 0% 89.8%",
+ ring: "0 0% 3.9%",
+ },
+ dark: {
+ background: "0 0% 3.9%",
+ foreground: "0 0% 98%",
+ card: "0 0% 3.9%",
+ "card-foreground": "0 0% 98%",
+ popover: "0 0% 3.9%",
+ "popover-foreground": "0 0% 98%",
+ primary: "0 0% 98%",
+ "primary-foreground": "0 0% 9%",
+ secondary: "0 0% 14.9%",
+ "secondary-foreground": "0 0% 98%",
+ muted: "0 0% 14.9%",
+ "muted-foreground": "0 0% 63.9%",
+ accent: "0 0% 14.9%",
+ "accent-foreground": "0 0% 98%",
+ destructive: "0 62.8% 30.6%",
+ "destructive-foreground": "0 0% 98%",
+ border: "0 0% 14.9%",
+ input: "0 0% 14.9%",
+ ring: "0 0% 83.1%",
+ },
+ },
+ },
+ {
+ name: "red",
+ label: "Red",
+ activeColor: {
+ light: "0 72.2% 50.6%",
+ dark: "0 72.2% 50.6%",
+ },
+ cssVars: {
+ light: {
+ background: "0 0% 100%",
+ foreground: "0 0% 3.9%",
+ card: "0 0% 100%",
+ "card-foreground": "0 0% 3.9%",
+ popover: "0 0% 100%",
+ "popover-foreground": "0 0% 3.9%",
+ primary: "0 72.2% 50.6%",
+ "primary-foreground": "0 85.7% 97.3%",
+ secondary: "0 0% 96.1%",
+ "secondary-foreground": "0 0% 9%",
+ muted: "0 0% 96.1%",
+ "muted-foreground": "0 0% 45.1%",
+ accent: "0 0% 96.1%",
+ "accent-foreground": "0 0% 9%",
+ destructive: "0 84.2% 60.2%",
+ "destructive-foreground": "0 0% 98%",
+ border: "0 0% 89.8%",
+ input: "0 0% 89.8%",
+ ring: "0 72.2% 50.6%",
+ },
+ dark: {
+ background: "0 0% 3.9%",
+ foreground: "0 0% 98%",
+ card: "0 0% 3.9%",
+ "card-foreground": "0 0% 98%",
+ popover: "0 0% 3.9%",
+ "popover-foreground": "0 0% 98%",
+ primary: "0 72.2% 50.6%",
+ "primary-foreground": "0 85.7% 97.3%",
+ secondary: "0 0% 14.9%",
+ "secondary-foreground": "0 0% 98%",
+ muted: "0 0% 14.9%",
+ "muted-foreground": "0 0% 63.9%",
+ accent: "0 0% 14.9%",
+ "accent-foreground": "0 0% 98%",
+ destructive: "0 62.8% 30.6%",
+ "destructive-foreground": "0 0% 98%",
+ border: "0 0% 14.9%",
+ input: "0 0% 14.9%",
+ ring: "0 72.2% 50.6%",
+ },
+ },
+ },
+ {
+ name: "rose",
+ label: "Rose",
+ activeColor: {
+ light: "346.8 77.2% 49.8%",
+ dark: "346.8 77.2% 49.8%",
+ },
+ cssVars: {
+ light: {
+ background: "0 0% 100%",
+ foreground: "240 10% 3.9%",
+ card: "0 0% 100%",
+ "card-foreground": "240 10% 3.9%",
+ popover: "0 0% 100%",
+ "popover-foreground": "240 10% 3.9%",
+ primary: "346.8 77.2% 49.8%",
+ "primary-foreground": "355.7 100% 97.3%",
+ secondary: "240 4.8% 95.9%",
+ "secondary-foreground": "240 5.9% 10%",
+ muted: "240 4.8% 95.9%",
+ "muted-foreground": "240 3.8% 46.1%",
+ accent: "240 4.8% 95.9%",
+ "accent-foreground": "240 5.9% 10%",
+ destructive: "0 84.2% 60.2%",
+ "destructive-foreground": "0 0% 98%",
+ border: "240 5.9% 90%",
+ input: "240 5.9% 90%",
+ ring: "346.8 77.2% 49.8%",
+ },
+ dark: {
+ background: "20 14.3% 4.1%",
+ foreground: "0 0% 95%",
+ popover: "0 0% 9%",
+ "popover-foreground": "0 0% 95%",
+ card: "24 9.8% 10%",
+ "card-foreground": "0 0% 95%",
+ primary: "346.8 77.2% 49.8%",
+ "primary-foreground": "355.7 100% 97.3%",
+ secondary: "240 3.7% 15.9%",
+ "secondary-foreground": "0 0% 98%",
+ muted: "0 0% 15%",
+ "muted-foreground": "240 5% 64.9%",
+ accent: "12 6.5% 15.1%",
+ "accent-foreground": "0 0% 98%",
+ destructive: "0 62.8% 30.6%",
+ "destructive-foreground": "0 85.7% 97.3%",
+ border: "240 3.7% 15.9%",
+ input: "240 3.7% 15.9%",
+ ring: "346.8 77.2% 49.8%",
+ },
+ },
+ },
+ {
+ name: "orange",
+ label: "Orange",
+ activeColor: {
+ light: "24.6 95% 53.1%",
+ dark: "20.5 90.2% 48.2%",
+ },
+ cssVars: {
+ light: {
+ background: "0 0% 100%",
+ foreground: "20 14.3% 4.1%",
+ card: "0 0% 100%",
+ "card-foreground": "20 14.3% 4.1%",
+ popover: "0 0% 100%",
+ "popover-foreground": "20 14.3% 4.1%",
+ primary: "24.6 95% 53.1%",
+ "primary-foreground": "60 9.1% 97.8%",
+ secondary: "60 4.8% 95.9%",
+ "secondary-foreground": "24 9.8% 10%",
+ muted: "60 4.8% 95.9%",
+ "muted-foreground": "25 5.3% 44.7%",
+ accent: "60 4.8% 95.9%",
+ "accent-foreground": "24 9.8% 10%",
+ destructive: "0 84.2% 60.2%",
+ "destructive-foreground": "60 9.1% 97.8%",
+ border: "20 5.9% 90%",
+ input: "20 5.9% 90%",
+ ring: "24.6 95% 53.1%",
+ },
+ dark: {
+ background: "20 14.3% 4.1%",
+ foreground: "60 9.1% 97.8%",
+ card: "20 14.3% 4.1%",
+ "card-foreground": "60 9.1% 97.8%",
+ popover: "20 14.3% 4.1%",
+ "popover-foreground": "60 9.1% 97.8%",
+ primary: "20.5 90.2% 48.2%",
+ "primary-foreground": "60 9.1% 97.8%",
+ secondary: "12 6.5% 15.1%",
+ "secondary-foreground": "60 9.1% 97.8%",
+ muted: "12 6.5% 15.1%",
+ "muted-foreground": "24 5.4% 63.9%",
+ accent: "12 6.5% 15.1%",
+ "accent-foreground": "60 9.1% 97.8%",
+ destructive: "0 72.2% 50.6%",
+ "destructive-foreground": "60 9.1% 97.8%",
+ border: "12 6.5% 15.1%",
+ input: "12 6.5% 15.1%",
+ ring: "20.5 90.2% 48.2%",
+ },
+ },
+ },
+ {
+ name: "green",
+ label: "Green",
+ activeColor: {
+ light: "142.1 76.2% 36.3%",
+ dark: "142.1 70.6% 45.3%",
+ },
+ cssVars: {
+ light: {
+ background: "0 0% 100%",
+ foreground: "240 10% 3.9%",
+ card: "0 0% 100%",
+ "card-foreground": "240 10% 3.9%",
+ popover: "0 0% 100%",
+ "popover-foreground": "240 10% 3.9%",
+ primary: "142.1 76.2% 36.3%",
+ "primary-foreground": "355.7 100% 97.3%",
+ secondary: "240 4.8% 95.9%",
+ "secondary-foreground": "240 5.9% 10%",
+ muted: "240 4.8% 95.9%",
+ "muted-foreground": "240 3.8% 46.1%",
+ accent: "240 4.8% 95.9%",
+ "accent-foreground": "240 5.9% 10%",
+ destructive: "0 84.2% 60.2%",
+ "destructive-foreground": "0 0% 98%",
+ border: "240 5.9% 90%",
+ input: "240 5.9% 90%",
+ ring: "142.1 76.2% 36.3%",
+ },
+ dark: {
+ background: "20 14.3% 4.1%",
+ foreground: "0 0% 95%",
+ popover: "0 0% 9%",
+ "popover-foreground": "0 0% 95%",
+ card: "24 9.8% 10%",
+ "card-foreground": "0 0% 95%",
+ primary: "142.1 70.6% 45.3%",
+ "primary-foreground": "144.9 80.4% 10%",
+ secondary: "240 3.7% 15.9%",
+ "secondary-foreground": "0 0% 98%",
+ muted: "0 0% 15%",
+ "muted-foreground": "240 5% 64.9%",
+ accent: "12 6.5% 15.1%",
+ "accent-foreground": "0 0% 98%",
+ destructive: "0 62.8% 30.6%",
+ "destructive-foreground": "0 85.7% 97.3%",
+ border: "240 3.7% 15.9%",
+ input: "240 3.7% 15.9%",
+ ring: "142.4 71.8% 29.2%",
+ },
+ },
+ },
+ {
+ name: "blue",
+ label: "Blue",
+ activeColor: {
+ light: "221.2 83.2% 53.3%",
+ dark: "217.2 91.2% 59.8%",
+ },
+ cssVars: {
+ light: {
+ background: "0 0% 100%",
+ foreground: "222.2 84% 4.9%",
+ card: "0 0% 100%",
+ "card-foreground": "222.2 84% 4.9%",
+ popover: "0 0% 100%",
+ "popover-foreground": "222.2 84% 4.9%",
+ primary: "221.2 83.2% 53.3%",
+ "primary-foreground": "210 40% 98%",
+ secondary: "210 40% 96.1%",
+ "secondary-foreground": "222.2 47.4% 11.2%",
+ muted: "210 40% 96.1%",
+ "muted-foreground": "215.4 16.3% 46.9%",
+ accent: "210 40% 96.1%",
+ "accent-foreground": "222.2 47.4% 11.2%",
+ destructive: "0 84.2% 60.2%",
+ "destructive-foreground": "210 40% 98%",
+ border: "214.3 31.8% 91.4%",
+ input: "214.3 31.8% 91.4%",
+ ring: "221.2 83.2% 53.3%",
+ },
+ dark: {
+ background: "222.2 84% 4.9%",
+ foreground: "210 40% 98%",
+ card: "222.2 84% 4.9%",
+ "card-foreground": "210 40% 98%",
+ popover: "222.2 84% 4.9%",
+ "popover-foreground": "210 40% 98%",
+ primary: "217.2 91.2% 59.8%",
+ "primary-foreground": "222.2 47.4% 11.2%",
+ secondary: "217.2 32.6% 17.5%",
+ "secondary-foreground": "210 40% 98%",
+ muted: "217.2 32.6% 17.5%",
+ "muted-foreground": "215 20.2% 65.1%",
+ accent: "217.2 32.6% 17.5%",
+ "accent-foreground": "210 40% 98%",
+ destructive: "0 62.8% 30.6%",
+ "destructive-foreground": "210 40% 98%",
+ border: "217.2 32.6% 17.5%",
+ input: "217.2 32.6% 17.5%",
+ ring: "224.3 76.3% 48%",
+ },
+ },
+ },
+ {
+ name: "yellow",
+ label: "Yellow",
+ activeColor: {
+ light: "47.9 95.8% 53.1%",
+ dark: "47.9 95.8% 53.1%",
+ },
+ cssVars: {
+ light: {
+ background: "0 0% 100%",
+ foreground: "20 14.3% 4.1%",
+ card: "0 0% 100%",
+ "card-foreground": "20 14.3% 4.1%",
+ popover: "0 0% 100%",
+ "popover-foreground": "20 14.3% 4.1%",
+ primary: "47.9 95.8% 53.1%",
+ "primary-foreground": "26 83.3% 14.1%",
+ secondary: "60 4.8% 95.9%",
+ "secondary-foreground": "24 9.8% 10%",
+ muted: "60 4.8% 95.9%",
+ "muted-foreground": "25 5.3% 44.7%",
+ accent: "60 4.8% 95.9%",
+ "accent-foreground": "24 9.8% 10%",
+ destructive: "0 84.2% 60.2%",
+ "destructive-foreground": "60 9.1% 97.8%",
+ border: "20 5.9% 90%",
+ input: "20 5.9% 90%",
+ ring: "20 14.3% 4.1%",
+ },
+ dark: {
+ background: "20 14.3% 4.1%",
+ foreground: "60 9.1% 97.8%",
+ card: "20 14.3% 4.1%",
+ "card-foreground": "60 9.1% 97.8%",
+ popover: "20 14.3% 4.1%",
+ "popover-foreground": "60 9.1% 97.8%",
+ primary: "47.9 95.8% 53.1%",
+ "primary-foreground": "26 83.3% 14.1%",
+ secondary: "12 6.5% 15.1%",
+ "secondary-foreground": "60 9.1% 97.8%",
+ muted: "12 6.5% 15.1%",
+ "muted-foreground": "24 5.4% 63.9%",
+ accent: "12 6.5% 15.1%",
+ "accent-foreground": "60 9.1% 97.8%",
+ destructive: "0 62.8% 30.6%",
+ "destructive-foreground": "60 9.1% 97.8%",
+ border: "12 6.5% 15.1%",
+ input: "12 6.5% 15.1%",
+ ring: "35.5 91.7% 32.9%",
+ },
+ },
+ },
+ {
+ name: "violet",
+ label: "Violet",
+ activeColor: {
+ light: "262.1 83.3% 57.8%",
+ dark: "263.4 70% 50.4%",
+ },
+ cssVars: {
+ light: {
+ background: "0 0% 100%",
+ foreground: "224 71.4% 4.1%",
+ card: "0 0% 100%",
+ "card-foreground": "224 71.4% 4.1%",
+ popover: "0 0% 100%",
+ "popover-foreground": "224 71.4% 4.1%",
+ primary: "262.1 83.3% 57.8%",
+ "primary-foreground": "210 20% 98%",
+ secondary: "220 14.3% 95.9%",
+ "secondary-foreground": "220.9 39.3% 11%",
+ muted: "220 14.3% 95.9%",
+ "muted-foreground": "220 8.9% 46.1%",
+ accent: "220 14.3% 95.9%",
+ "accent-foreground": "220.9 39.3% 11%",
+ destructive: "0 84.2% 60.2%",
+ "destructive-foreground": "210 20% 98%",
+ border: "220 13% 91%",
+ input: "220 13% 91%",
+ ring: "262.1 83.3% 57.8%",
+ },
+ dark: {
+ background: "224 71.4% 4.1%",
+ foreground: "210 20% 98%",
+ card: "224 71.4% 4.1%",
+ "card-foreground": "210 20% 98%",
+ popover: "224 71.4% 4.1%",
+ "popover-foreground": "210 20% 98%",
+ primary: "263.4 70% 50.4%",
+ "primary-foreground": "210 20% 98%",
+ secondary: "215 27.9% 16.9%",
+ "secondary-foreground": "210 20% 98%",
+ muted: "215 27.9% 16.9%",
+ "muted-foreground": "217.9 10.6% 64.9%",
+ accent: "215 27.9% 16.9%",
+ "accent-foreground": "210 20% 98%",
+ destructive: "0 62.8% 30.6%",
+ "destructive-foreground": "210 20% 98%",
+ border: "215 27.9% 16.9%",
+ input: "215 27.9% 16.9%",
+ ring: "263.4 70% 50.4%",
+ },
+ },
+ },
+] as const;
+
+export type Theme = (typeof themes)[number];
+
+export const BASE_THEME_WITH_VARIABLES = `
+:root {
+ --background: <%- colors.light["background"] %>;
+ --foreground: <%- colors.light["foreground"] %>;
+ --card: <%- colors.light["card"] %>;
+ --card-foreground: <%- colors.light["card-foreground"] %>;
+ --popover: <%- colors.light["popover"] %>;
+ --popover-foreground: <%- colors.light["popover-foreground"] %>;
+ --primary: <%- colors.light["primary"] %>;
+ --primary-foreground: <%- colors.light["primary-foreground"] %>;
+ --secondary: <%- colors.light["secondary"] %>;
+ --secondary-foreground: <%- colors.light["secondary-foreground"] %>;
+ --muted: <%- colors.light["muted"] %>;
+ --muted-foreground: <%- colors.light["muted-foreground"] %>;
+ --accent: <%- colors.light["accent"] %>;
+ --accent-foreground: <%- colors.light["accent-foreground"] %>;
+ --destructive: <%- colors.light["destructive"] %>;
+ --destructive-foreground: <%- colors.light["destructive-foreground"] %>;
+ --border: <%- colors.light["border"] %>;
+ --input: <%- colors.light["input"] %>;
+ --ring: <%- colors.light["ring"] %>;
+ --radius: <%- radius %>rem;
+}
+
+.dark {
+ --background: <%- colors.dark["background"] %>;
+ --foreground: <%- colors.dark["foreground"] %>;
+ --card: <%- colors.dark["card"] %>;
+ --card-foreground: <%- colors.dark["card-foreground"] %>;
+ --popover: <%- colors.dark["popover"] %>;
+ --popover-foreground: <%- colors.dark["popover-foreground"] %>;
+ --primary: <%- colors.dark["primary"] %>;
+ --primary-foreground: <%- colors.dark["primary-foreground"] %>;
+ --secondary: <%- colors.dark["secondary"] %>;
+ --secondary-foreground: <%- colors.dark["secondary-foreground"] %>;
+ --muted: <%- colors.dark["muted"] %>;
+ --muted-foreground: <%- colors.dark["muted-foreground"] %>;
+ --accent: <%- colors.dark["accent"] %>;
+ --accent-foreground: <%- colors.dark["accent-foreground"] %>;
+ --destructive: <%- colors.dark["destructive"] %>;
+ --destructive-foreground: <%- colors.dark["destructive-foreground"] %>;
+ --border: <%- colors.dark["border"] %>;
+ --input: <%- colors.dark["input"] %>;
+ --ring: <%- colors.dark["ring"] %>;
+}`;
+
+export function getThemeCode(theme: Theme) {
+ if (!theme) {
+ return "";
+ }
+
+ return template(BASE_THEME_WITH_VARIABLES)({
+ colors: theme.cssVars,
+ radius: 0.5,
+ });
+}