Cookbook
ref
s in examples?
How to use Use 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 .test.js
or .spec.js
) by default.
Use ignore option to customize this behavior:
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:
Or, to make these components available for all examples:
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:
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.
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:
How to use React Styleguidist with Preact?
You need to alias react
and react-dom
to 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:
info
See available theme variables.
info
Styles use JSS with these plugins: jss-isolate, jss-nested, jss-camel-case, jss-default-unit, jss-compose and jss-global.
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
then in ./styleguide/theme.js
and in ./styleguide/styles.js
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.
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.
You can replace the StyleGuideRenderer
component like this:
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:
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
- 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?
- Put
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?
Two options:
Put a
favicon.ico
file into the root folder of your site.Use template option:
How to add external JavaScript and CSS files?
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?
Use template and theme options:
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?
Install react-axe.
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.
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:
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.
With Storybook you write stories in JavaScript files:
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).
Feature | Storybook | Styleguidist |
---|---|---|
Component examples | JavaScript | Markdown |
Props docs | Yes | Yes |
Public methods docs | No | Yes |
Style guide¹ | No | Yes |
Customizable design | No | Yes |
Extra documentation² | No | Yes |
Plugins | Many | In development |
React | Yes | Yes |
Preact | Yes | Yes |
React Native | Yes | react-native-web |
Vue | Yes | Fork |
¹ 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.