I want to build a component library on top of tailwindcss. Therefore I want to encapsulate my components with bit.dev. Is this possible, or are these two competitive approaches?
How to use bit.dev with tailwindcss
2.1k Views Asked by L. Heider At
2
There are 2 best solutions below
0
On
So one thing you should consider is the difference between the tailwind.config.js's for your component(s) and the tailwind.config.js that you might have in your project. As @stephen-j mentioned, you can also create a global tailwind.config.js that is shared across your projects.
But, if you might have projects that need different configs. you can ensure that the component is more reusable, by adding an important class to the component's config.
e.g. if you are making a button, ensure it has a class .button, then in the tailwind config, ensure you set the important value to .button
// button.jsx
const Button = ({children}) => {
return (
<div className="button">
<button className="bg-primary">{children}</button>
</div>
)
}
// tailwind.config.js
module.exports = {
important:".button",
purge: [],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {
colors:{
primary:"#9333ea"
}
},
},
variants: {
extend: {},
},
plugins: [],
}
Using this method will export classes as .button .bg-primary
It is possible. I managed to do it as follows. (Using latest v15 - Bit Harmony)
Before you get started with your project, set up a collection through your Bit profile: https://bit.dev/~create-collection
npm i -g @teambit/bvmbvm installbit loginbit init --harmonyworkspace.jsoncin the root folder of your project:Everything else in that file can (should?) stay the same.
tailwind-config/index.jsbit add tailwind-confignpm install)bit tag --alla.k.a. commit (as we know from git)bit exporta.k.a. pushThe component should now appear in your collection and you can also reuse the Tailwind's config file across various projects using any package manager e.g.
npm install @your-profile/your-collection.tailwind-configAnd lastly, add this to your
tailwind.config.jsfile:module.exports = require('./node_modules/@your-profile/your-collection.tailwind-config')I would personally say, this is a great way to have a consistent and functional design system.
For more information about Bit's inner workings, have a look at the documentation: https://harmony-docs.bit.dev/
UPDATE: The best way to initialise a freshly pulled repository (that includes
.bitmap&workspace.jsonc) is usingbit importand thennpm install