Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .storybook/custom-addons/strictmode/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {addons, makeDecorator} from '@storybook/addons';
import {getQueryParams} from '@storybook/client-api';
import React, {StrictMode, useEffect, useState} from 'react';

function StrictModeDecorator(props) {
let {children} = props;
let [isStrict, setStrict] = useState(getQueryParams()?.strict === 'true' || false);

useEffect(() => {
let channel = addons.getChannel();
let updateStrict = (val) => {
setStrict(val);
};
channel.on('strict/updated', updateStrict);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would've like to used the storybook globals but for some reason it stopped working

return () => {
channel.removeListener('strict/updated', updateStrict);
};
}, []);

return isStrict ? (
<StrictMode>
{children}
</StrictMode>
Comment thread
LFDanLu marked this conversation as resolved.
) : children;
}

export const withStrictModeSwitcher = makeDecorator({
name: 'withStrictModeSwitcher',
parameterName: 'strictModeSwitcher',
wrapper: (getStory, context) => {
return (
<StrictModeDecorator>
{getStory(context)}
</StrictModeDecorator>
);
}
});
40 changes: 40 additions & 0 deletions .storybook/custom-addons/strictmode/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {addons, types} from '@storybook/addons';
import {getQueryParams} from '@storybook/client-api';
import React, {useEffect, useState} from 'react';

const StrictModeToolBar = ({api}) => {
let channel = addons.getChannel();
let [isStrict, setStrict] = useState(getQueryParams()?.strict === 'true' || false);
let onChange = () => {
setStrict((old) => {
channel.emit('strict/updated', !old);
return !old;
})
};

useEffect(() => {
api.setQueryParams({
'strict': isStrict
});
});

return (
<div style={{display: 'flex', alignItems: 'center', fontSize: '12px'}}>
<div style={{marginRight: '10px'}}>
<label htmlFor="strictmode">StrictMode:
<input type="checkbox" id="strictmode" name="strictmode" checked={isStrict} onChange={onChange} />
</label>
</div>
</div>
);
};

addons.register('StrictModeSwitcher', (api) => {
addons.add('StrictModeSwitcher', {
title: 'Strict mode switcher',
type: types.TOOL,
//👇 Shows the Toolbar UI element if either the Canvas or Docs tab is active
match: ({ viewMode }) => !!(viewMode && viewMode.match(/^(story|docs)$/)),
render: () => <StrictModeToolBar api={api} />
});
});
8 changes: 3 additions & 5 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ module.exports = {
'storybook-dark-mode',
'./custom-addons/provider/register',
'./custom-addons/descriptions/register',
'./custom-addons/theme/register'
'./custom-addons/theme/register',
'./custom-addons/strictmode/register'
],
typescript: {
check: false,
reactDocgen: false
},
reactOptions: {
strictMode: process.env.STRICT_MODE
},
}
};
2 changes: 2 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {configureActions} from '@storybook/addon-actions';
import React from 'react';
import {VerticalCenter} from './layout';
import {withProviderSwitcher} from './custom-addons/provider';
import {withStrictModeSwitcher} from './custom-addons/strictmode';

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

Expand Down Expand Up @@ -29,5 +30,6 @@ export const decorators = [
<Story />
</VerticalCenter>
),
withStrictModeSwitcher,
withProviderSwitcher
];
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"install-16": "yarn add -W react@^16.8.0 react-dom@^16.8.0 @testing-library/react@^12 @testing-library/react-hooks@^8",
"install-17": "yarn add -W react@^17 react-dom@^17 @testing-library/react@^12 @testing-library/react-hooks@^8",
"start": "cross-env NODE_ENV=storybook start-storybook -p 9003 --ci -c '.storybook'",
"start-strict": "cross-env NODE_ENV=storybook STRICT_MODE=1 start-storybook -p 9003 --ci -c '.storybook'",
"build:storybook": "build-storybook -c .storybook -o dist/$(git rev-parse HEAD)/storybook",
"build:storybook-16": "build-storybook -c .storybook -o dist/$(git rev-parse HEAD)/storybook-16",
"build:storybook-17": "build-storybook -c .storybook -o dist/$(git rev-parse HEAD)/storybook-17",
Expand Down