How to use
refs in examples?
ref prop as a function and assign a reference to a local variable:
How to exclude some components from style guide?
Styleguidist will ignore tests (
__tests__ folder and filenames containing
.spec.js) by default.
Use ignore option to customize this behavior:
You should pass glob patterns, for example, use
**/components/Button.js instead of
How to hide some components in style guide but make them available in examples?
Enable skipComponentsWithoutExample option and do not add an example file (
Readme.md by default) to components you want to ignore.
Import these components in your examples:
Or, to make these components available for all examples:
Button component will be available in every example without a need to
How to render React components that aren’t part of the style guide?
Import these components in your examples:
How to dynamically load other components in an example?
Although examples don’t have direct access to webpack’s
require.context feature, you can use it in a separate helper file which you require in your example code. If you wanted to create an example to load and show all your icon components, you could do this:
How to display the source code of any file?
First, code examples can receive props and settings:
The above example adds a setting called
file with the relative path to the file we want to display as value.
Second, use the updateExample config option, to detect the setting and change the content of a fenced code block:
How to set global styles for user components?
Using the jss-global API you can set global styles in your config:
Above, we have set
font-family: 'Helvetica'; on the body.
This does not set styles on the style guide UI, for that read How to change styles of a style guide.
In your style guide config:
How to use React Styleguidist with Preact?
You need to alias
See the Preact example style guide.
How to change styles of a style guide?
Use theme to change fonts, colors, etc.
Use styles to tweak the style of any particular Styleguidist component.
As an example:
See available theme variables.
Use React Developer Tools to find component and style names. For example a component
<LogoRenderer><h1 className="rsg--logo-53"> corresponds to an example above.
Use a function instead of an object for styles to access all theme variables in your custom styles.
You can store all styles in a separate file to allow hot module replacement (HMR). Same goes for theme variables.
The same example above would then translate as:
styleguide.config,js, objects are replaced with file paths
Each modification of
styles.js will trigger a hot module replacement, updating the styleguide in the browser.
Check out the themed example on the github repo to learn more and try it out.
How to use CSS animations in your style guide?
As seen in the
@keyframes animation examples above, the animation property in CSS rules do not directly use the name of their keyframe animations because of internal keyframe scoping.
To use a CSS animation, you have to define its keyframe at the root of the renderer object. The use of
@keyframes in styling above are good examples of this.
How to change the layout of a style guide?
You can replace any Styleguidist React component. But in most of the cases you’ll want to replace
*Renderer components — all HTML is rendered by these components. For example
PropsRenderer, etc. — check the source to see what components are available.
There’s also a special wrapper component —
Wrapper — that wraps every example component. By default, it renders
children as is but you can use it to provide custom logic.
For example, you can replace the
Wrapper component to wrap any example in the React Intl’s provider component. You can’t wrap the whole style guide because every example is compiled separately in a browser.
You can replace the
StyleGuideRenderer component like this:
We have an example style guide with custom components.
How to change syntax highlighting colors?
How to change style guide dev server logs output?
You can modify webpack dev server logs format changing
stats option of webpack config:
How to debug my components and examples?
- Open your browser’s developer tools
debugger;statement wherever you want: in a component source, a Markdown example or even in an editor in a browser.
How to debug the exceptions thrown from my components?
debugger;statement at the beginning of your code.
- Press the button in your browser’s developer tools.
- Press the button and the debugger will stop execution at the next exception.
How to use the production or development build of React?
In some cases, you might need to use the development build of React instead of the default production one. For example, this might be needed if you use React Native and make references to a React Native component’s PropTypes in your code. As React removes all PropTypes in its production build, your code will fail. By default, Styleguidist uses the development build for the dev server and the production one for static builds.
If you use code like the example above, you might see a
Cannot read property 'isRequired' of undefined error. To avoid it, you need to tell Styleguidist to use React’s development build. To do this, set the
NODE_ENV variable to
development in your npm script.
Note: The script above uses cross-env to make sure the environment variable is properly set on all platforms. Run
npm i -D cross-env to add it.
How to use Vagrant with Styleguidist?
First, read Vagrant guide from the webpack documentation. Then enable polling in your webpack config:
How to add a favicon?
favicon.icofile into the root folder of your site.
Use template option:
Use template option:
In comparison to require option, these scripts and links are run in the browser, not during webpack build process. It can be useful for side effect-causing scripts in which your components, or in this case Babel output, need to function properly.
How to add fonts from Google Fonts?
How to reuse project’s webpack config?
See in configuring webpack.
How to use React Styleguidist with Redux, Relay or Styled Components?
How to use React-axe to test accessibility of components?
Load React-axe with the style guide and run checks for each example:
- Start your style guide server and open your browser’s developer tools console.
If you are using Jest for testing you can also use jest-axe.
How to change the names of components displayed in Styleguidist UI?
You might want to change your components’ names to be displayed differently, for example, for stylistic purposes or to give them more descriptive names in your style guide.
This can be done by adding @visibleName tag to your component documentation.
In case you want to change components’ names in bulk, for example, based on their current name, you can use updateDocs config option:
How to re-use the types in Styleguidist?
From version 10, Styleguidist is written using TypeScript language.
It allows the maintainers to catch type mismatch before execution and gives them a better developer experience.
NOTE: Since all files in
src/client/rsg-components are aliased to
rsg-components using webpack, you will have to add this alias to your
This way when you write the following component, TypeScript will resolve typings for client components and help you type them properly.
What’s the difference between Styleguidist and Storybook?
Both tools are good and mature, they have many similarities but also some distinctions that may make you choose one or the other. For me, the biggest distinction is how you describe component variations.
And with Styleguidist you write examples in Markdown files:
Another important distinction is that Storybook shows only one variation of one component at a time but Styleguidist can show all variations of all components, all variations of a single component or one variation. It’s easier to create a style guide with Styleguidist but Storybook has more tools to develop components (though we’re working on that too).
|Public methods docs||No||Yes|
¹ All components on a single page.
² Include non-component documentation.
Are there any other projects like this?
- Catalog, create living style guides using Markdown or React.
- Cosmos, a tool for designing encapsulated React components.
- Docz, a tool for documenting your components with zero configuration and live preview.
- React Storybook, isolate your React UI Component development from the main app.
- Atellier, a React components emulator. Last release 2016.
- Carte Blanche, an isolated development space with integrated fuzz testing for your components. Last release 2016.
- React BlueKit, render React components with editable source and live preview. Last release 2017.
- React Cards, devcards for React. Last release 2017.
- React Styleguide Generator, a React style guide generator. Last release 2017.
- React-demo, a component for creating demos of other components with props editor. Last release 2017.
- SourceJS, a platform to unify all your frontend documentation. It has a Styleguidist plugin. On hold.