A UI component library is a set of ready-made UI components such as buttons, inputs, dialogs, and so on. They serve as building blocks for layouts. Thanks to their modular nature, we can arrange components in many different ways to achieve unique effects. Each library has a distinctive look and feel, but most of them offer theming, and their components are customizable and reusable.I will walk you through the process of creating your own UI Component Library using :
Create a new React project and install TailwindCSS
npx create-react-app storybook-postcss-tailwindInitialize TailwindCSS.
npm install -D tailwindcss@latest postcss@latest autoprefixer@latest@tailwind base;
@tailwind components;
@tailwind utilities;Create tailwind.config.js with npx.--full option scaffold file that matches the default configuration file that Tailwind uses internally.
npx tailwindcss init --full"./src/**/*.{js,ts,jsx,tsx}"}Install postcss-cli and create postcss.config.js file.
npm install -D postcss-cli module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
Install Storybook and create a new component
Initialize Storybook with the following command:
npx sb initCreate sample component src/lib/components/Button/Button.js, with some styling:
import React from ‘react’
import PropTypes from 'prop-types'
const Button = ({ label }) => {
return (
<div>
<button
className='bg-red-500 text-white text-xl py-4 px-8 rounded-md'>{label}</button>
</div>
)
};
Button.propTypes = {
label: PropTypes.string.isRequired
};
Button.defaultProps = {
label: 'Button'
};
export default ButtonCreate src/lib/components/Button/Button.stories.js with the following content:
import React from 'react';
import Button from './Button'
export default {
title: 'Example/Button',
component: Button,
};
const Template = (args) => <Button {...args} />
export const Default = Template.bind({})
Default.args = {
label: 'Button'
}; import './styles/main.css';
import Button from './lib/components/Button/Button'
export {
Button
};
Configure
npm i -D cross-env @babel/cli @babel/preset-env @babel/preset-reactCreate babel.config.js file:
module.exports = function (api) {
api.cache(true);
const presets = [ "@babel/preset-env", "@babel/preset-react" ];
const plugins = [ "macros" ];
return {
presets,
plugins
};
}To avoid any React conflict, you can move the following React dependencies to a peer dependency:
"peerDependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3"
} "scripts": {
"build:tailwind": "postcss src/lib/styles/tailwind.css -o src/lib/styles/main.css",
"build:tailwind-prod": "cross-env NODE_ENV=production postcss src/lib/styles/tailwind.css -o src/lib/styles/main.css"
}, "private": false,
"main": "dist/index.js",
"module": "dist/index.js",
"files": [
"dist",
"README.md"
], "scripts": {
"clean": "rimraf dist",
"compile": "npm run clean && cross-env NODE_ENV=production babel src/lib/ --out-dir dist --copy-files"
},
Building for production and Publishing to
Build tailwindcss for production:
npm run build:tailwind-prodnpm run compilenpm loginnpm publishThat's it! We managed to get a simple UI Library using Storybook and TailwindCSS. This is one option how can you create UI Components Library. From here you can: