Introduce a Simplified Syntax for Grouping Pseudo Classes in Tailwind CSS 🌟 #12712
Replies: 4 comments 5 replies
-
There have been suggestions about adding such a feature. The maintainers have addressed this request before, that you may wish to read up on if you have not already done so: |
Beta Was this translation helpful? Give feedback.
-
Is there anything that has been done in this regard? |
Beta Was this translation helpful? Give feedback.
-
@wongjn do you think it could work if we wrote like this but had it "unwrapped" during the build? Postcss or maybe tailwind itself could do this. |
Beta Was this translation helpful? Give feedback.
-
I found a solution: https://stackoverflow.com/a/78286098/6166627 Here's how it worked for me for v4: Create a import plugin from 'tailwindcss/plugin'
module.exports = plugin.withOptions(() => ({ matchUtilities }) => {
matchUtilities({
join: value => ({
[`@apply ${value.replaceAll(',', ' ')}`]: {}
})
})
}) Import it into your global @plugin "./twPluginJoin.mjs"; Lastly, update intellisense to get proper syntax highlighting (in intellij that is in the tailwind plugin settings): "experimental": {
"classRegex": [["join-\\[((?:[\\w-]+,?)+)\\]", "([^,]+)"]]
} Usage: className="[&_button]:join-[min-w-0,p-0] flex gap-1" |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
While working extensively with Tailwind CSS, I've encountered a recurring issue that results in lengthy and repetitive class names, particularly when utilizing pseudo classes.
For instance:
"lg:flex lg:flex-row lg:px-4 hover:text-white hover:bg-black"
The repetitive use of prefixes like lg: and hover: not only clutters the HTML but also increases the potential for errors and makes the code less maintainable.
To address this, I propose introducing a more concise syntax for grouping pseudo classes and their respective styles. Consider the following alternative syntax:
"lg:flex|flex-row|px-4 hover:text-white|bg-black"
Here, I've employed the
|
character as a delimiter to encapsulate multiple pseudo classes under a single class declaration. This approach minimizes redundancy, enhances readability, and promotes cleaner code organization.Implementing such a feature would not only streamline the development process but also fortify Tailwind CSS's efficiency and versatility.
If a similar capability already exists within Tailwind CSS that I'm unaware of, I'd appreciate any guidance or pointers.
🙏Thank you for considering this enhancement request.
Beta Was this translation helpful? Give feedback.
All reactions