Skip to content

Commit 7bf553f

Browse files
Enable strict mode in storybook on a per story basis (#3333)
* add checkbox to enable strict mode on a story by story basis * checkbox for strict * getting rid of storybook globals in favor of state the globals are updating anymore for some reason... * me dumb, me forget to convert to boolean * removing style Co-authored-by: Robert Snow <[email protected]>
1 parent ee3cc80 commit 7bf553f

File tree

5 files changed

+82
-6
lines changed

5 files changed

+82
-6
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {addons, makeDecorator} from '@storybook/addons';
2+
import {getQueryParams} from '@storybook/client-api';
3+
import React, {StrictMode, useEffect, useState} from 'react';
4+
5+
function StrictModeDecorator(props) {
6+
let {children} = props;
7+
let [isStrict, setStrict] = useState(getQueryParams()?.strict === 'true' || false);
8+
9+
useEffect(() => {
10+
let channel = addons.getChannel();
11+
let updateStrict = (val) => {
12+
setStrict(val);
13+
};
14+
channel.on('strict/updated', updateStrict);
15+
return () => {
16+
channel.removeListener('strict/updated', updateStrict);
17+
};
18+
}, []);
19+
20+
return isStrict ? (
21+
<StrictMode>
22+
{children}
23+
</StrictMode>
24+
) : children;
25+
}
26+
27+
export const withStrictModeSwitcher = makeDecorator({
28+
name: 'withStrictModeSwitcher',
29+
parameterName: 'strictModeSwitcher',
30+
wrapper: (getStory, context) => {
31+
return (
32+
<StrictModeDecorator>
33+
{getStory(context)}
34+
</StrictModeDecorator>
35+
);
36+
}
37+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {addons, types} from '@storybook/addons';
2+
import {getQueryParams} from '@storybook/client-api';
3+
import React, {useEffect, useState} from 'react';
4+
5+
const StrictModeToolBar = ({api}) => {
6+
let channel = addons.getChannel();
7+
let [isStrict, setStrict] = useState(getQueryParams()?.strict === 'true' || false);
8+
let onChange = () => {
9+
setStrict((old) => {
10+
channel.emit('strict/updated', !old);
11+
return !old;
12+
})
13+
};
14+
15+
useEffect(() => {
16+
api.setQueryParams({
17+
'strict': isStrict
18+
});
19+
});
20+
21+
return (
22+
<div style={{display: 'flex', alignItems: 'center', fontSize: '12px'}}>
23+
<div style={{marginRight: '10px'}}>
24+
<label htmlFor="strictmode">StrictMode:
25+
<input type="checkbox" id="strictmode" name="strictmode" checked={isStrict} onChange={onChange} />
26+
</label>
27+
</div>
28+
</div>
29+
);
30+
};
31+
32+
addons.register('StrictModeSwitcher', (api) => {
33+
addons.add('StrictModeSwitcher', {
34+
title: 'Strict mode switcher',
35+
type: types.TOOL,
36+
//👇 Shows the Toolbar UI element if either the Canvas or Docs tab is active
37+
match: ({ viewMode }) => !!(viewMode && viewMode.match(/^(story|docs)$/)),
38+
render: () => <StrictModeToolBar api={api} />
39+
});
40+
});

.storybook/main.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ module.exports = {
1111
'storybook-dark-mode',
1212
'./custom-addons/provider/register',
1313
'./custom-addons/descriptions/register',
14-
'./custom-addons/theme/register'
14+
'./custom-addons/theme/register',
15+
'./custom-addons/strictmode/register'
1516
],
1617
typescript: {
1718
check: false,
1819
reactDocgen: false
19-
},
20-
reactOptions: {
21-
strictMode: process.env.STRICT_MODE
22-
},
20+
}
2321
};

.storybook/preview.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {configureActions} from '@storybook/addon-actions';
22
import React from 'react';
33
import {VerticalCenter} from './layout';
44
import {withProviderSwitcher} from './custom-addons/provider';
5+
import {withStrictModeSwitcher} from './custom-addons/strictmode';
56

67
// decorator order matters, the last one will be the outer most
78

@@ -29,5 +30,6 @@ export const decorators = [
2930
<Story />
3031
</VerticalCenter>
3132
),
33+
withStrictModeSwitcher,
3234
withProviderSwitcher
3335
];

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"install-16": "yarn add -W react@^16.8.0 react-dom@^16.8.0 @testing-library/react@^12 @testing-library/react-hooks@^8",
1313
"install-17": "yarn add -W react@^17 react-dom@^17 @testing-library/react@^12 @testing-library/react-hooks@^8",
1414
"start": "cross-env NODE_ENV=storybook start-storybook -p 9003 --ci -c '.storybook'",
15-
"start-strict": "cross-env NODE_ENV=storybook STRICT_MODE=1 start-storybook -p 9003 --ci -c '.storybook'",
1615
"build:storybook": "build-storybook -c .storybook -o dist/$(git rev-parse HEAD)/storybook",
1716
"build:storybook-16": "build-storybook -c .storybook -o dist/$(git rev-parse HEAD)/storybook-16",
1817
"build:storybook-17": "build-storybook -c .storybook -o dist/$(git rev-parse HEAD)/storybook-17",

0 commit comments

Comments
 (0)