Reputation: 1819
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?
Upvotes: 4
Views: 2210
Reputation: 1016
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
Upvotes: 1
Reputation: 61
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/bvm
bvm install
bit login
bit init --harmony
workspace.jsonc
in the root folder of your project: {
"defaultScope": "your-profile.your-collection"
}
Everything else in that file can (should?) stay the same.
tailwind-config/index.js
bit add tailwind-config
npm install
)bit tag --all
a.k.a. commit (as we know from git)bit export
a.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-config
And lastly, add this to your tailwind.config.js
file:
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 using bit import
and then npm install
Upvotes: 5