Skip to content

Commit

Permalink
Merge pull request #26265 from storybookjs/framework-doc-react-webpack5
Browse files Browse the repository at this point in the history
Docs: Add `react-webpack5` framework doc
  • Loading branch information
kylegach authored Mar 11, 2024
2 parents 3004edf + f9646dd commit 298d60c
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 46 deletions.
48 changes: 2 additions & 46 deletions code/frameworks/react-webpack5/README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,3 @@
# Storybook for React
# Storybook for React & Webpack

Storybook for React is a UI development environment for your React components.
With it, you can visualize different states of your UI components and develop them interactively.

![Storybook Screenshot](https://github.com/storybookjs/storybook/blob/main/media/storybook-intro.gif)

Storybook runs outside of your app.
So you can develop UI components in isolation without worrying about app specific dependencies and requirements.

## Getting Started

```sh
cd my-react-app
npx storybook@latest init
```

For more information visit: [storybook.js.org](https://storybook.js.org)

---

Storybook also comes with a lot of [addons](https://storybook.js.org/addons) and a great API to customize as you wish.
You can also build a [static version](https://storybook.js.org/docs/react/sharing/publish-storybook) of your Storybook and deploy it anywhere you want.

Here are some featured storybooks that you can reference to see how Storybook works:

- [Demo of Storybook Design System](https://storybook.js.org/design-system) - [source](https://github.com/storybookjs/design-system)

## Create React App

Support for [Create React App](https://create-react-app.dev/) is handled by [`@storybook/preset-create-react-app`](https://github.com/storybookjs/presets/tree/master/packages/preset-create-react-app).

This preset enables support for all Create React App features, including Sass/SCSS and TypeScript.

If you're working on an app that was initialized manually (i.e., without the use of Create React App), ensure that your app has [react-dom](https://www.npmjs.com/package/react-dom) included as a dependency. Failing to do so can lead to unforeseen issues with Storybook and your project.

## Typescript

`@storybook/react` is now exporting its own types to use with Typescript.
You don't need to have `@types/storybook__react` installed anymore if it was your case.
But you probably also need to use types from `@types/node @types/react`.

## Docs

- [Basics](https://storybook.js.org/docs/react/get-started)
- [Configurations](https://storybook.js.org/docs/react/configure)
- [Addons](https://storybook.js.org/docs/react/configure/storybook-addons)
See [documentation](https://storybook.js.org/docs/8.0/get-started/react-webpack5?renderer=react) for installation instructions, usage examples, APIs, and more.
180 changes: 180 additions & 0 deletions docs/get-started/react-webpack5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
---
title: Storybook for React & Webpack
---

export const SUPPORTED_RENDERER = 'react';

Storybook for React & Webpack is a [framework](../contribute/framework.md) that makes it easy to develop and test UI components in isolation for [React](https://react.dev/) applications built with [Webpack](https://webpack.js.org/).

<If notRenderer={SUPPORTED_RENDERER}>

<Callout variant="info">

Storybook for React & Webpack is only supported in [React](?renderer=react) projects.

</Callout>

<!-- End non-supported renderers -->

</If>

<If renderer={SUPPORTED_RENDERER}>

## Requirements

- React ≥ 16.8
- Webpack ≥ 5.0
- Storybook ≥ 8.0

## Getting started

### In a project without Storybook

Follow the prompts after running this command in your React project's root directory:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/init-command.npx.js.mdx',
'common/init-command.yarn.js.mdx',
'common/init-command.pnpm.js.mdx',
]}
/>

<!-- prettier-ignore-end -->

[More on getting started with Storybook.](./install.md)

### In a project with Storybook

This framework is designed to work with Storybook 7+. If you’re not already using v7, upgrade with this command:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/storybook-upgrade.npm.js.mdx',
'common/storybook-upgrade.pnpm.js.mdx',
'common/storybook-upgrade.yarn.js.mdx'
]}
/>

<!-- prettier-ignore-end -->

#### Automatic migration

When running the `upgrade` command above, you should get a prompt asking you to migrate to `@storybook/react-webpack5`, which should handle everything for you. In case that auto-migration does not work for your project, refer to the manual migration below.

#### Manual migration

First, install the framework:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'react/react-webpack5-install.npm.js.mdx',
'react/react-webpack5-install.pnpm.js.mdx',
'react/react-webpack5-install.yarn.js.mdx'
]}
/>

<!-- prettier-ignore-end -->

Next, install and register your appropriate compiler addon, depending on whether you're using SWC (recommended) or Babel:

<Callout variant="info">

If your project is using [Create React App](#create-react-app-cra), you can skip this step.

</Callout>

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/storybook-addon-compiler-swc-auto-install.npm.js.mdx',
'common/storybook-addon-compiler-swc-auto-install.pnpm.js.mdx',
'common/storybook-addon-compiler-swc-auto-install.yarn.js.mdx',
]}
/>

<!-- prettier-ignore-end -->

or

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/storybook-addon-compiler-babel-auto-install.npm.js.mdx',
'common/storybook-addon-compiler-babel-auto-install.pnpm.js.mdx',
'common/storybook-addon-compiler-babel-auto-install.yarn.js.mdx',
]}
/>

<!-- prettier-ignore-end -->

More details can be found in the [Webpack builder docs](../builders/webpack.md#compiler-support).

Finally, update your `.storybook/main.js|ts` to change the framework property:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'react/react-webpack5-add-framework.js.mdx',
'react/react-webpack5-add-framework.ts.mdx'
]}
/>

<!-- prettier-ignore-end -->

## Run the Setup Wizard

If all goes well, you should see a setup wizard that will help you get started with Storybook introducing you to the main concepts and features, including how the UI is organized, how to write your first story, and how to test your components' response to various inputs utilizing [controls](../essentials/controls).

![Storybook onboarding](./example-onboarding-wizard.png)

If you skipped the wizard, you can always run it again by adding the `?path=/onboarding` query parameter to the URL of your Storybook instance, provided that the example stories are still available.

## Create React App (CRA)

Support for [Create React App](https://create-react-app.dev/) is handled by [`@storybook/preset-create-react-app`](https://github.com/storybookjs/presets/tree/master/packages/preset-create-react-app).

This preset enables support for all CRA features, including Sass/SCSS and TypeScript.

If you're working on an app that was initialized manually (i.e., without the use of CRA), ensure that your app has [react-dom](https://www.npmjs.com/package/react-dom) included as a dependency. Failing to do so can lead to unforeseen issues with Storybook and your project.

## API

### Options

You can pass an options object for additional configuration if needed:

```ts
// .storybook/main.ts
import type { StorybookConfig } from '@storybook/react-webpack5';

const config: StorybookConfig = {
framework: {
name: '@storybook/react-webpack5',
options: {
// ...
},
},
};

export default config;
```

#### `builder`

Type: `Record<string, any>`

Configure options for the [framework's builder](../api/main-config-framework.md#optionsbuilder). For this framework, available options can be found in the [Webpack builder docs](../builders/webpack.md).

<!-- End supported renderers -->

</If>
7 changes: 7 additions & 0 deletions docs/snippets/react/react-webpack5-add-framework.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```js
// .storybook/main.js
export default {
// ...
framework: '@storybook/react-webpack5', // 👈 Add this
};
```
11 changes: 11 additions & 0 deletions docs/snippets/react/react-webpack5-add-framework.ts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```ts
// .storybook/main.ts
import { StorybookConfig } from '@storybook/react-webpack5';

const config: StorybookConfig = {
// ...
framework: '@storybook/react-webpack5', // 👈 Add this
};

export default config;
```
3 changes: 3 additions & 0 deletions docs/snippets/react/react-webpack5-install.npm.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```shell
npm install --save-dev @storybook/react-webpack5
```
3 changes: 3 additions & 0 deletions docs/snippets/react/react-webpack5-install.pnpm.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```shell
pnpm install --save-dev @storybook/react-webpack5
```
3 changes: 3 additions & 0 deletions docs/snippets/react/react-webpack5-install.yarn.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```shell
yarn add --dev @storybook/react-webpack5
```
5 changes: 5 additions & 0 deletions docs/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ module.exports = {
title: 'React & Vite',
type: 'link',
},
{
pathSegment: 'react-webpack5',
title: 'React & Webpack',
type: 'link',
},
{
pathSegment: 'vue3-vite',
title: 'Vue & Vite',
Expand Down

0 comments on commit 298d60c

Please sign in to comment.