This is a starter template for creating a React component library. It comes pre-configured with Rollup for bundling, Babel for transpiling, and Storybook for interactive component development and documentation. Ideal for developers who want to build reusable UI components for React applications, this template simplifies the process of creating, building, and publishing React component libraries. π
- π¦ Bundling with Rollup: Package your components efficiently for both CommonJS and ES module formats.
- π Transpiling with Babel: Use modern ES6+ and JSX syntax, ensuring compatibility across environments.
- π Storybook Integration: Document and test your components in isolation with live previews.
- π¨ CSS Modules: Scoped styling with PostCSS support.
- π PropTypes and TypeScript Support: Type-check your props and ensure proper usage.
To get started, simply run the following command:
npx create-react-component-library
Here's a list of the most important scripts included in the template:
-
Start Storybook: Run Storybook to develop and preview your components interactively.
npm start
This will open Storybook in your browser at http://localhost:6006.
-
Build the Library: Bundle your components into distributable formats.
npm run build-lib
This will output your library to the
dist/
folder, creating bothCommonJS
andESM
versions.
After running the starter, the basic folder structure will look like this:
βββ dist/ # Compiled and bundled files
βββ src/ # Source files (React components)
β βββ index.jsx # Entry point for your library
βββ .storybook/ # Storybook configuration
βββ rollup.config.js # Rollup bundling configuration
βββ package.json # Project configuration and dependencies
βββ README.md # Project documentation
To add a component to your library, create it under the src/
folder. Here's an example Button.jsx
component:
import React from 'react';
import PropTypes from 'prop-types';
import './Button.css';
export const Button = ({ label, onClick, primary }) => (
<button
className={primary ? 'btn-primary' : 'btn-secondary'}
onClick={onClick}
>
{label}
</button>
);
Button.propTypes = {
label: PropTypes.string.isRequired,
onClick: PropTypes.func,
primary: PropTypes.bool,
};
To bundle the library and make it distributable, run:
npm run build-lib
The bundled files will appear in the dist/
folder. These files are ready for use in other projects or publishing to npm.
To publish your library on npm:
-
Ensure your package name in
package.json
is unique. -
Run the following command to log in to npm:
npm login
-
Publish the package:
npm publish
Developers can now install your library in their projects using:
npm install your-library-name
Storybook is set up in this template for interactive component development. To run Storybook locally, use:
npm start
This will open the Storybook dashboard where you can view and interact with your components. You can add stories for new components under the src/stories/
folder.
- react: Ensures the consuming project provides React.
- react-dom: Provides DOM-specific methods for React.
- @babel/plugin-transform-runtime: Optimizes Babel's runtime helpers.
- @rollup/plugin-node-resolve: Helps Rollup find
node_modules
. - rollup-plugin-postcss: Adds support for processing CSS files.
- Storybook: Provides a UI for developing and testing components in isolation.
{
"name": "react-component-library",
"version": "1.0.0",
"description": "Template for creating React component libraries",
"main": "dist/index.jsx",
"module": "dist/index.es.js",
"scripts": {
"start": "storybook dev -p 6006",
"build-lib": "rollup -c"
},
"author": "@sasibhumaraju",
"license": "MIT",
"devDependencies": {
"@babel/plugin-transform-runtime": "^7.25.4",
"@babel/preset-react": "^7.24.7",
"@babel/runtime": "^7.25.6",
"rollup": "^4.22.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-postcss": "^4.0.2",
"storybook-css-modules-preset": "^1.1.1",
"@storybook/react-vite": "^8.3.2",
"@storybook/addon-essentials": "^8.3.2"
},
"peerDependencies": {
"prop-types": "^15.8.1",
"react": "^18.3.1",
"react-dom": "^18.3.1"
}
}
This project is licensed under the MIT License.