Component Documentation

Your components' docs are generated as part of the preview process. Change the preview's configuration to customize how your docs (and compositions) are bundled and rendered.

To customize your components' preview, implement the preview method in your env.

/* @filename: my-react-env.bit-env.ts */

import { ReactEnv } from '@teambit/react.react-env';

import { Preview } from '@teambit/preview';
import { ReactPreview } from '@teambit/preview.react-preview';
import webpackTransformer from './config/webpack.config';

export class MyReactEnv extends ReactEnv {
  preview(): EnvHandler<Preview> {
    return ReactPreview.from({
      /* preview config goes here */
    });
  }
}

export default new MyReactEnv();
CopiedCopy

A component's preview configuration does not affect production code.

Webpack configuration

Review your preview's webpack config by running the following on a component that uses your env:

$bit
Copiedcopy

The output is similar to the following:

Environment: my-org.my-scope/examples/my-react-env

teambit.compilation/bundler

dev server config:

{ mode: 'development', devtool: 'inline-source-map', ... } ...

To modify your preview's Webpack config, implement the preview method in your custom env. Provide the transformers property with an array of Webpack config transformers.

For example:

// @filename: my-react-env.bit-env.ts
// ...
import webpackTransformer from './config/webpack.config';

export class MyReactEnv extends ReactEnv {
  preview(): EnvHandler<Preview> {
    return ReactPreview.from({
      transformers: [webpackTransformer],
    });
  }
}

export default new MyReactEnv();
CopiedCopy

Learn how to create a Webpack transformer in Webpack transformers.

Docs

Docs are displayed in the components' 'Overview' tab. A component's documentation describes the function and purpose of the component and instructs on how to use it.

Doc files glob pattern

Update the glob patterns listed in your env's env.jsonc file to determine which files, in a component's directory, should be loaded as docs.

{
  "patterns": {
    "docs": ["**/*.docs.*", "**/*.readme.*"]
  }
}
CopiedCopy

Since docs are only used for development, every docs file is considered as a dev file.

A single component cannot have more than a single docs file.

Docs providers

Your components' documentation might require a certain context, such as a theme provider or a language provider.

The following example wraps every docs page with a theme provider:

my-react-env.bit-env.ts
preview/docs.tsx

Docs template

The docs-template component is responsible for rendering your components' documentation. Replace the default template with your own component, to modify the way documentation is being rendered.

Run the following to fork the default docs template:

$bit
Copiedcopy

Modify the forked component (docs template) and use it to replace the default docs template:

my-react-env.bit-env.ts
preview/docs.tsx

For example, see this docs template component which replaces the root.render with the older ReactDOM.render to mount React 17 components.