Skip to content

Commit

Permalink
Add Next.js framework doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kylegach committed Jan 17, 2024
1 parent 24b16ea commit a4f0795
Show file tree
Hide file tree
Showing 43 changed files with 1,766 additions and 0 deletions.
935 changes: 935 additions & 0 deletions docs/get-started/nextjs.md

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions docs/snippets/react/nextjs-add-framework.js.mdx
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,
},
},
},
};
```
20 changes: 20 additions & 0 deletions docs/snippets/react/nextjs-add-framework.ts.mdx
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;
```
13 changes: 13 additions & 0 deletions docs/snippets/react/nextjs-app-directory-in-meta.js.mdx
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 docs/snippets/react/nextjs-app-directory-in-meta.ts-4-9.mdx
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;
```
16 changes: 16 additions & 0 deletions docs/snippets/react/nextjs-app-directory-in-meta.ts.mdx
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 docs/snippets/react/nextjs-app-directory-in-preview.js.mdx
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 docs/snippets/react/nextjs-app-directory-in-preview.ts.mdx
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;
```
25 changes: 25 additions & 0 deletions docs/snippets/react/nextjs-configure-svgr.js.mdx
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;
},
};
```
29 changes: 29 additions & 0 deletions docs/snippets/react/nextjs-configure-svgr.ts.mdx
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;
```
12 changes: 12 additions & 0 deletions docs/snippets/react/nextjs-image-static-dirs.js.mdx
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',
},
],
};
```
16 changes: 16 additions & 0 deletions docs/snippets/react/nextjs-image-static-dirs.ts.mdx
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;
```
3 changes: 3 additions & 0 deletions docs/snippets/react/nextjs-install.npm.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```shell
npm install --save-dev @storybook/nextjs
```
3 changes: 3 additions & 0 deletions docs/snippets/react/nextjs-install.pnpm.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```shell
pnpm install --save-dev @storybook/nextjs
```
3 changes: 3 additions & 0 deletions docs/snippets/react/nextjs-install.yarn.js.mdx
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 docs/snippets/react/nextjs-navigation-override-in-story.js.mdx
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 docs/snippets/react/nextjs-navigation-override-in-story.ts-4-9.mdx
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 docs/snippets/react/nextjs-navigation-override-in-story.ts.mdx
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',
},
},
},
},
};
```
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
},
},
},
},
};
```
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;
```
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);
},
},
},
},
};
```
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;
```
Loading

0 comments on commit a4f0795

Please sign in to comment.