import { z } from "zod"; import chroma from "chroma-js"; export const fontStackEnum = z.enum([ "system-ui", "transitional", "old-style", "humanist", "geometric-humanist", "classical-humanist", "neo-grotesque", "monospace-slab-serif", "monospace-code", "industrial", "rounded-sans", "slab-serif", "antique", "didone", "handwritten", ]); export type FontStack = z.infer; export const fontStacksLabels: Record = { "system-ui": "System UI", transitional: "Transitional", "old-style": "Old Style", humanist: "Humanist", "geometric-humanist": "Geometric Humanist", "classical-humanist": "Classical Humanist", "neo-grotesque": "Neo-Grotesque", "monospace-slab-serif": "Monospace Slab Serif", "monospace-code": "Monospace Code", industrial: "Industrial", "rounded-sans": "Rounded Sans", "slab-serif": "Slab Serif", antique: "Antique", didone: "Didone", handwritten: "Handwritten", }; export const fontStacksFonts: Record = { "system-ui": "system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif", transitional: "Charter, 'Bitstream Charter', 'Sitka Text', Cambria, serif", "old-style": "'Iowan Old Style', 'Palatino Linotype', 'URW Palladio L', P052, serif", humanist: "Seravek, 'Gill Sans Nova', Ubuntu, Calibri, 'DejaVu Sans', source-sans-pro, sans-serif", "geometric-humanist": "Avenir, Montserrat, Corbel, 'URW Gothic', source-sans-pro, sans-serif", "classical-humanist": "Optima, Candara, 'Noto Sans', source-sans-pro, sans-serif", "neo-grotesque": "'Helvetica Neue', Helvetica, 'Arial Nova', Nimbus Sans', Arial, sans-serif", "monospace-slab-serif": "'Courier New', Courier, 'Lucida Console', monospace", "monospace-code": "'Source Code Pro', 'Fira Code', 'Consolas', 'Courier New', monospace", industrial: "'Eurostile', 'Bank Gothic', 'Microgramma', sans-serif", "rounded-sans": "'VAG Rounded', 'Arial Rounded MT Bold', 'Quicksand', sans-serif", "slab-serif": "Rockwell, 'Rockwell Nova', 'Roboto Slab', 'DejaVu Serif', 'Sitka Small', serif", antique: "'Baskerville', 'Caslon', Superclarendon, 'Bookman Old Style', 'URW Bookman', 'URW Bookman L', 'Georgia Pro', Georgia, serif", didone: "Didot, 'Bodoni MT', 'Noto Serif Display', 'URW Palladio L', P052, Sylfaen serif", handwritten: "'Segoe Print', 'Bradley Hand', Chilanka, TSCu_Comic, casual, cursive", }; const googleFont = z .object({ type: z.literal("google"), family: z.string().min(1).max(100), weights: z .array(z.number().min(100).max(900)) .min(2) .describe( "Weights range [min, max]. E.g. [300, 700] loads 300, 400, 500, 600, 700 (weights the font does not have are skipped). Single-weight display fonts (Anton, Bebas Neue, Instrument Serif…) take [400, 400].", ) .optional(), }) .describe(`Google font with family and optional weights range. IMPORTANT: - Be careful to only use fonts available on Google Fonts. - weights must be an array of at least 2 numbers `); export type GoogleFont = z.infer; const stackFont = z .object({ type: z.literal("stack"), family: fontStackEnum, }) .describe("Font stack based on common font categories"); const genericFont = z.discriminatedUnion("type", [stackFont, googleFont]); export type GenericFont = z.infer; /** * Schema representing a theme */ export const themeSchema = z.strictObject({ id: z .string() .regex(/^[a-z](-?[a-z0-9])*$/) .describe( "The unique identifier of the theme. Use a lowercase, hyphen-separated string. Do not start with a number.", ), name: z.string().describe("The name of the theme"), description: z.string().describe("The description of the theme"), tags: z.array(z.string()).describe("The tags of the theme"), browserColorScheme: z .enum(["light", "dark"]) .describe("Color of browser-provided UI. Either 'light' or 'dark'"), // Define the theme colors colors: z .strictObject({ // Base colors base100: z .string() .describe( "Base surface color of page, used for blank backgrounds. Should be white or near-white for light color-schemes, and black or near-black for dark color-schemes.", ), base200: z .string() .describe( "Should be darker than base 100 but still light for light color-schemes, and lighter but still dark for dark color-schemes.", ), base300: z .string() .describe( "3rd base color, should be darker than base 200 for light color-schemes, and lighter than base 200 for dark color-schemes.", ), baseContent: z .string() .describe("The color used for text/icons placed on top of base color backgrounds."), // Accent colors primary: z.string().describe("The brand's primary color."), primaryContent: z .string() .describe("The color used for text/icons placed on top of primary color backgrounds."), secondary: z.string().describe("The brand's second most used color."), secondaryContent: z .string() .describe("The color used for text/icons placed on top of secondary color backgrounds."), accent: z.string().describe("The brand's least used color."), accentContent: z .string() .describe("The color used for text/icons placed on top of accent color backgrounds."), neutral: z.string().describe("The base neutral color."), neutralContent: z .string() .describe("The color used for text/icons placed on top of neutral color backgrounds."), info: z.string().describe("Color used to convey information."), infoContent: z .string() .describe("The color used for text/icons placed on top of info color backgrounds."), success: z.string().describe("Color used to convey success."), successContent: z .string() .describe("The color used for text/icons placed on top of success color backgrounds."), warning: z.string().describe("Color used to convey warnings."), warningContent: z .string() .describe("The color used for text/icons placed on top of warning color backgrounds."), error: z.string().describe("Color used to convey errors."), errorContent: z .string() .describe("The color used for text/icons placed on top of error color backgrounds."), }) .describe("The colors of the theme. Use oklch() css notation when possible."), // Define the theme typography typography: z.strictObject({ base: z .number() .optional() .default(16) .describe("The base font size in pixels. It is safe to keep it as is. A safe value is 16."), sans: genericFont .default({ type: "stack", family: "system-ui", }) .optional() .describe( `The sans-serif font for the theme. You will define 'sans' or 'serif', or both. Default is system-ui stack. A font can be of type 'stack' or 'google'. - 'stack' fonts use common font stacks. - 'google' fonts load from Google Fonts. IMPORTANT note for Google fonts: - Be careful to only use fonts available on Google Fonts. - 'weights' must be an array of exactly 2 numbers representing the min and max weights to load `, ), serif: genericFont .default({ type: "stack", family: "transitional", }) .optional() .describe( "The serif font for the theme. You will define 'sans' or 'serif', or both. Default is transitional stack.", ), others: z .record(z.string(), genericFont) .optional() .describe( "Other fonts for the theme. Keyed by any name, will be transformed in tailwind's 'font-' class", ), }), radiusSelector: z .string() .optional() .default("2rem") .describe( "Border radius of selectors (checkbox, toggle, badge). Set from the site's art direction: 0 for sharp, 0.25rem subtle, 1rem soft, 2rem pill.", ), radiusField: z .string() .optional() .default("0.25rem") .describe( "Border radius of fields (button, input, select, tab). Set from the site's art direction: 0 for sharp, 0.25rem subtle, 0.75rem soft, 9999px pill.", ), radiusBox: z .string() .optional() .default("0.5rem") .describe( "Border radius of boxes (card, modal, alert). Set from the site's art direction: 0 for sharp, 0.25-0.5rem subtle, 1-2rem soft.", ), sizeSelector: z .string() .optional() .default("0.25rem") .describe( "Base size of selectors (checkbox, toggle, badge). Value must be 0.25rem unless we intentionally want bigger or smaller selectors. Recommend 0.25rem.", ), sizeField: z .string() .optional() .default("0.25rem") .describe( "Base size of fields (button, input, select, tab). Value must be 0.25rem unless we intentionally want bigger or smaller fields. Recommend 0.25rem.", ), border: z .string() .optional() .default("1px") .describe("Border size. 1px by default; 2px for bold, graphic directions (poster, brutalist, playful)."), depth: z.boolean().optional().default(false).describe("'false' for flat design, 'true' for depth effect."), }); export type Theme = z.infer; export const themesArray = z.array(themeSchema); export type ThemesArray = z.infer; export const defaultTheme = themeSchema.parse({ id: "midnight-aurora", name: "Midnight Aurora", description: "A sophisticated dark theme inspired by the northern lights, featuring deep blues and vibrant accent colors that create an elegant, modern aesthetic.", tags: ["dark", "modern", "professional", "tech", "elegant"], browserColorScheme: "dark", colors: { base100: "oklch(0.15 0.02 260)", base200: "oklch(0.20 0.025 260)", base300: "oklch(0.25 0.03 260)", baseContent: "oklch(0.93 0.01 260)", primary: "oklch(0.65 0.20 250)", primaryContent: "oklch(0.98 0.005 250)", secondary: "oklch(0.70 0.15 180)", secondaryContent: "oklch(0.15 0.03 180)", accent: "oklch(0.75 0.18 330)", accentContent: "oklch(0.15 0.03 330)", neutral: "oklch(0.35 0.02 260)", neutralContent: "oklch(0.90 0.01 260)", info: "oklch(0.70 0.15 230)", infoContent: "oklch(0.15 0.02 230)", success: "oklch(0.70 0.18 145)", successContent: "oklch(0.15 0.03 145)", warning: "oklch(0.80 0.16 85)", warningContent: "oklch(0.20 0.03 85)", error: "oklch(0.65 0.20 25)", errorContent: "oklch(0.98 0.01 25)", }, typography: { base: 16, sans: { type: "google", family: "Inter", weights: [300, 700], }, serif: { type: "google", family: "Playfair Display", weights: [300, 700], }, others: { mono: { type: "stack", family: "monospace-code", }, display: { type: "google", family: "Space Grotesk", weights: [300, 700], }, }, }, } satisfies z.input); export function isDefaultTheme(theme: Theme): boolean { return theme.id === defaultTheme.id; } /** * Process a theme, eventually fixing colors and translating them to oklch notations * @param theme */ export function processTheme(theme: Theme): Theme { const validTheme = themeSchema.parse(theme); return { ...validTheme, typography: { ...validTheme.typography, base: 16, // override any base size }, colors: Object.entries(validTheme.colors).reduce( (acc, [key, value]) => { const fixedColor = fixOklchColor(value); acc[key as keyof typeof validTheme.colors] = fixedColor; return acc; }, {} as typeof validTheme.colors, ), }; } function fixOklchColor(color: string) { const valid = chroma.valid(color); if (valid) { return color; } // Try to fix the color if it looks like oklch by removing commas sometimes added by mistake const oklchRegex = /ok(lch|lab)\(([^)]+)\)/; if (oklchRegex.test(color)) { const withoutComma = color.replace(/,/g, " "); if (chroma.valid(withoutComma)) { return withoutComma; } } return color; }