m3-tailwind-colors

中文文档
0.4.0发布于 2026/11/04MITTypeScriptESM + CJS

Generate Material 3 colors for Tailwind CSS (v3 and v4) and NativeWind.

原始使用文档
文档目录30 节
  1. Tailwind CSS v4 Support Now Available
  2. Demo
  3. Why M3 Tailwind Colors?
  4. Features
  5. Installation
  6. Quick Start
  7. For Tailwind v4
  8. For Tailwind v3
  9. Tailwind v4 Usage
  10. CLI Commands
  11. Programmatic API
  12. Configuration File
  13. NativeWind Usage (React Native)
  14. Quick Start
  15. Generated CSS Format
  16. Generated TypeScript Colors
  17. Custom Output Paths
  18. Configuration File
  19. Migrating from NativeWindUI Starter
  20. Tailwind v3 Usage
  21. Usage with React Native Styling
  22. Migrating to v4
  23. Prerequisites
  24. Step-by-step Migration
  25. Generated Colors
  26. Fixed colors
  27. Custom colors
  28. Parameters
  29. Contributing
  30. License

M3 Tailwind Colors

M3 Tailwind Colors is a utility package that generates Material 3 color based on the Material 3 color system for use with Tailwind CSS and NativeWind.

Tailwind CSS v4 Support Now Available

Demo

View Live Demo

Why M3 Tailwind Colors?

Material 3 offers a robust color system, but manually generating and managing these tonal palettes for Tailwind CSS can be overwhelming.

This package solves that by:

  • Automating Palette Creation: Generates all required tonal palettes (primary, secondary, error, surface, etc.) from a single color.
  • Ensuring Accessibility: Uses the official Material Color Utilities for accessible contrast ratios.
  • Type Safety: Provides TypeScript support for React Native (NativeWind) and Tailwind config.
  • Consistency: Guarantees your Light and Dark modes are perfectly synchronized.

Simply pick a primary color, and let the tool handle the complex color science.

Features

  • Generate a full palette of Material 3 colors based on a primary color.
  • NEW: Tailwind CSS v4 support with native @theme CSS variables
  • NEW: CLI tool for easy theme generation
  • Easily integrate with Tailwind CSS or NativeWind projects.
  • Supports both light and dark color schemes.
  • Hex and OKLCH color format support.

Read more about how it works here

How it works: When only a primary color is provided, it is used as a seed color to generate the entire Material 3 palette including secondary, tertiary, surface, and all other color tokens according to Material Design 3 guidelines.

Installation

Install the package via npm:

npm install m3-tailwind-colors

This will automatically install the necessary dependencies, including @material/material-color-utilities.

Note: Tailwind CSS must already be set up in your project before using this package.

Quick Start

For Tailwind v4

# Initialize configuration
npx m3-tailwind-colors init --primary "#0062A8"

# Generate CSS theme
npx m3-tailwind-colors generate

# Import in your main CSS file
# @import "./src/m3-theme.css";

For Tailwind v3

// tailwind.config.js
const { M3TailwindConfigColors } = require("m3-tailwind-colors");

module.exports = {
  theme: {
    extend: {
      colors: M3TailwindConfigColors({ primary: "#0062A8" })
    }
  }
};

Tailwind v4 Usage

CLI Commands

Initialize Configuration

Creates a m3-colors.config.json file with default values:

npx m3-tailwind-colors init --primary "#0062A8"

Options:

  • --primary <color> - Primary color (hex format)
  • --secondary <color> - Secondary color (optional)
  • --tertiary <color> - Tertiary color (optional)
  • --scheme <name> - Color scheme (content, expressive, etc.)
  • --contrast <number> - Contrast level (-1 to 1)
  • --format <format> - Color format (hex or oklch)
  • --mode <mode> - Output mode (combined, light, or dark)
  • --output <path> - Output CSS file path
  • --config <path> - Config file path

Generate Theme

Generates the CSS theme file:

npx m3-tailwind-colors generate

Options:

  • --include-tailwind-import - Include @import "tailwindcss" in output
  • --dark-mode <strategy> - Dark mode strategy: media (default), class, or none
  • --target <platform> - Target platform: web (default) or nativewind
  • --colors-output <path> - Output path for the TypeScript colors file (NativeWind only)

This reads your m3-colors.config.json and outputs CSS like this (default media strategy):

@theme {
  --color-primary: #3b82f6;
  --color-on-primary: #ffffff;
  --color-surface: #fafafa;
  /* ... all Material 3 color tokens */
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-primary: #60a5fa;
    /* ... dark mode colors */
  }
}

With --dark-mode class, dark colors apply when a .dark class is on an ancestor element:

@custom-variant dark (&:where(.dark, .dark *));

@theme {
  --color-primary: #3b82f6;
  /* ... */
}

@variant dark {
  --color-primary: #60a5fa;
  /* ... */
}

With --dark-mode none, only the light @theme block is output.

Programmatic API

import { generateM3ThemeCSS } from 'm3-tailwind-colors/v4';
import fs from 'fs';

const css = generateM3ThemeCSS({
  colors: {
    primary: "#0062A8",
    secondary: "#CBF7ED",
    tertiary: "#F98948"
  },
  format: "hex",      // or "oklch"
  mode: "combined",   // or "light" or "dark"
  scheme: "content",  // Material scheme type
  contrast: 0,        // -1 to 1
  includeTailwindImport: false, // Include @import "tailwindcss"
  darkModeStrategy: "media"     // or "class" or "none"
});

fs.writeFileSync('./src/theme.css', css);

Configuration File

Example m3-colors.config.json:

{
  "colors": {
    "primary": "#0062A8",
    "secondary": "#CBF7ED",
    "tertiary": "#F98948",
    "success": "#16A34A",
    "error": "#EF4444",
    "warning": "#D14700",
    "info": "#0055D1"
  },
  "scheme": "content",
  "contrast": 0,
  "format": "hex",
  "mode": "combined",
  "output": "src/m3-theme.css"
}

Note: You can add any custom color key (like success, warning, info) and it will generate a full tonal palette for that color.


NativeWind Usage (React Native)

Generate Material 3 colors for NativeWind projects with the --target nativewind flag.

Quick Start

npx m3-tailwind-colors generate --target nativewind --primary "#0062A8"

This generates two files:

  • global.css - CSS variables in NativeWind format
  • theme/m3-colors.ts - TypeScript colors for React Native

Generated CSS Format

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  :root {
    --primary: 0 74 129;
    --on-primary: 255 255 255;
    --android-primary: 0 74 129;
  }
  @media (prefers-color-scheme: dark) {
    :root {
      --primary: 160 201 255;
    }
  }
}

Generated TypeScript Colors

export const COLORS = {
  light: {
    primary: 'rgb(0, 74, 129)',
    onPrimary: 'rgb(255, 255, 255)',
  },
  dark: {
    primary: 'rgb(160, 201, 255)',
  },
} as const;

Custom Output Paths

npx m3-tailwind-colors generate \
  --target nativewind \
  --primary "#0062A8" \
  --output "src/global.css" \
  --colors-output "src/theme/colors.ts"

Configuration File

Add target to your m3-colors.config.json:

{
  "colors": { "primary": "#0062A8" },
  "target": "nativewind",
  "colorsOutput": "theme/m3-colors.ts"
}

Migrating from NativeWindUI Starter

If you're using the NativeWindUI starter template, the generated M3 colors use different names. Update your theme/index.ts:

// theme/index.ts
import { Theme, DefaultTheme, DarkTheme } from '@react-navigation/native';
import { COLORS } from './colors';

const NAV_THEME: { light: Theme; dark: Theme } = {
  light: {
    dark: false,
    colors: {
      background: COLORS.light.background,
      border: COLORS.light.outlineVariant,     // was: grey5
      card: COLORS.light.surface,               // was: card
      notification: COLORS.light.error,         // was: destructive
      primary: COLORS.light.primary,
      text: COLORS.light.onBackground,          // was: COLORS.black
    },
    fonts: DefaultTheme.fonts,
  },
  dark: {
    dark: true,
    colors: {
      background: COLORS.dark.background,
      border: COLORS.dark.outlineVariant,       // was: grey5
      card: COLORS.dark.surfaceContainer,       // was: grey6
      notification: COLORS.dark.error,          // was: destructive
      primary: COLORS.dark.primary,
      text: COLORS.dark.onBackground,           // was: COLORS.white
    },
    fonts: DarkTheme.fonts,
  },
};

export { NAV_THEME };

Color name mappings:

NativeWindUIM3 Equivalent
grey6surfaceContainerLowest
grey5outlineVariant
cardsurface or surfaceContainer
destructiveerror
COLORS.whiteHardcode 'rgb(255,255,255)' or use onPrimary
COLORS.blackHardcode 'rgb(0,0,0)' or use onBackground

Tailwind v3 Usage

To use the generated colors in your Tailwind CSS configuration, follow these steps:

  • Import and Generate Colors: Use the M3TailwindConfigColors function to generate your Material 3 color palette based on a primary color.

  • Extend Tailwind's Colors: Add the generated colors to the extend.colors section of your Tailwind configuration.

Example Tailwind Configuration:

/** @type {import('tailwindcss').Config} */

const { M3TailwindConfigColors } = require("m3-tailwind-colors");

const generatedColors = M3TailwindConfigColors({
  primary: "#CE7E00", // Replace with your primary color
  otherCustomColor: "#65ff34", //optional
});

module.exports = {
  content: ["./app/**/*.{js,jsx,ts,tsx}", "./components/**/*.{js,jsx,ts,tsx}"],
  presets: [require("nativewind/preset")],
  theme: {
    extend: {
      colors: {
        ...generatedColors,
      },
    },
  },
  plugins: [],
};

Usage with React Native Styling

The package exports a function that can be used to generate colors for React Native:

import { M3TailwindRNColors } from "m3-tailwind-colors";

const M3colors = M3TailwindRNColors(
  { primary: "#CE7E00" },
  {
    scheme: "content",
    contrast: 0,
  }
);

export const Colors = M3colors;

Then use in your React Native files:

<View
  style={{
    backgroundColor:
      colorScheme === "dark" ? Colors.dark.surface : Colors.light.surface,
  }}
>
  {children}
</View>

Migrating to v4

Prerequisites

npm install tailwindcss@next

Step-by-step Migration

1. Generate v4 theme:

npx m3-tailwind-colors init --primary "#0062A8"
npx m3-tailwind-colors generate

2. Update your CSS entry file:

/* Add to your main CSS file */
@import "./m3-theme.css";

3. Remove Tailwind config (optional):

# tailwind.config.js can be deleted if only used for colors
rm tailwind.config.js

4. No class name changes needed!

Your existing utility classes continue to work:

  • bg-primary - Still works
  • text-on-primary - Still works
  • All Material 3 color tokens remain the same

Generated Colors

The generated color palette includes all Material 3 color tokens:

  • Primary: primary, on-primary, primary-container, on-primary-container, inverse-primary
  • Secondary: secondary, on-secondary, secondary-container, on-secondary-container
  • Tertiary: tertiary, on-tertiary, tertiary-container, on-tertiary-container
  • Fixed: primary-fixed, primary-fixed-dim, on-primary-fixed, on-primary-fixed-variant, and the same four for secondary and tertiary
  • Error: error, on-error, error-container, on-error-container
  • Surface: surface, on-surface, surface-variant, on-surface-variant, surface-dim, surface-bright, surface-container-lowest, surface-container-low, surface-container, surface-container-high, surface-container-highest, inverse-surface, on-inverse-surface, surface-tint
  • Background: background, on-background
  • Outline: outline, outline-variant
  • Other: scrim, shadow

Fixed colors

Fixed colors keep the same value in light and dark mode. Use them for elements that should not change appearance when the theme switches, such as a brand banner or a chip on a photo:

<div class="bg-primary-fixed text-on-primary-fixed">Same in light and dark</div>

At the default contrast (0) the light and dark values are identical. At higher contrast levels, Material 3 may adjust them slightly per mode to keep text readable.

Custom colors

Each custom color (e.g. success) generates eight tokens:

  • success, on-success, success-container, on-success-container
  • success-fixed, success-fixed-dim, on-success-fixed, on-success-fixed-variant

In camelCase output the names are success, onSuccess, successContainer, onSuccessContainer, successFixed, successFixedDim, onSuccessFixed and onSuccessFixedVariant.


Parameters

The M3TailwindConfigColors and M3TailwindRNColors functions accept:

M3TailwindRNColors(
  {
    primary: '#ff0000', // Required: Primary color in hex format
    secondary: '#ffff00', // Optional: Auto-generated if not provided
    tertiary: '#0000ff', // Optional: Auto-generated if not provided
    customname: '#00ff00', // Optional: Any custom colors
  },
  {
    scheme: 'content', // Optional: 'content', 'expressive', 'fidelity', 'monochrome', 'neutral', 'tonalSpot', 'vibrant'
    contrast: 0, // Optional: -1 (less contrast) to 1 (more contrast)
  },
  true // Optional: Use kebab-case (default: false)
)

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

License

MIT License - see LICENSE for details.

版本与发布记录

m3-tailwind-colors 共发布 10 个版本,首次发布于 2024/08/27;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.4.0latest2026/11/041 天前
0.3.42026/01/239 个月前
0.3.32026/01/239 个月前
0.3.12026/01/239 个月前
0.1.52024/08/302 年前
0.1.42024/08/272 年前

关于 m3-tailwind-colors 的常见问题

m3-tailwind-colors 是什么?

Generate Material 3 colors for Tailwind CSS (v3 and v4) and NativeWind.

如何安装 m3-tailwind-colors?

在项目目录运行 npm install m3-tailwind-colors,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 m3-tailwind-colors,可以用 npx m3-tailwind-colors 直接运行。

m3-tailwind-colors 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

m3-tailwind-colors 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

m3-tailwind-colors 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

m3-tailwind-colors 可以商用吗?

m3-tailwind-colors 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

m3-tailwind-colors 还在维护吗?

最新版本 0.4.0 发布于 2026/11/04(1 天前);至今共发布 10 个版本,其中过去 12 个月发布了 4 个;首次发布于 2024/08/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。