How to make @utility
+ @apply
compatible with prefix()
in Tailwind v4?
#18896
-
I'm trying to build a CSS design system package with Tailwind v4. The idea is to expose utilities, similar to what DaisyUI does. For example, I want to define a @utility btn {
@layer utilities.components.base {
@apply bg-amber-500;
}
} The package would be published as @import "tailwindcss";
@import "@acme/css"; This works fine without a prefix. ProblemWhen a consumer wants to use Tailwind’s @import "tailwindcss" prefix(tw);
@import "@acme/css"; Expected usage: <button class="tw:btn">button</button> But instead I get this error: Cannot apply unprefixed utility class QuestionWhat is the recommended way to define utilities in a package (using
Reproduction playgroundHere is a reproduction playground |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Answer for "What is the recommended way [...]"I know this isn't the answer you were looking for, but I'd like to point out that the use of
If I do it this way, then there’s no error: @import "tailwindcss" prefix(tw);
@utility btn {
background-color: var(--tw-color-amber-500);
} <button class="tw:btn">example</button> In this case, you need to call all utilities with the prefix, and the variables also get the prefix. How to handle your "cannot apply unprefixed utility class" message
If you really insist on using @import "tailwindcss" prefix(tw);
@utility btn {
@apply tw:bg-amber-500;
} <button class="tw:btn">example</button> |
Beta Was this translation helpful? Give feedback.
TailwindCSS requires you to manually add the prefix. If you don't want to (or can't) do this, you could possibly inject a plugin that runs before the TailwindCSS engine, interprets the
@apply
lines, and inserts the appropriate prefix for you.I assume DaisyUI has written such a plugin for itself in the background using the property.
So if you want to do the same thi…