default apply class styles #19937
Replies: 3 comments 2 replies
-
|
A pragmatic solution could be not to write any CSS. Rather, use React components to hide the implementation, so that it is like you are writing function Table ({ className, ...props }) {
return (
<table className={`table__content ${className}`} {...props }/>
);
}
function App() {
return (
<Table>
…
</Table>
);
}However, if you really did want a |
Beta Was this translation helpful? Give feedback.
-
|
Why not let this work? table {
@apply table-content;
} |
Beta Was this translation helpful? Give feedback.
-
|
from gemini: To make @apply work with custom classes (like table-content) inside the @layer base block in Tailwind CSS v4/v5, you have to modify how the Design System handles candidate resolution. In the new engine (written in Rust/TypeScript), Tailwind distinguishes between Utilities (which are known to the engine) and Plain CSS (which are just strings). By default, @apply only looks at Utilities. If you were to modify the Tailwind source code (specifically the @tailwindcss/node or core package), here are the two logical paths to achieve this:
The Logic Change: "Is table-content in my list of generated utilities? No? Throw error." You would change it to: "Is table-content in my list of utilities OR does it exist as a selector in the current CSS entry file? Yes? Inline those declarations."
// Conceptual change to Tailwind Core's apply visitor
function handleApply(node, designSystem) {
const className = node.params;
// 1. Check if it's a built-in Tailwind utility (standard behavior)
let rule = designSystem.resolve(className);
// 2. THE CHANGE: Check if it's a user-defined class in the CSS
if (!rule) {
rule = designSystem.getUserDefinedClass(className);
}
if (rule) {
node.replaceWith(rule.nodes);
} else {
// Current behavior: fail
throw node.error(`Cannot apply unknown utility: ${className}`);
}
}Is it a good solution? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
HeroUI v3 defines a lot of styles, such as:
Then I can use native tags with these styles:
<table className='table-content'>What if I need apply these styles defaultly?
// global.css table { // just use table-content here }Then
<table>would work the same as<table className='table-content'>.How to write the css (using tailwindcss)?
Beta Was this translation helpful? Give feedback.
All reactions