Skip to content

Commit

Permalink
Merge pull request #25534 from storybookjs/framework-doc-nextjs
Browse files Browse the repository at this point in the history
Docs: Add Next.js framework doc
  • Loading branch information
kylegach authored Jan 24, 2024
2 parents 3eeb562 + 7bdefcb commit 1010f2f
Show file tree
Hide file tree
Showing 40 changed files with 1,607 additions and 1,052 deletions.
1,055 changes: 3 additions & 1,052 deletions code/frameworks/nextjs/README.md

Large diffs are not rendered by default.

935 changes: 935 additions & 0 deletions docs/get-started/nextjs.md

Large diffs are not rendered by default.

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

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

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
// NavigationBasedComponent.stories.js
import NavigationBasedComponent from './NavigationBasedComponent';

export default {
component: NavigationBasedComponent,
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
// NavigationBasedComponent.stories.ts
import { Meta, StoryObj } from '@storybook/react';

import NavigationBasedComponent from './NavigationBasedComponent';

const meta = {
component: NavigationBasedComponent,
parameters: {
nextjs: {
appDirectory: true, // 👈 Set this
},
},
} satisfies Meta<typeof NavigationBasedComponent>;
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
// NavigationBasedComponent.stories.ts
import { Meta, StoryObj } from '@storybook/react';

import NavigationBasedComponent from './NavigationBasedComponent';

const meta: Meta<typeof NavigationBasedComponent> = {
component: NavigationBasedComponent,
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
// NavigationBasedComponent.stories.js
import NavigationBasedComponent from './NavigationBasedComponent';

export default {
component: NavigationBasedComponent,
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
// NavigationBasedComponent.stories.ts
import { Meta, StoryObj } from '@storybook/react';

import NavigationBasedComponent from './NavigationBasedComponent';

const meta = {
component: NavigationBasedComponent,
parameters: {
nextjs: {
appDirectory: true,
},
},
} satisfies Meta<typeof NavigationBasedComponent>;
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
// NavigationBasedComponent.stories.ts
import { Meta, StoryObj } from '@storybook/react';

import NavigationBasedComponent from './NavigationBasedComponent';

const meta: Meta<typeof NavigationBasedComponent> = {
component: NavigationBasedComponent,
parameters: {
nextjs: {
appDirectory: true,
},
},
};
export default meta;

type Story = StoryObj<typeof NavigationBasedComponent>;

// 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,21 @@
```js
// .storybook/preview.js

export default {
// ...
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);
},
},
},
},
};
```
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;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
```js
// NavigationBasedComponent.stories.js
import NavigationBasedComponent from './NavigationBasedComponent';

export default {
component: NavigationBasedComponent,
parameters: {
nextjs: {
appDirectory: true,
navigation: {
segments: [
['slug', 'hello'],
['framework', 'nextjs'],
],
},
},
},
};
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
```ts
// NavigationBasedComponent.stories.ts
import { Meta, StoryObj } from '@storybook/react';

import NavigationBasedComponent from './NavigationBasedComponent';

const meta = {
component: NavigationBasedComponent,
parameters: {
nextjs: {
appDirectory: true,
navigation: {
segments: [
['slug', 'hello'],
['framework', 'nextjs'],
],
},
},
},
} satisfies Meta<typeof NavigationBasedComponent>;
export default meta;
```
Loading

0 comments on commit 1010f2f

Please sign in to comment.