React Styleguidist
React Styleguidist is a robust tool for generating living style guides and developing React components in isolation. It provides a hot-reloaded development server where developers can view, interact with, and document components. The current stable version is 13.1.4, with recent maintenance releases addressing bug fixes and minor improvements, typically seeing several releases per year including minor versions and patches. Key differentiators include its reliance on Markdown files for component documentation, automatic listing of `propTypes`, and the ability to show live, editable usage examples. It also offers extensive configuration options for Webpack and theming, allowing deep integration into existing project setups. Unlike some alternatives, it focuses on generating a static style guide from existing components and their documentation, rather than being a visual drag-and-drop builder, promoting a documentation-driven development workflow.
Common errors
-
Error: Webpack 4 is no longer supported.
cause Your project is using `react-styleguidist` v12 or higher with Webpack 4.fixUpgrade Webpack to version 5: `npm install webpack@^5 webpack-cli@^4 --save-dev`. -
Error: You must install peer dependencies: react@>=18.0 react-dom@>=18.0
cause Your project is running `react-styleguidist` v13.1.1 or higher with an older version of React or ReactDOM.fixUpgrade `react` and `react-dom` to version 18 or higher: `npm install react@^18.0.0 react-dom@^18.0.0`. -
Error: Cannot find module 'babel-loader' (or similar loader name)
cause Your `styleguide.config.js` requires a specific Webpack loader (e.g., `babel-loader`), but it has not been installed as a dependency in your project.fixInstall the missing loader as a development dependency: `npm install --save-dev babel-loader` (replace `babel-loader` with the actual missing loader). -
TypeError: Cannot read properties of undefined (reading 'build')
cause Attempting to call `.build()` or `.server()` directly on `require('react-styleguidist')` without passing a configuration object to the factory function.fixPass your configuration object to the `react-styleguidist` factory function to instantiate it correctly: `const styleguidist = require('react-styleguidist')(config); styleguidist.build(...);`
Warnings
- breaking React Styleguidist v13.0.0 dropped support for React 16. Projects using v13 or later must use React 17 or newer.
- breaking React Styleguidist v12.0.0 deprecated support for Webpack 4. If you are using v12 or higher, your project must use Webpack 5.
- gotcha The `defaultProps` property on functional components is deprecated in modern React (v18+). While React Styleguidist v13.1.3 fixed an internal bug related to `defaultProps`, direct usage on functional components is discouraged.
- gotcha Incorrect Webpack configuration is a common source of issues, leading to compilation failures or components not rendering correctly within the style guide. React Styleguidist internally uses Webpack and allows extensive customization.
Install
-
npm install react-styleguidist -
yarn add react-styleguidist -
pnpm add react-styleguidist
Imports
- styleguidist
import styleguidist from 'react-styleguidist';
const styleguidist = require('react-styleguidist'); - styleguide.config.js (config export)
export default { /* config */ };module.exports = { /* config */ }; - styleguidist(config).build
import { build } from 'react-styleguidist';const styleguidist = require('react-styleguidist')(config); styleguidist.build((err, conf) => { /* ... */ });
Quickstart
{
"name": "my-component-library",
"version": "1.0.0",
"scripts": {
"styleguide": "styleguidist start",
"styleguide:build": "styleguidist build"
},
"dependencies": {
"react": ">=18.0",
"react-dom": ">=18.0"
},
"devDependencies": {
"react-styleguidist": "^13.1.4",
"babel-loader": "^9.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0"
}
}
// styleguide.config.js
module.exports = {
components: 'src/components/**/*.jsx',
webpackConfig: {
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
]
}
},
title: 'My Component Library Style Guide'
};
// src/components/Button/Button.jsx
import React from 'react';
import PropTypes from 'prop-types';
function Button({ children, onClick = () => {} }) {
return (
<button
style={{
padding: '10px 20px',
border: 'none',
borderRadius: '5px',
backgroundColor: '#2D9EE0',
color: 'white',
cursor: 'pointer',
}}
onClick={onClick}
>
{children}
</button>
);
}
Button.propTypes = {
/**
* Button content
*/
children: PropTypes.node.isRequired,
/**
* Click handler
*/
onClick: PropTypes.func,
};
export default Button;
// src/components/Button/Button.md
```## Button
A versatile button component.
### Usage
```jsx
<Button onClick={() => alert('Clicked!')}>Click Me</Button>
```
```jsx
<Button>Another Button</Button>
```
```