-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Based on - https://github.com/storybookjs/storybook/tree/next/code/frameworks/nextjs - https://storybook.js.org/blog/integrate-nextjs-and-storybook-automatically/
- Loading branch information
Showing
43 changed files
with
1,766 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
```js | ||
// .storybook/main.js | ||
export default { | ||
// ... | ||
framework: { | ||
// name: '@storybook/react-webpack5', // 👈 Remove this | ||
name: '@storybook/nextjs', // 👈 Add this | ||
options: { | ||
builder: { | ||
// Set useSWC to true if you want to try out the experimental SWC compiler in Next.js >= 14.0.0 | ||
useSWC: true, | ||
}, | ||
}, | ||
}, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
```ts | ||
// .storybook/main.ts | ||
import { StorybookConfig } from '@storybook/nextjs'; | ||
|
||
const config: StorybookConfig = { | ||
// ... | ||
framework: { | ||
// name: '@storybook/react-webpack5', // 👈 Remove this | ||
name: '@storybook/nextjs', // 👈 Add this | ||
options: { | ||
builder: { | ||
// Set useSWC to true if you want to try out the experimental SWC compiler in Next.js >= 14.0.0 | ||
useSWC: true, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default config; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
```js | ||
// SomeComponentThatUsesTheNavigation.stories.js | ||
import SomeComponentThatUsesTheNavigation from './SomeComponentThatUsesTheNavigation'; | ||
|
||
export default { | ||
component: SomeComponentThatUsesTheNavigation, | ||
parameters: { | ||
nextjs: { | ||
appDirectory: true, // 👈 Set this | ||
}, | ||
}, | ||
}; | ||
``` |
16 changes: 16 additions & 0 deletions
16
docs/snippets/react/nextjs-app-directory-in-meta.ts-4-9.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
```ts | ||
// SomeComponentThatUsesTheNavigation.stories.ts | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import SomeComponentThatUsesTheNavigation from './SomeComponentThatUsesTheNavigation'; | ||
|
||
const meta = { | ||
component: SomeComponentThatUsesTheNavigation, | ||
parameters: { | ||
nextjs: { | ||
appDirectory: true, // 👈 Set this | ||
}, | ||
}, | ||
} satisfies Meta<typeof SomeComponentThatUsesTheNavigation>; | ||
export default meta; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
```ts | ||
// SomeComponentThatUsesTheNavigation.stories.ts | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import SomeComponentThatUsesTheNavigation from './SomeComponentThatUsesTheNavigation'; | ||
|
||
const meta: Meta<typeof SomeComponentThatUsesTheNavigation> = { | ||
component: SomeComponentThatUsesTheNavigation, | ||
parameters: { | ||
nextjs: { | ||
appDirectory: true, // 👈 Set this | ||
}, | ||
}, | ||
}; | ||
export default meta; | ||
``` |
13 changes: 13 additions & 0 deletions
13
docs/snippets/react/nextjs-app-directory-in-preview.js.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
```js | ||
// .storybook/preview.js | ||
|
||
export default { | ||
// ... | ||
parameters: { | ||
// ... | ||
nextjs: { | ||
appDirectory: true, | ||
}, | ||
}, | ||
}; | ||
``` |
16 changes: 16 additions & 0 deletions
16
docs/snippets/react/nextjs-app-directory-in-preview.ts.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
```ts | ||
// .storybook/preview.ts | ||
import { Preview } from '@storybook/react'; | ||
|
||
const preview: Preview = { | ||
// ... | ||
parameters: { | ||
// ... | ||
nextjs: { | ||
appDirectory: true, | ||
}, | ||
}, | ||
}; | ||
|
||
export default preview; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
```js | ||
// .storybook/main.js | ||
export default { | ||
// ... | ||
webpackFinal: async (config) => { | ||
config.module = config.module || {}; | ||
config.module.rules = config.module.rules || []; | ||
|
||
// This modifies the existing image rule to exclude .svg files | ||
// since you want to handle those files with @svgr/webpack | ||
const imageRule = config.module.rules.find((rule) => rule?.['test']?.test('.svg')); | ||
if (imageRule) { | ||
imageRule['exclude'] = /\.svg$/; | ||
} | ||
|
||
// Configure .svg files to be loaded with @svgr/webpack | ||
config.module.rules.push({ | ||
test: /\.svg$/, | ||
use: ['@svgr/webpack'], | ||
}); | ||
|
||
return config; | ||
}, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
```ts | ||
// .storybook/main.ts | ||
import { StorybookConfig } from '@storybook/nextjs'; | ||
|
||
const config: StorybookConfig = { | ||
// ... | ||
webpackFinal: async (config) => { | ||
config.module = config.module || {}; | ||
config.module.rules = config.module.rules || []; | ||
|
||
// This modifies the existing image rule to exclude .svg files | ||
// since you want to handle those files with @svgr/webpack | ||
const imageRule = config.module.rules.find((rule) => rule?.['test']?.test('.svg')); | ||
if (imageRule) { | ||
imageRule['exclude'] = /\.svg$/; | ||
} | ||
|
||
// Configure .svg files to be loaded with @svgr/webpack | ||
config.module.rules.push({ | ||
test: /\.svg$/, | ||
use: ['@svgr/webpack'], | ||
}); | ||
|
||
return config; | ||
}, | ||
}; | ||
|
||
export default config; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
```js | ||
// .storybook/main.js | ||
export default { | ||
// ... | ||
staticDirs: [ | ||
{ | ||
from: '../src/components/fonts', | ||
to: 'src/components/fonts', | ||
}, | ||
], | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
```ts | ||
// .storybook/main.ts | ||
import { StorybookConfig } from '@storybook/nextjs'; | ||
|
||
const config: StorybookConfig = { | ||
// ... | ||
staticDirs: [ | ||
{ | ||
from: '../src/components/fonts', | ||
to: 'src/components/fonts', | ||
}, | ||
], | ||
}; | ||
|
||
export default config; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```shell | ||
npm install --save-dev @storybook/nextjs | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```shell | ||
pnpm install --save-dev @storybook/nextjs | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```shell | ||
yarn add --dev @storybook/nextjs | ||
``` |
28 changes: 28 additions & 0 deletions
28
docs/snippets/react/nextjs-navigation-override-in-story.js.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
```js | ||
// SomeComponentThatUsesTheNavigation.stories.js | ||
import SomeComponentThatUsesTheNavigation from './SomeComponentThatUsesTheNavigation'; | ||
|
||
export default { | ||
component: SomeComponentThatUsesTheNavigation, | ||
parameters: { | ||
nextjs: { | ||
appDirectory: true, | ||
}, | ||
}, | ||
}; | ||
|
||
// If you have the actions addon, | ||
// you can interact with the links and see the route change events there | ||
export const Example = { | ||
parameters: { | ||
nextjs: { | ||
navigation: { | ||
pathname: '/profile', | ||
query: { | ||
user: '1', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
``` |
33 changes: 33 additions & 0 deletions
33
docs/snippets/react/nextjs-navigation-override-in-story.ts-4-9.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
```ts | ||
// SomeComponentThatUsesTheNavigation.stories.ts | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import SomeComponentThatUsesTheNavigation from './SomeComponentThatUsesTheNavigation'; | ||
|
||
const meta = { | ||
component: SomeComponentThatUsesTheNavigation, | ||
parameters: { | ||
nextjs: { | ||
appDirectory: true, | ||
}, | ||
}, | ||
} satisfies Meta<typeof SomeComponentThatUsesTheNavigation>; | ||
export default meta; | ||
|
||
type Story = StoryObj<typeof Meta>; | ||
|
||
// If you have the actions addon, | ||
// you can interact with the links and see the route change events there | ||
export const Example: Story = { | ||
parameters: { | ||
nextjs: { | ||
navigation: { | ||
pathname: '/profile', | ||
query: { | ||
user: '1', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
``` |
33 changes: 33 additions & 0 deletions
33
docs/snippets/react/nextjs-navigation-override-in-story.ts.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
```ts | ||
// SomeComponentThatUsesTheNavigation.stories.ts | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import SomeComponentThatUsesTheNavigation from './SomeComponentThatUsesTheNavigation'; | ||
|
||
const meta: Meta<typeof SomeComponentThatUsesTheNavigation> = { | ||
component: SomeComponentThatUsesTheNavigation, | ||
parameters: { | ||
nextjs: { | ||
appDirectory: true, | ||
}, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
type Story = StoryObj<typeof SomeComponentThatUsesTheNavigation>; | ||
|
||
// If you have the actions addon, | ||
// you can interact with the links and see the route change events there | ||
export const Example: Story = { | ||
parameters: { | ||
nextjs: { | ||
navigation: { | ||
pathname: '/profile', | ||
query: { | ||
user: '1', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
``` |
17 changes: 17 additions & 0 deletions
17
docs/snippets/react/nextjs-navigation-push-override-in-preview-incomplete.js.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
```js | ||
// .storybook/preview.js | ||
|
||
export default { | ||
// ... | ||
parameters: { | ||
// ... | ||
nextjs: { | ||
navigation: { | ||
push() { | ||
// The default implementation that logs the action into the Actions panel is lost | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
``` |
20 changes: 20 additions & 0 deletions
20
docs/snippets/react/nextjs-navigation-push-override-in-preview-incomplete.ts.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
```ts | ||
// .storybook/preview.ts | ||
import { Preview } from '@storybook/react'; | ||
|
||
const preview: Preview = { | ||
// ... | ||
parameters: { | ||
// ... | ||
nextjs: { | ||
navigation: { | ||
push() { | ||
// The default implementation that logs the action into the Actions panel is lost | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default preview; | ||
``` |
21 changes: 21 additions & 0 deletions
21
docs/snippets/react/nextjs-navigation-push-override-in-preview.js.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
```js | ||
// .storybook/preview.js | ||
|
||
export default { | ||
// ... | ||
parameters: { | ||
// ... | ||
nextjs: { | ||
router: { | ||
navigation(...args) { | ||
// Custom logic can go here | ||
// This logs to the Actions panel | ||
action('nextNavigation.push')(...args); | ||
// Return whatever you want here | ||
return Promise.resolve(true); | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
``` |
24 changes: 24 additions & 0 deletions
24
docs/snippets/react/nextjs-navigation-push-override-in-preview.ts.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
```ts | ||
// .storybook/preview.ts | ||
import { Preview } from '@storybook/react'; | ||
|
||
const preview: Preview = { | ||
// ... | ||
parameters: { | ||
// ... | ||
nextjs: { | ||
navigation: { | ||
push(...args) { | ||
// Custom logic can go here | ||
// This logs to the Actions panel | ||
action('nextNavigation.push')(...args); | ||
// Return whatever you want here | ||
return Promise.resolve(true); | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default preview; | ||
``` |
Oops, something went wrong.