Tailwindcss: Use data attributes instead of classes

Created on 7 Oct 2019  路  5Comments  路  Source: tailwindlabs/tailwindcss

the prefix option is quite cool, but is there an even stricter way to distinguish tailwind classes from the rest?

e.g.:

<div class="own-class1 own-class2" data-tw="bg-white rounded-lg"></div>

if not, is this doable via a tailwind plugin?

Most helpful comment

Thanks! Made a quick prototype that seems to work: https://github.com/vielhuber/postcss-tailwind-data-attr

PostCSS (and Tailwind CSS) is a joy!

All 5 comments

You can disable all corePlugins and write your own that style elements based on data-attributes 馃憤

Thanks for your answer!

If I disable all corePlugins, then I loose a lot of functionality I need (i don't want to reimplement everything).

Is it not possible to have a postcss plugin or another plugin in the build pipeline of tailwind that replaces all class names with data-attributes?

Probably although I'm not aware of any. Maybe you'll find something on https://www.postcss.parts/

Yeah I think you could probably write a PostCSS plugin to transform any Tailwind classes into data attribute selectors, maybe by assigning a unique prefix in your Tailwind config, then walking all of the rules in CSS looking for that prefix and converting to data attributes.

Not something I plan to bake into core though, sorry 馃槥

Thanks! Made a quick prototype that seems to work: https://github.com/vielhuber/postcss-tailwind-data-attr

PostCSS (and Tailwind CSS) is a joy!

Was this page helpful?
0 / 5 - 0 ratings