Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/material-tailwind.
Something went wrong. Try again.
material-tailwind index.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749import { argbFromHex, hexFromArgb, TonalPalette, SchemeContent, SchemeExpressive, SchemeFidelity, SchemeFruitSalad, SchemeMonochrome, SchemeNeutral, SchemeRainbow, SchemeTonalSpot, SchemeVibrant, Hct, MaterialDynamicColors, DynamicColor,} from "@material/material-color-utilities";import plugin from "tailwindcss/plugin.js";
import defaultConfiguration from "./default.config.js";import { defaultGamut, gamuts, oklchFromArgb, oklchFromHct } from "./color.js";
/** * @import {DynamicScheme} from "@material/material-color-utilities" * @import {Gamut} from "./color.js" */
/** * The scheme styles Material provides, keyed by the name used in the plugin * options. These are the "styles" in the Material Theme Builder. * @see https://material-foundation.github.io/material-theme-builder/ */const variants = { monochrome: SchemeMonochrome, neutral: SchemeNeutral, "tonal-spot": SchemeTonalSpot, vibrant: SchemeVibrant, expressive: SchemeExpressive, fidelity: SchemeFidelity, content: SchemeContent, rainbow: SchemeRainbow, "fruit-salad": SchemeFruitSalad,};
/** @typedef {keyof typeof variants} VariantName */
/** @type {VariantName} */const defaultVariant = "tonal-spot";
/** * The color spec versions material-color-utilities implements. "2025" is the * Material 3 Expressive spec and adds the "dim" colors. * `SpecVersion` itself is not re-exported from the package root, so the union * is declared here. * @typedef {"2021" | "2025"} SpecVersion */
/** * Typed as strings so an unvalidated option can be checked against it * @type {string[]} */const specVersions = ["2021", "2025"];
/** @type {SpecVersion} */const defaultSpecVersion = "2021";
/** * Whether the generated colors are added to Tailwind's own palette or take its * place. Material's palettes are named for their role, and two of those names * ("neutral" and "error") are also Tailwind color names, so in "extend" the two * scales interleave: Material defines the steps it has and Tailwind's survive * at the rest, leaving `bg-neutral-50` Material's mid grey while * `bg-neutral-500` is still Tailwind's. "replace" drops Tailwind's palette so * every color name means exactly one thing. * @typedef {"extend" | "replace"} ColorsMode */
/** * Typed as strings so an unvalidated option can be checked against it * @type {string[]} */const colorsModes = ["extend", "replace"];
/** * Extending is what the plugin has always done, so it stays the default and * nobody's colors move when they upgrade. * @type {ColorsMode} */const defaultColorsMode = "extend";
//TODO update Tailwind CSS to a version after 4.0.6 to fix types/** * @import {Config} from "tailwindcss" * @import {ThemeConfig} from "tailwindcss/plugin" */
/** * Convert camelCase to kebab-case * e.g. "neutralVariant" -> "neutral-variant" * @param {string} value * @returns {string} */function camelToKebabCase(value) { return value.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();}
/** * * @param {TonalPalette} palette * @param {Gamut} gamut * @returns {Generator<[number, string]>} */function* generatePaletteSteps(palette, gamut) { // Supported color steps // Material Design goes from 0 to 100 for lightness (like percent) // whereas Tailwind goes from 50 to 950 // We support material way of thinking here
// Create Tonal pallettes // See https://m3.material.io/styles/color/system/how-the-system-works#3ce9da92-a118-4692-8b2c-c5c52a413fa6 // And https://material-foundation.github.io/material-theme-builder/ //TODO check out tailwind dynamic color values and/or make configurable const materialPalletteSteps = [ 0, 5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 70, 80, 90, 95, 98, 99, 100, ];
for (const step of materialPalletteSteps) { yield [step, resolvePaletteStep(palette, step, gamut)]; }}
/** * TonalPalette averages tones 98 and 100 for tone 99 of a yellow palette, * which is an sRGB operation and not an HCT triple we could re-solve in a * wider gamut. It lands next to white with almost no chroma, so nothing is * lost by taking the sRGB answer for it. * @param {TonalPalette} palette * @param {number} tone * @returns {boolean} */function isAveragedTone(palette, tone) { return tone === 99 && Hct.isYellow(palette.hue);}
/** * @param {TonalPalette} palette * @param {number} tone * @param {Gamut} gamut * @returns {string} */function resolvePaletteStep(palette, tone, gamut) { if (gamut === "srgb") return hexFromArgb(palette.tone(tone)); if (isAveragedTone(palette, tone)) return oklchFromArgb(palette.tone(tone));
return oklchFromHct(palette.hue, palette.chroma, tone, gamut);}
/** @typedef {[name: string, palette: TonalPalette][]} PaletteArray *//** * * @param {PaletteArray} materialPalettes * @param {Gamut} gamut */function createPalettes(materialPalettes, gamut) { /** @type {Record<string, string>} */ const palettes = {}; for (let [name, palette] of materialPalettes) { name = camelToKebabCase(name);
for (const [step, color] of generatePaletteSteps(palette, gamut)) { palettes[`${name}-${step}`] = color; } }
return palettes;}
/** * The namespace Tailwind reads color utilities out of. Every token this plugin * emits is a `--color-*` variable, which is what Tailwind documents for adding * colors to a theme, so `var(--color-primary)` works in an arbitrary value the * same way `var(--color-red-500)` does. * @see https://tailwindcss.com/docs/theme */const namespace = "--color-";
/** * A Tailwind plugin cannot register real theme variables. `@theme` is only a * CSS-level directive: handed to `addBase` it is copied into the output * verbatim as an at-rule no browser understands, and no utilities come out of * it. So the theme value a utility inlines is made a `var()` reference and the * definition it points at is written separately, which reproduces what `@theme` * would have emitted. The one thing that cannot be reproduced is Tailwind's * tree shaking: every token ships whether or not a utility uses it. * @param {string} name * @returns {string} */function reference(name) { return `var(${namespace}${name})`;}
/** * The color accessors on MaterialDynamicColors. Every one of them is a method * taking no arguments that returns the DynamicColor for the scheme's spec * version, except `highestSurface`, which is a lookup helper taking a scheme, * and `allColors`, which is an array. `allColors` is an instance field rather * than a prototype member, so it only has to be excluded for the type. * @typedef {Exclude<keyof MaterialDynamicColors, "highestSurface" | "allColors">} ColorName */
/** * The deprecated statics on MaterialDynamicColors are deliberately not used * here. They only cover the 2021 spec and would silently omit the "dim" colors. * @type {ColorName[]} */const colorNames = /** @type {ColorName[]} */ ( Object.getOwnPropertyNames(MaterialDynamicColors.prototype).filter( (name) => name !== "constructor" && name !== "highestSurface", ));
/** * Resolves a color for a scheme, or undefined when the color does not exist in * that scheme's spec version (e.g. the "dim" colors before "2025"). * @param {DynamicScheme} scheme * @param {ColorName} name * @param {Gamut} gamut * @returns {string | undefined} */function resolveColor(scheme, name, gamut) { const color = scheme.colors[name](); if (!(color instanceof DynamicColor)) return undefined;
if (gamut === "srgb") return hexFromArgb(scheme.getArgb(color));
// The 2021 spec resolves through TonalPalette, so it inherits the averaged // tone 99 of a yellow palette if ( scheme.specVersion !== "2025" && isAveragedTone(color.palette(scheme), color.getTone(scheme)) ) return oklchFromArgb(scheme.getArgb(color));
return oklchFromHct(...requestedHct(scheme, color), gamut);}
/** * The hue, chroma and tone a dynamic color asks for, before `Hct.toInt` fits it * into sRGB. Going through the resolved ARGB instead would mean re-reading a * color chroma has already been clipped out of, so the wider gamut would have * nothing left to give back. * * This mirrors what the two spec versions' calculation delegates do: 2021 takes * the palette's own chroma, 2025 scales it by the color's chroma multiplier. * @param {DynamicScheme} scheme * @param {DynamicColor} color * @returns {[hue: number, chroma: number, tone: number]} */function requestedHct(scheme, color) { const palette = color.palette(scheme); const tone = color.getTone(scheme);
if (scheme.specVersion === "2025") { const multiplier = color.chromaMultiplier?.(scheme) ?? 1; return [palette.hue, palette.chroma * multiplier, tone]; }
return [palette.hue, palette.chroma, tone];}
/** * @typedef {object} Tokens * @property {Record<string, string | Record<string, string>>} colors * The Tailwind theme, every value a `var()` reference * @property {Record<string, string>} leaves * Tokens whose value is a literal color, keyed by the bare token name without * the `--color-` the variable gets when it is registered. These are the ones * worth overriding and the only ones that can be registered with `@property`. * The colors composed out of them live in the theme values instead. */
/** * Creates colors * @param {Schemes} schemes * @param {Gamut} gamut * @param {ContrastName[]} contrasts * @returns {Tokens} */function createColors(schemes, gamut, contrasts) { /** @type {Record<string, string | Record<string, string> >} */ const colors = {}; /** @type {Record<string, string>} */ const leaves = {};
for (const [schemeName, scheme] of Object.entries(schemes)) { /** @type {Record<string,string>} */ const schemeColors = {};
for (const name of colorNames) { const color = resolveColor(scheme, name, gamut); if (color === undefined) continue;
// Tailwind flattens a nested color object with a dash, so `light.primary` // is the `--color-light-primary` a `bg-light-primary` utility reads const token = `${camelToKebabCase(schemeName)}-${camelToKebabCase(name)}`; leaves[token] = color; schemeColors[camelToKebabCase(name)] = reference(token); }
colors[camelToKebabCase(schemeName)] = schemeColors; }
for (const name of colorNames) { for (const contrast of contrasts) { const { prefix, light, dark } = contrastLevels[contrast]; const lightScheme = schemes[light]; const darkScheme = schemes[dark]; if (lightScheme === undefined || darkScheme === undefined) continue;
const lightColor = resolveColor(lightScheme, name, gamut); const darkColor = resolveColor(darkScheme, name, gamut); if (lightColor === undefined || darkColor === undefined) continue;
const role = camelToKebabCase(name); const token = `${prefix}${role}`;
// Composed out of the per-scheme tokens rather than restating their // literals, so overriding `--color-light-primary` moves `--color-primary` // and every utility built on it too. // // It goes in the theme as a fallback rather than being declared anywhere. // A declaration would land in `@layer base`, which beats the // `@layer theme` a user's `@theme` override lands in, and would silently // ignore it. Only reading the variable means setting it anywhere wins, // and nothing ships for a color no utility uses. const composite = `light-dark(${reference(`${light}-${role}`)}, ${reference(`${dark}-${role}`)})`; colors[token] = `var(${namespace}${token}, ${composite})`; } }
return { colors, leaves };}
/** * @typedef {"light" | "dark"} Scheme * @typedef {"reduced" | "medium" | "high"} Contrast * @typedef {Scheme | `${Scheme}-${Contrast}-contrast`} SchemeName * @typedef {Partial<Record<SchemeName, DynamicScheme>>} Schemes */
/** * Every color role exists at four contrast levels. Only the default one is * generated unless asked for, because the other three are two thirds of * everything this plugin emits and most themes never use them. * * Contrast values are the ones recommended by * https://github.com/material-foundation/material-color-utilities/blob/9889de141b3b5194b8574f9e378e55f4428bdb5e/dev_guide/creating_color_scheme.md * @satisfies {Record<string, {contrast: number, prefix: string, light: SchemeName, dark: SchemeName}>} */const contrastLevels = { default: { contrast: 0, prefix: "", light: "light", dark: "dark" }, reduced: { contrast: -1, prefix: "reduced-contrast-", light: "light-reduced-contrast", dark: "dark-reduced-contrast", }, medium: { contrast: 0.5, prefix: "medium-contrast-", light: "light-medium-contrast", dark: "dark-medium-contrast", }, high: { contrast: 1, prefix: "high-contrast-", light: "light-high-contrast", dark: "dark-high-contrast", },};
/** @typedef {keyof typeof contrastLevels} ContrastName */
/** * The unqualified color roles come from the default level, so it is always * generated whether or not it was asked for. * @type {ContrastName} */const requiredContrast = "default";
/** * @param {string} sourceColor * @param {VariantName} variant * @param {SpecVersion} specVersion * @param {ContrastName[]} contrasts * @returns {Schemes} */function createSchemes(sourceColor, variant, specVersion, contrasts) { const color = Hct.fromInt(argbFromHex(sourceColor)); const Scheme = variants[variant];
/** @type {Schemes} */ const schemes = {}; for (const name of contrasts) { const { contrast, light, dark } = contrastLevels[name]; schemes[light] = new Scheme(color, false, contrast, specVersion); schemes[dark] = new Scheme(color, true, contrast, specVersion); }
return schemes;}
/** * Registering a custom property makes an override that is not a color fall back * to the generated default instead of poisoning every declaration that reads * it, and makes the token animatable, so a theme change can be transitioned. * * Only the leaves get this. A registered `<color>` property computes to a * resolved color at the element it is declared on, so a registered * `light-dark()` is frozen at the root's `color-scheme` and stops following a * `color-scheme: dark` subtree. The composites therefore have to stay * unregistered, which also rules out `initial-value`, as that may not contain * `var()`. * @param {Record<string, string>} leaves * @returns {Record<string, Record<string, string>>} */function registerProperties(leaves) { /** @type {Record<string, Record<string, string>>} */ const properties = {};
for (const [token, color] of Object.entries(leaves)) { properties[`@property ${namespace}${token}`] = { syntax: '"<color>"', // Colors are inherited, and Tailwind's own `--tw-*` internals are not, so // this cannot be left to the default inherits: "true", "initial-value": color, }; }
return properties;}
/** * @param {string} sourceColor * @param {VariantName} variant * @param {SpecVersion} specVersion * @param {Gamut} gamut * @param {ColorsMode} colorsMode * @param {ContrastName[]} contrasts * @returns {{theme: ThemeConfig, base: Record<string, Record<string, string>>}} */function createTheme( sourceColor, variant, specVersion, gamut, colorsMode, contrasts,) { const schemes = createSchemes(sourceColor, variant, specVersion, contrasts); const { colors, leaves } = createColors(schemes, gamut, contrasts);
// The palettes are the same for light and dark /** @type {PaletteArray} */ const sourcePalettes = Object.entries(schemes.light ?? {}) .filter(([, value]) => value instanceof TonalPalette) // Remove "palette" postfix .map(([key, value]) => [key.replace("Palette", ""), value]);
const palettes = createPalettes(sourcePalettes, gamut);
// The source color is an sRGB hex the user gave us, so there is no wider // gamut version of it to recover. It is only restated in the theme's format. leaves.source = gamut === "srgb" ? sourceColor : oklchFromArgb(argbFromHex(sourceColor));
/** @type {Record<string, string | Record<string, string>>} */ const themeColors = { source: reference("source"), ...colors }; for (const [token, color] of Object.entries(palettes)) { leaves[token] = color; themeColors[token] = reference(token); }
const theme = { ...defaultConfiguration };
if (colorsMode === "replace") { // `theme.colors` replaces Tailwind's palette instead of merging with it, so // nothing of Tailwind's is left to collide with a Material palette step. // The color keywords (`transparent`, `current`, `inherit`) are built into // the utilities rather than read from the theme and survive on their own, // but black and white do come from here and are too widely used to drop. theme.colors = { black: "#000000", white: "#ffffff", ...themeColors }; theme.extend = { ...theme.extend }; } else { theme.extend = { ...theme.extend, colors: themeColors }; }
// Only the leaves are written out. They carry their color as `initial-value`, // and the composites are fallbacks inside the theme values, so there is // nothing left to declare. return { theme, base: registerProperties(leaves) };}
class PluginOptionsUndefinedError extends Error { constructor() { super( "Please configure a source color in your Tailwind CSS file to use @claas.dev/material-tailwind e.g. `@plugin '@claas.dev/material-tailwind' { source-color: '#0c1445' }`", ); }}
class SourceColorUndefinedError extends Error { constructor() { super( "Please configure a source color in your Tailwind CSS file to use @claas.dev/material-tailwind e.g. `@plugin '@claas.dev/material-tailwind' { source-color: '#0c1445' }`", ); }}
class UnknownVariantError extends Error { /** @param {string} variant */ constructor(variant) { super( `"${variant}" is not a Material scheme variant. Pick one of: ${Object.keys(variants).join(", ")}.`, ); }}
class UnknownSpecVersionError extends Error { /** @param {string} specVersion */ constructor(specVersion) { super( `"${specVersion}" is not a Material color spec version. Pick one of: ${specVersions.join(", ")}.`, ); }}
class UnknownGamutError extends Error { /** @param {string} gamut */ constructor(gamut) { super( `"${gamut}" is not a supported gamut. Pick one of: ${gamuts.join(", ")}.`, ); }}
class UnknownColorsModeError extends Error { /** @param {string} colorsMode */ constructor(colorsMode) { super( `"${colorsMode}" is not a way to apply the colors. Pick one of: ${colorsModes.join(", ")}.`, ); }}
class UnknownContrastError extends Error { /** @param {string} contrast */ constructor(contrast) { super( `"${contrast}" is not a Material contrast level. Pick any of: ${Object.keys(contrastLevels).join(", ")}, or "all".`, ); }}
/** * Aliases accepted for a gamut, so the CSS can say what reads naturally. A Map * rather than an object so a name like "constructor" misses instead of * resolving to something inherited. * @type {Map<string, Gamut>} */const gamutAliases = new Map( /** @type {[string, Gamut][]} */ ([ ["srgb", "srgb"], ["s-rgb", "srgb"], ["p3", "display-p3"], ["display-p3", "display-p3"], ["displayp3", "display-p3"], ["rec2020", "rec2020"], ["rec-2020", "rec2020"], ]),);
/** * Reads the first of `names` that is set in the options * @param {Record<string, unknown>} options * @param {string[]} names * @returns {string | undefined} */function readOption(options, names) { for (const name of names) { if (!(name in options)) continue;
const value = options[name]; // Tailwind parses unquoted values in `@plugin`, so a spec version like // 2025 arrives as a number if (value !== undefined && value !== null) return String(value); }
return undefined;}
/** * Reads the contrast levels to generate. Takes a list, because they are not * exclusive, in whatever separator reads naturally: `high`, `high medium`, * `high, medium` and `all` all work. * @param {Record<string, unknown>} options * @returns {ContrastName[]} */function readContrasts(options) { const requested = readOption(options, ["contrasts", "contrast"]); if (requested === undefined) return [requiredContrast];
const names = requested .split(/[\s,]+/) .filter(Boolean) .map((name) => name.toLowerCase());
if (names.includes("all")) return /** @type {ContrastName[]} */ (Object.keys(contrastLevels));
for (const name of names) if (!(name in contrastLevels)) throw new UnknownContrastError(name);
// The default level is what the unqualified roles come from, so it is always // generated. A Set keeps the rest from being generated twice if listed twice. return [ ...new Set( /** @type {ContrastName[]} */ ([requiredContrast, ...names]), ), ];}
/** * Reads the plugin options, in either their CSS or their camelCase spelling * @param {Record<string, unknown> | undefined} options * @returns {{sourceColor: string, variant: VariantName, specVersion: SpecVersion, gamut: Gamut, colorsMode: ColorsMode, contrasts: ContrastName[]}} */function resolveOptions(options) { if (options === undefined) throw new PluginOptionsUndefinedError();
const sourceColor = readOption(options, [ "source-color", "sourceColor", "source", ]); if (sourceColor === undefined) throw new SourceColorUndefinedError();
/** @type {VariantName} */ let variant = defaultVariant; const requestedVariant = readOption(options, ["variant", "style"]); if (requestedVariant !== undefined) { // Accept "tonalSpot" as well as "tonal-spot" const name = camelToKebabCase(requestedVariant.trim()).toLowerCase(); if (!(name in variants)) throw new UnknownVariantError(requestedVariant);
variant = /** @type {VariantName} */ (name); }
const specVersion = readOption(options, ["spec-version", "specVersion", "spec"]) ?? defaultSpecVersion; if (!specVersions.includes(specVersion)) throw new UnknownSpecVersionError(specVersion);
/** @type {Gamut} */ let gamut = defaultGamut; const requestedGamut = readOption(options, [ "gamut", "color-gamut", "colorGamut", ]); if (requestedGamut !== undefined) { const alias = gamutAliases.get(requestedGamut.trim().toLowerCase()); if (alias === undefined) throw new UnknownGamutError(requestedGamut);
gamut = alias; }
/** @type {ColorsMode} */ let colorsMode = defaultColorsMode; const requestedColorsMode = readOption(options, ["colors", "colorsMode"]); if (requestedColorsMode !== undefined) { const name = requestedColorsMode.trim().toLowerCase(); if (!colorsModes.includes(name)) throw new UnknownColorsModeError(requestedColorsMode);
colorsMode = /** @type {ColorsMode} */ (name); }
return { sourceColor, variant, specVersion: /** @type {SpecVersion} */ (specVersion), gamut, colorsMode, contrasts: readContrasts(options), };}
/** * `plugin.withOptions` calls one function for the theme and another for the * base styles, and both need the same generated colors. Solving a whole theme * is not cheap, so it is done once per distinct set of options. * @type {Map<string, ReturnType<typeof createTheme>>} */const themes = new Map();
/** * @param {Record<string, unknown> | undefined} options * @returns {ReturnType<typeof createTheme>} */function build(options) { const resolved = resolveOptions(options); const key = JSON.stringify(resolved);
let theme = themes.get(key); if (theme === undefined) { theme = createTheme( resolved.sourceColor, resolved.variant, resolved.specVersion, resolved.gamut, resolved.colorsMode, resolved.contrasts, ); themes.set(key, theme); }
return theme;}
// This is based on code I saw in Tailwinds own plugin repositories like @tailwindcss/typography// Types are a bit cursed right now/** * @import {PluginCreator, PluginsConfig} from "tailwindcss/plugin" *//** @type {PluginsConfig} */const materialTailwindPlugin = plugin.withOptions( (options) => (api) => { // `addBase` is the only place a plugin can put a declaration, so the // variables the theme references are defined from here api.addBase(build(options).base); }, (options) => ({ theme: build(options).theme }),);
export default materialTailwindPlugin;