A modern, production-ready boilerplate for building component libraries using React, Material UI, and Vite. This monorepo demonstrates best practices for creating, testing, and publishing reusable UI components.
- Monorepo Architecture: Uses pnpm workspaces to organize and manage multiple packages
- Modern Build System: Configured Vite for component library development and builds
- Optimized Bundle: Generates ESM modules with preserved structure for better tree-shaking
- Type Safety: Includes TypeScript configuration with type declarations generation
- Component Development: Integrates Storybook for isolated component testing and documentation
- Theming System: Provides Material UI theme setup with proper TypeScript support
- Quality Assurance: Configured ESLint, Prettier, and TypeScript validation
- Local Testing: Setup for Verdaccio to test packages before publishing
- Containerization: Docker configurations for various development and CI environments
- CI/CD Ready: Includes GitHub Actions workflow templates
- Developer Experience: Contains VS Code Dev Container for consistent development environment
This monorepo contains the following packages:
βββ packages/
β βββ components/ # React UI component library with Material UI
β βββ icons/ # SVG icon components package
βββ storybook/ # Storybook documentation and development environment
βββ scripts/ # Build and publish scripts
βββ .verdaccio/ # Local package registry configuration
The @component-lib/components
package includes:
- React UI components based on Material UI
- Customizable theming system
- Type-safe component props
The @component-lib/icons
package includes:
- Custom SVG icons as React components
- Customizable icon styling
- Icon variants implementation
The Storybook package provides:
- Interactive component documentation
- Development environment for building and testing components
- Theme switching capability
- Component prop control and documentation
- Node.js v22.9.0 or later
- pnpm v10.2.1 or later
-
Clone the repository:
git clone git@github.com:manuelcoca/sample-component-lib-react-vite.git cd sample-component-lib-react-vite
-
Install dependencies:
pnpm install
-
Build all packages:
pnpm all:build
-
Start Storybook:
pnpm storybook
After building and publishing, you can use the components package in your application:
import { Button } from '@component-lib/components';
import { mainTheme } from '@component-lib/components/theme';
import { ThemeProvider } from '@mui/material/styles';
function App() {
return (
<ThemeProvider theme={mainTheme}>
<Button variant="contained">Click Me</Button>
</ThemeProvider>
);
}
-
Create a new component directory:
packages/components/src/components/MyComponent/
-
Add component files:
MyComponent.tsx # Component implementation index.ts # Component export
-
Export the component in the main index file:
// packages/components/src/components/index.ts export * from './MyComponent';
-
Create a Storybook story:
storybook/src/stories/MyComponent.stories.tsx
The component library includes a customizable theme system:
import { createTheme } from '@mui/material/styles';
import { palette, typography, spacing } from '@component-lib/components/theme';
const customTheme = createTheme({
palette: {
...palette,
primary: {
main: '#0088cc',
},
},
typography,
spacing,
});
pnpm storybook
- Start Storybook development serverpnpm storybook:build
- Build Storybook static sitepnpm all:build
- Build all packagespnpm all:lint
- Lint all packagespnpm all:typecheck
- Type check all packagespnpm format
- Format all files with Prettierpnpm format:check
- Check if files are properly formatted
pnpm components:build
- Build the components packagepnpm components:build:analyze
- Build with bundle analysispnpm components:lint
- Lint the components packagepnpm components:typecheck
- Type check the components packagepnpm components:publish
- Publish to registry
pnpm icons:build
- Build the icons packagepnpm icons:build:analyze
- Build with bundle analysispnpm icons:lint
- Lint the icons packagepnpm icons:typecheck
- Type check the icons packagepnpm icons:publish
- Publish to registry
This repository uses Verdaccio as a local npm registry for testing packages before publishing to a public registry.
-
Start Verdaccio:
pnpm verdaccio
-
Build and publish packages to local registry:
pnpm components:build pnpm components:publish
-
In your test project, update
.npmrc
to use the local registry:registry=http://localhost:4873/
-
Install the package in your test project:
npm install @component-lib/components
This repository includes Docker configurations for different purposes:
Dockerfile.Test
- For testing the component library in a CI environmentDockerfile.Publish
- For building and publishing packages
docker build -f Dockerfile.Test -t component-lib-test .
docker run component-lib-test
docker build -f Dockerfile.Publish -t component-lib-publish .
docker run component-lib-publish
For a consistent development environment, this repository includes a VS Code Dev Container configuration with all necessary tools and extensions.
- Open the repository in VS Code
- Click "Reopen in Container" when prompted
- Wait for the container to build and initialize
To analyze the bundle size and composition:
pnpm components:build:analyze
This will generate an interactive visualization in packages/components/dist/stats/stats.html
.
The repository includes a GitHub Actions workflow template for automated testing and publishing.
This project is licensed under the MIT License, which means:
- It is completely free to use, modify, and distribute, including for commercial purposes
- You can use the code in proprietary software
- The only requirement is including the original license and copyright notice
- No warranty is provided - the code is offered "as is"
See the LICENSE file for the complete license text.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request