-
I'm a TailwindCSS plugin author. I'm trying to use a custom function to generate the My plugin looks something like this: plugin.withOptions<Partial<Options>>(
// ...,
(options) => {
const { "base-hue": baseHue } = { ...defaultOptions, ...options };
// ...
return {
theme: {
color: myFunction(baseHue), // Outputs colors like `plugin: { 400: "oklch(...)" }`
},
}
}
); And use it via my CSS: @import "tailwindcss";
@plugin "my-plugin-name" {
base-hue: 300;
}
@theme {
--color-test-400: #444333; // For testing
} But the resulted CSS doesn't actually output the actual color. @theme {
/* ... */
--color-test-400: #444333;
}
/* ... */
@layer base {
/* ... */
.bg-plugin-400: {
background: oklch(/* ... */); /* Why it doesn't generate and use the color from the @theme? */
}
.bg-test-400 {
background: var(--color-test-400);
}
} Other stuff like using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
This is because the JavaScript plugin API is mainly for v3 backwards compatibility. In v3, CSS variables weren't use and the values were used directly as you have experienced. As a workaround, you can output the CSS variables yourself: plugin.withOptions<Partial<Options>>(
(options) => {
const { "base-hue": baseHue } = { ...defaultOptions, ...options };
const colors = myFunction(baseHue);
addBase({
// Use `colors` to convert to an object shape like:
// ```
// {
// '--color-plugin-400': 'oklch(…)',
// },
// ```
':root': …,
});
},
(options) => {
// ...
return {
theme: {
colors: {
plugin: {
400: 'var(--color-plugin-400)',
},
},
},
}
}
); |
Beta Was this translation helpful? Give feedback.
This is because the JavaScript plugin API is mainly for v3 backwards compatibility. In v3, CSS variables weren't use and the values were used directly as you have experienced.
As a workaround, you can output the CSS variables yourself: