Skip to content

Instantly share code, notes, and snippets.

@nakasyou
Last active August 12, 2023 03:38
Show Gist options
  • Save nakasyou/f59754b835927de4726ed46e7e6cbac9 to your computer and use it in GitHub Desktop.
Save nakasyou/f59754b835927de4726ed46e7e6cbac9 to your computer and use it in GitHub Desktop.
Material Color on TailwindCSS
const { argbFromHex, themeFromSourceColor, applyTheme, hexFromArgb } = require("@material/material-color-utilities")
const tailwindPlugin = require("tailwindcss/plugin")
const materialColorPlugin = (baseColor) => {
const theme = themeFromSourceColor(argbFromHex(baseColor));
const props = Object.keys(theme.schemes.light.props);
const classes = props.map(prop => prop.replace(/[A-Z]/g, l => '-' + l.toLowerCase()));
const classToCss = Object.entries({
bg: 'background-color',
text: 'color',
border: 'border-color',
decoration: 'text-decoration-color',
shadow: '--tw-shadow-color',
});
const lightComponents = Object.assign(...classToCss.map(([className, cssKey]) => Object.fromEntries(classes.map((query, index) => {
const cssObject = {};
cssObject[cssKey] = hexFromArgb(theme.schemes.light[props[index]]);
return ['.' + className + '-' + query, cssObject];
}))));
const darkComponents = Object.assign(...classToCss.map(([className, cssKey]) => Object.fromEntries(classes.map((query, index) => {
const cssObject = {};
cssObject[cssKey] = hexFromArgb(theme.schemes.dark[props[index]]);
return ['.' + className + '-' + query, cssObject];
}))));
const components = {
'@media (prefers-color-scheme: dark)': darkComponents,
'@media (prefers-color-scheme: light)': lightComponents,
};
return tailwindPlugin(({ addComponents }) => {
addComponents(components);
});
};
@nakasyou
Copy link
Author

// tailwind.config.js
module.exports = {
  plugins: [
    materialColorPlugin('#ff0000'),
  ]
}

@nakasyou
Copy link
Author

All classes:

.bg-primary
.bg-on-primary
.bg-primary-container
.bg-on-primary-container
.bg-secondary
.bg-on-secondary
.bg-secondary-container
.bg-on-secondary-container
.bg-tertiary
.bg-on-tertiary
.bg-tertiary-container
.bg-on-tertiary-container
.bg-error
.bg-on-error
.bg-error-container
.bg-on-error-container
.bg-background
.bg-on-background
.bg-surface
.bg-on-surface
.bg-surface-variant
.bg-on-surface-variant
.bg-outline
.bg-outline-variant
.bg-shadow
.bg-scrim
.bg-inverse-surface
.bg-inverse-on-surface
.bg-inverse-primary
.text-primary
.text-on-primary
.text-primary-container
.text-on-primary-container
.text-secondary
.text-on-secondary
.text-secondary-container
.text-on-secondary-container
.text-tertiary
.text-on-tertiary
.text-tertiary-container
.text-on-tertiary-container
.text-error
.text-on-error
.text-error-container
.text-on-error-container
.text-background
.text-on-background
.text-surface
.text-on-surface
.text-surface-variant
.text-on-surface-variant
.text-outline
.text-outline-variant
.text-shadow
.text-scrim
.text-inverse-surface
.text-inverse-on-surface
.text-inverse-primary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment