Cookbook

How to use refs in examples?

Use ref prop as a function and assign a reference to a local variable:

const [value, setValue] = React.useState('')
let textarea
;<div>
<Button onClick={() => textarea.insertAtCursor('Pizza')}>
Insert
</Button>
<Textarea
value={value}
onChange={e => setValue(e.target.value)}
ref={ref => (textarea = ref)}
/>
</div>

How to exclude some components from style guide?

Styleguidist will ignore tests (__tests__ folder and filenames containing .test.js or .spec.js) by default.

Use ignore option to customize this behavior:

module.exports = {
ignore: ['**/*.spec.js', '**/components/Button.js']
}
caution

You should pass glob patterns, for example, use **/components/Button.js instead of components/Button.js.

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:

// ```jsx inside Markdown
import Button from '../common/Button'
;<Button>Push Me Tender</Button>

Or, to make these components available for all examples:

// styleguide.config.js
module.exports = {
require: [path.resolve(__dirname, 'styleguide/setup.js')]
}
// styleguide/setup.js
import Button from './src/components/common/Button'
global.Button = Button

The Button component will be available in every example without a need to import it.

How to render React components that aren’t part of the style guide?

Import these components in your examples:

// ```jsx or ```jsx noeditor inside Markdown
import ColorPalette from './components/ColorPalette'
;<ColorPalette />

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:

// load-icons.js
const iconsContext = require.context('./icons/', true, /js$/)
const icons = iconsContext.keys().reduce((icons, file) => {
const Icon = iconsContext(file).default
const label = file.slice(2, -3) // strip './' and '.js'
icons[label] = Icon
return icons
}, {})
export default icons
// ```jsx or ```jsx noeditor inside Markdown
import icons from './load-icons'
const iconElements = Object.keys(icons).map(iconName => {
const Icon = icons[iconName]
return (
<span key={iconName}>
{iconName}: {<Icon />}
</span>
)
})
<div>{iconElements}</div>

How to display the source code of any file?

First, code examples can receive props and settings:

```js { "file": "../mySourceCode.js" }
```

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:

module.exports = {
updateExample(props, exampleFilePath) {
// props.settings are passed by any fenced code block, in this case
const { settings, lang } = props
// "../mySourceCode.js"
if (typeof settings.file === 'string') {
// "absolute path to mySourceCode.js"
const filepath = path.resolve(exampleFilePath, settings.file)
// displays the block as static code
settings.static = true
// no longer needed
delete settings.file
return {
content: fs.readFileSync(filepath, 'utf8'),
settings,
lang
}
}
return props
}
}

How to set global styles for user components?

Using the jss-global API you can set global styles in your config:

module.exports = {
components: 'src/components/**/[A-Z]*.js',
styles: {
StyleGuide: {
'@global body': {
fontFamily: 'Helvetica'
}
}
}
}

Above, we have set font-family: 'Helvetica'; on the body.

tip

This does not set styles on the style guide UI, for that read How to change styles of a style guide.

How to add custom JavaScript and CSS or polyfills?

In your style guide config:

const path = require('path')
module.exports = {
require: [
'babel-polyfill',
path.join(__dirname, 'path/to/script.js'),
path.join(__dirname, 'path/to/styles.css')
]
}

How to use React Styleguidist with Preact?

You need to alias react and react-dom to preact-compat:

module.exports = {
webpackConfig: {
resolve: {
alias: {
react: 'preact-compat',
'react-dom': 'preact-compat'
}
}
}
}

See the Preact example style guide.

How to change styles of a style guide?

There are two config options to change your style guide UI: theme and styles.

Use theme to change fonts, colors, etc.

Use styles to tweak the style of any particular Styleguidist component.

As an example:

module.exports = {
theme: {
color: {
link: 'firebrick',
linkHover: 'salmon'
},
fontFamily: {
base: '"Comic Sans MS", "Comic Sans", cursive'
}
},
styles: {
Logo: {
// We're changing the LogoRenderer component
logo: {
// We're changing the rsg--logo-XX class name inside the component
animation: 'blink ease-in-out 300ms infinite'
},
'@keyframes blink': {
to: { opacity: 0 }
}
}
}
}
info

See available theme variables.

info
tip

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.

tip

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:

In styleguide.config,js, objects are replaced with file paths

module.exports = {
// ...
styles: './styleguide/styles.js',
theme: './styleguide/themes.js'
}

then in ./styleguide/theme.js

module.exports = {
color: {
link: 'firebrick',
linkHover: 'salmon'
},
fontFamily: {
base: '"Comic Sans MS", "Comic Sans", cursive'
}
}

and in ./styleguide/styles.js

module.exports = {
Logo: {
// We're changing the LogoRenderer component
logo: {
// We're changing the rsg--logo-XX class name inside the component
animation: 'blink ease-in-out 300ms infinite'
},
'@keyframes blink': {
to: { opacity: 0 }
}
}
}

Each modification of theme.js or 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.

module.exports = {
styles: function(theme) {
return {
Logo: {
logo: {
// we can now change the color used in the logo item to use the theme's `link` color
color: theme.color.link
}
}
}
}
}

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 ReactComponentRenderer, ComponentsListRenderer, 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.

// styleguide.config.js
const path = require('path')
module.exports = {
styleguideComponents: {
Wrapper: path.join(__dirname, 'src/styleguide/Wrapper')
}
}
// src/styleguide/Wrapper.js
import React, { Component } from 'react'
import { IntlProvider } from 'react-intl'
export default class Wrapper extends Component {
render() {
return (
<IntlProvider locale="en">{this.props.children}</IntlProvider>
)
}
}

You can replace the StyleGuideRenderer component like this:

// styleguide.config.js
const path = require('path')
module.exports = {
styleguideComponents: {
StyleGuideRenderer: path.join(
__dirname,
'src/styleguide/StyleGuideRenderer'
)
}
}
// src/styleguide/StyleGuideRenderer.js
import React from 'react'
const StyleGuideRenderer = ({
title,
version,
homepageUrl,
components,
toc,
hasSidebar
}) => (
<div className="root">
<h1>{title}</h1>
{version && <h2>{version}</h2>}
<main className="wrapper">
<div className="content">
{components}
<footer className="footer">
<Markdown
text={`Created with [React Styleguidist](${homepageUrl})`}
/>
</footer>
</div>
{hasSidebar && <div className="sidebar">{toc}</div>}
</main>
</div>
)

We have an example style guide with custom components.

How to change syntax highlighting colors?

Styleguidist uses Prism for code highlighting in static examples and inside the editor. You can change the colors using the theme config option:

// styleguide.config.js
module.exports = {
theme: {
color: {
codeComment: '#6d6d6d',
codePunctuation: '#999',
codeProperty: '#905',
codeDeleted: '#905',
codeString: '#690',
codeInserted: '#690',
codeOperator: '#9a6e3a',
codeKeyword: '#1673b1',
codeFunction: '#DD4A68',
codeVariable: '#e90'
}
}
}

How to change style guide dev server logs output?

You can modify webpack dev server logs format changing stats option of webpack config:

module.exports = {
webpackConfig(env) {
if (env === 'development') {
return {
stats: {
chunks: false,
chunkModules: false,
chunkOrigins: false
}
}
}
return {}
}
}

How to debug my components and examples?

  1. Open your browser’s developer tools
  2. Write 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?

  1. Put debugger; statement at the beginning of your code.
  2. Press the Debugger button in your browser’s developer tools.
  3. Press the Continue 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.

import React from 'react'
import { TextInput } from 'react-native'
const CustomInput = ({ value }) => <TextInput value={value} />
CustomInput.propTypes = {
// Will fail in a static build
value: TextInput.value.isRequired
}

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.

{
"scripts": {
"build": "cross-env NODE_ENV=development react-styleguidist build"
}
}

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:

devServer: {
watchOptions: {
poll: true
}
}

How to add a favicon?

Two options:

  1. Put a favicon.ico file into the root folder of your site.

  2. Use template option:

module.exports = {
template: {
favicon: 'https://assets-cdn.github.com/favicon.ico'
}
}

How to add external JavaScript and CSS files?

Use template option:

module.exports = {
template: {
head: {
scripts: [
{
src: 'assets/js/babelHelpers.min.js'
}
],
links: [
{
rel: 'stylesheet',
href:
'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css'
}
]
}
}
}

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?

Use template and theme options:

module.exports = {
template: {
head: {
links: [
{
rel: 'stylesheet',
href: 'https://fonts.googleapis.com/css?family=Roboto'
}
]
}
},
theme: {
fontFamily: {
base: '"Roboto", sans-serif'
}
}
}

How to reuse project’s webpack config?

See in configuring webpack.

How to use React Styleguidist with Redux, Relay or Styled Components?

See working with third-party libraries.

How to use React-axe to test accessibility of components?

  1. Install react-axe.

  2. Load React-axe with the style guide and run checks for each example:

// styleguide.config.js
module.exports = {
require: [path.resolve(__dirname, 'styleguide/setup.js')]
}
// styleguide/setup.js
import React from 'react'
import ReactDOM from 'react-dom'
var context = {
include: [['[data-preview]']]
}
if (process.env.NODE_ENV !== 'production') {
var axe = require('react-axe')
axe(React, ReactDOM, 1000, undefined, context)
}
  1. 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:

module.exports = {
updateDocs(docs) {
if (docs && docs.displayName) {
docs.visibleName = docs.displayName.toLowerCase()
}
return docs
}
}

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.

It also allows you to write customized style guide components using TypeScript TSX instead of JavaScript JSX.

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 tsconfig.json file:

{
"compilerOptions": {
"baseUrl": ".",
"paths": {
// remap rsg-components/anything to its version in react-styleguidist
"rsg-components/*": [
"node_modules/react-styleguidist/lib/client/rsg-components/*"
]
}
},
"include": ["src"],
"exclude": ["node_modules"]
}

This way when you write the following component, TypeScript will resolve typings for client components and help you type them properly.

import Styled from 'rsg-components/Styled'
import Heading from 'rsg-components/Heading'
export default function SectionsRenderer({ children }) {
return (
<div>
{children.length > 0 && (
<div>
<Heading level={1}>Example Components</Heading>
<p>These are the greatest components</p>
</div>
)}
<DefaultSectionsRenderer>{children}</DefaultSectionsRenderer>
</div>
)
}

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.

With Storybook you write stories in JavaScript files:

import React from 'react'
import { storiesOf } from '@storybook/react'
import { action } from '@storybook/addon-actions'
import Button from '../components/Button'
storiesOf('Button', module)
.add('default', () => (
<Button onClick={action('clicked')}>Push Me</Button>
))
.add('large size', () => <Button size="large">Push Me</Button>)

And with Styleguidist you write examples in Markdown files:

React button component example:
```js
<Button onClick={() => console.log('clicked')>Push Me</Button>
```
Large size:
```js
<Button size="large">Push Me</Button>
```

Styleguidist screenshot

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).

FeatureStorybookStyleguidist
Component examplesJavaScriptMarkdown
Props docsYesYes
Public methods docsNoYes
Style guide¹NoYes
Customizable designNoYes
Extra documentation²NoYes
PluginsManyIn development
ReactYesYes
PreactYesYes
React NativeYesreact-native-web
VueYesFork

¹ All components on a single page.
² Include non-component documentation.

Are there any other projects like this?

Active

  • 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.

Inactive

  • 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.