File tree Expand file tree Collapse file tree 5 files changed +82
-6
lines changed Expand file tree Collapse file tree 5 files changed +82
-6
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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 ( / ^ ( s t o r y | d o c s ) $ / ) ) ,
38
+ render : ( ) => < StrictModeToolBar api = { api } />
39
+ } ) ;
40
+ } ) ;
Original file line number Diff line number Diff line change @@ -11,13 +11,11 @@ module.exports = {
11
11
'storybook-dark-mode' ,
12
12
'./custom-addons/provider/register' ,
13
13
'./custom-addons/descriptions/register' ,
14
- './custom-addons/theme/register'
14
+ './custom-addons/theme/register' ,
15
+ './custom-addons/strictmode/register'
15
16
] ,
16
17
typescript : {
17
18
check : false ,
18
19
reactDocgen : false
19
- } ,
20
- reactOptions : {
21
- strictMode : process . env . STRICT_MODE
22
- } ,
20
+ }
23
21
} ;
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import {configureActions} from '@storybook/addon-actions';
2
2
import React from 'react' ;
3
3
import { VerticalCenter } from './layout' ;
4
4
import { withProviderSwitcher } from './custom-addons/provider' ;
5
+ import { withStrictModeSwitcher } from './custom-addons/strictmode' ;
5
6
6
7
// decorator order matters, the last one will be the outer most
7
8
@@ -29,5 +30,6 @@ export const decorators = [
29
30
< Story />
30
31
</ VerticalCenter >
31
32
) ,
33
+ withStrictModeSwitcher ,
32
34
withProviderSwitcher
33
35
] ;
Original file line number Diff line number Diff line change 12
12
"install-16" : " yarn add -W react@^16.8.0 react-dom@^16.8.0 @testing-library/react@^12 @testing-library/react-hooks@^8" ,
13
13
"install-17" : " yarn add -W react@^17 react-dom@^17 @testing-library/react@^12 @testing-library/react-hooks@^8" ,
14
14
"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'" ,
16
15
"build:storybook" : " build-storybook -c .storybook -o dist/$(git rev-parse HEAD)/storybook" ,
17
16
"build:storybook-16" : " build-storybook -c .storybook -o dist/$(git rev-parse HEAD)/storybook-16" ,
18
17
"build:storybook-17" : " build-storybook -c .storybook -o dist/$(git rev-parse HEAD)/storybook-17" ,
You can’t perform that action at this time.
0 commit comments