Working with third-party libraries
How Styleguidist works
Styleguidist loads your components (see Loading and exposing components for more) but it uses react-docgen to generate documentation which may require changes in your code to work properly.
React-docgen reads your components as static text files and looks for patterns like class or function declarations that look like React components. It does not run any JavaScript code, so, if your component is dynamically generated, is wrapped in a higher-order component, or is split into several files, then react-docgen may not understand it.
It supports components defined via React.createClass
, ES6 classes and function components, with optional Flow and TypeScript type annotations.
In many cases you may trick Styleguidist and react-docgen by exporting both components: an enhanced component as the default export and a base component as a named export:
Each example is rendered in an independent React root. You can control React Context by defining a custom Wrapper component like this:
Please see our examples and refer to react-docgen documentation for more information about what types of syntax are supported.
While Styleguidist supports TypeScript out of the box, thanks to react-docgen
, this support is limited. Consider this example:
Here we’re reexporting a third-party component from node_modules
. Styleguidist won’t be able to render prop types of this component, unless we’re using react-docgen-typescript
:
Install react-docgen-typescript.
Create a
styleguide.config.js
, see configuration reference.Update your
styleguide.config.js
:module.exports = {propsParser: require('react-docgen-typescript').withCustomConfig('./tsconfig.json').parse}
Using Styleguidist with other libraries
Redux
To use Redux store with one component, import it from your Markdown example:
To use Redux store in every component, redefine the Wrapper
component:
Relay
First, mock out Relay. You’ll need the content from this Gist for your mocked-out Relay replacement.
Second, provide sample data to your React components to send actual results from your GraphQL backend:
Based on @mikberg’s blog post.
Styled-components
To show PropTypes documentation for styled-components, you need to add the @component
JSDoc annotation to the component export:
caution
Object notation isn’t supported yet, use string literals instead:
caution
Other use case for calling the styled
factory as a function, like styled-system, aren’t supported too:
ThemeProvider
Adding styled-components If your styled-components require a theme to render properly, add a ThemeProvider
to your style guide.
First, create a Provider
component:
Next, add Provider
to your styleguide.config.js
:
This will automatically apply your theme to your styled-components. When you open the style guide by running npx styleguidist server
, you should see your components render as expected.
Emotion
The usage is similar to styled-components.
Theme UI
The usage is similar to Adding styled-components ThemeProvider
.
Fela
Check out the official example.
CSS Modules with react-css-modules
You need to export two components: (1) unstyled React component as named export and (2) enhanced component as a default export:
This approach will also work with react-css-themr and other similar libraries.
Styletron
To use Styletron store with one component, require it from your example:
To use Styletron in every component redefine the Wrapper component: