Skip to content

Commit 912a9d1

Browse files
committed
QueryParams addon preset
1 parent 4703c15 commit 912a9d1

File tree

7 files changed

+25
-6
lines changed

7 files changed

+25
-6
lines changed

addons/queryparams/README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ First, install the addon.
1010
$ yarn add @storybook/addon-queryparams --dev
1111
```
1212

13-
import the `withQuery` decorator so the url will be changed before rendering stories.
13+
Add this line to your `main.js` file (create this file inside your storybook config directory if needed).
14+
15+
```js
16+
module.exports = {
17+
addons: ['@storybook/addon-queryparams'],
18+
};
19+
```
1420

1521
```js
1622
import React from 'react';
17-
import { storiesOf, addDecorator } from '@storybook/react';
18-
import { withQuery } from '@storybook/addon-queryparams';
23+
import { storiesOf } from '@storybook/react';
1924

2025
storiesOf('button', module)
21-
.addDecorator(withQuery)
2226
.addParameters({
2327
query: {
2428
mock: true,

addons/queryparams/preset.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dist/preset');

addons/queryparams/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { PARAM_KEY } from './constants';
88
export const withQuery = makeDecorator({
99
name: 'withQuery',
1010
parameterName: PARAM_KEY,
11+
skipIfNoParametersOrOptions: true,
1112
wrapper: (getStory: StoryGetter, context: StoryContext, { parameters }) => {
1213
const { location } = document;
1314
const currentQuery = qs.parse(location.search, { ignoreQueryPrefix: true });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { withQuery } from '../index';
2+
3+
export const decorators = [withQuery];
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type QueryParamsOptions = {
2+
addDecorator?: boolean;
3+
};
4+
5+
export function config(entry: any[] = [], { addDecorator = true }: QueryParamsOptions = {}) {
6+
const queryParamsConfig = [];
7+
if (addDecorator) {
8+
queryParamsConfig.push(require.resolve('./addDecorator'));
9+
}
10+
return [...entry, ...queryParamsConfig];
11+
}

examples/official-storybook/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
'@storybook/addon-viewport',
2323
'@storybook/addon-graphql',
2424
'@storybook/addon-contexts',
25+
'@storybook/addon-queryparams',
2526
],
2627
webpackFinal: async (config, { configType }) => ({
2728
...config,

examples/official-storybook/stories/addon-queryparams.stories.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { document } from 'global';
22
import React from 'react';
3-
import { withQuery } from '@storybook/addon-queryparams';
43

54
export default {
65
title: 'Addons/QueryParams',
7-
decorators: [withQuery],
86

97
parameters: {
108
query: {

0 commit comments

Comments
 (0)