@@ -5,6 +5,12 @@ import { withInfo } from '@storybook/addon-info';
5
5
import { withPerformance } from 'storybook-addon-performance' ;
6
6
import { withFluentProvider , withKeytipLayer , withStrictMode } from '@fluentui/storybook' ;
7
7
8
+ /**
9
+ * "PACKAGE_NAME" placeholder is being replaced by webpack loader - @link {./preview.loader}
10
+ * @type {string }
11
+ */
12
+ const packageNamePlaceholder = 'PACKAGE_NAME' ;
13
+
8
14
addDecorator ( withInfo ) ;
9
15
addDecorator ( withPerformance ) ;
10
16
addDecorator ( withKeytipLayer ) ;
@@ -36,7 +42,7 @@ export const parameters = {
36
42
* NOTE:
37
43
* - this is a temporary workaround until we migrate to new storybook 6 APIs -> old `addDecorator` duplicates rendered decorators
38
44
* - source of this function is interpolated during runtime with webpack
39
- * - "PACKAGE_NAME" placeholder is being replaced
45
+ *
40
46
*/
41
47
function addCustomDecorators ( ) {
42
48
/**
@@ -46,7 +52,7 @@ function addCustomDecorators() {
46
52
47
53
if (
48
54
[ 'react-button' , 'react-cards' , 'react-checkbox' , 'react-slider' , 'react-tabs' , 'react-toggle' ] . includes (
49
- 'PACKAGE_NAME' ,
55
+ packageNamePlaceholder ,
50
56
)
51
57
) {
52
58
initializeIcons ( ) ;
@@ -66,7 +72,7 @@ function addCustomDecorators() {
66
72
'react-text' ,
67
73
'react-components' ,
68
74
'react-portal' ,
69
- ] . includes ( 'PACKAGE_NAME' )
75
+ ] . includes ( packageNamePlaceholder )
70
76
) {
71
77
customDecorators . add ( withFluentProvider ) . add ( withStrictMode ) ;
72
78
}
@@ -109,15 +115,22 @@ function loadStories() {
109
115
require . context ( '../src/PACKAGE_NAME' , true , / \. ( E x a m p l e | s t o r i e s ) \. t s x $ / ) ,
110
116
] ;
111
117
112
- // @ts -ignore -- PACKAGE_NAME is replaced by a loader
113
- if ( 'PACKAGE_NAME' === 'react' || 'PACKAGE_NAME' === 'react-components' ) {
118
+ if ( packageNamePlaceholder === 'react' || packageNamePlaceholder === 'react-components' ) {
114
119
// For suite package storybooks, also show the examples of re-exported component packages.
115
120
// preview-loader will replace REACT_ DEPS with the actual list.
116
121
contexts . push (
117
122
require . context ( '../src' , true , / ( R E A C T _ D E P S | P A C K A G E _ N A M E ) \/ \w + \/ [ \w . ] + \. ( E x a m p l e | s t o r i e s ) \. ( t s x | m d x ) $ / ) ,
118
123
) ;
119
124
}
120
125
126
+ // @TODO
127
+ // - this is a temporary solution until all converged packages use new storybook configuration
128
+ // - after new config is in place remove this whole IF
129
+ if ( packageNamePlaceholder === 'react-components' ) {
130
+ // include package collocated stories within react-components
131
+ contexts . push ( require . context ( '../../' , true , / ( R E A C T _ D E P S ) \/ s r c \/ [ \w . / ] + \. ( E x a m p l e | s t o r i e s ) \. ( t s x | m d x ) $ / ) ) ;
132
+ }
133
+
121
134
for ( const req of contexts ) {
122
135
req . keys ( ) . forEach ( key => {
123
136
generateStoriesFromExamples ( key , stories , req ) ;
@@ -150,8 +163,11 @@ function generateStoriesFromExamples(key, stories, req) {
150
163
// Depending on the starting point of the context, and the package layout, the key will be like one of these:
151
164
// ./ComponentName/ComponentName.Something.Example.tsx
152
165
// ./package-name/ComponentName/ComponentName.Something.Example.tsx
166
+ // ./package-name/src/.../ComponentName.stories.tsx - @TODO remove this line after new storybook setup has been applied for all converged packages
153
167
const segments = key . split ( '/' ) ;
168
+
154
169
if ( segments . length < 3 ) {
170
+ console . warn ( `Invalid storybook context location found: key: ${ key } | segments: ${ segments } ` ) ;
155
171
return ;
156
172
}
157
173
@@ -161,28 +177,7 @@ function generateStoriesFromExamples(key, stories, req) {
161
177
return ;
162
178
}
163
179
164
- /** @type {string } */
165
- let componentName ;
166
-
167
- // Story URLs are generated based off the story name
168
- // In the case of `react-components` a (package name) suffix is added to each story
169
- // This results in a difference name and URL between individual storybooks and the react-components suite storyboo
170
- // https://storybook.js.org/docs/react/configure/sidebar-and-urls#permalinking-to-stories
171
- // Use the id property in stories to ensure the same URL between individual and suite storyboo
172
- /** @type {string } */
173
- let componentId ;
174
-
175
- if ( segments . length === 3 ) {
176
- // ./ComponentName/ComponentName.Something.Example.tsx
177
- componentName = segments [ 1 ] ;
178
- componentId = segments [ 1 ] ;
179
- } else {
180
- // ./package-name/ComponentName/ComponentName.Something.Example.tsx
181
- // For @fluentui /react, don't include the package name in the sidebar
182
- // @ts -ignore -- PACKAGE_NAME is replaced by a loader
183
- componentName = 'PACKAGE_NAME' === 'react' ? segments [ 2 ] : `${ segments [ 2 ] } (${ segments [ 1 ] } )` ;
184
- componentId = segments [ 2 ] ;
185
- }
180
+ const { componentName, componentId } = generateComponentName ( segments ) ;
186
181
187
182
if ( ! stories . has ( componentName ) ) {
188
183
stories . set ( componentName , {
@@ -220,4 +215,79 @@ function generateStoriesFromExamples(key, stories, req) {
220
215
}
221
216
}
222
217
}
218
+
219
+ /**
220
+ *
221
+ * @param {string[] } segments
222
+ * @returns {{componentName:string; componentId:string} }
223
+ */
224
+ function generateComponentName ( segments ) {
225
+ /**
226
+ * @TODO
227
+ * - this is a temporary solution until all converged packages use new storybook configuration
228
+ * - after new config is in place remove this
229
+ *
230
+ * ./<package-name>/src/.../ComponentName.Something.Example.tsx
231
+ */
232
+ const isCollocatedStory = segments . includes ( 'src' ) ;
233
+
234
+ /**
235
+ * ./ComponentName/ComponentName.Something.Example.tsx
236
+ */
237
+ const isReactExamplesStory = segments . length === 3 ;
238
+
239
+ /**
240
+ * For @fluentui/react, don't include the package name in the sidebar
241
+ * ./package-name/ComponentName/ComponentName.Something.Example.tsx
242
+ */
243
+ // @ts -ignore -- PACKAGE_NAME is replaced by a loader
244
+ const isReactPackageStory = 'PACKAGE_NAME' === 'react' ;
245
+
246
+ // @TODO
247
+ // - this is a temporary solution until all converged packages use new storybook configuration
248
+ // - after new config is in place remove this whole IF
249
+ if ( isCollocatedStory ) {
250
+ // ./<package-name>/src/.../ComponentName.Something.Example.tsx
251
+ // ↓↓↓
252
+ // [., <package-name>, src, ..., ComponentName, ComponentName.Something.Example.tsx]
253
+ const packageName = segments [ 1 ] ;
254
+ const storyFileName = segments [ segments . length - 1 ] ;
255
+ const [ , storyName ] = / ( \w + ) \. ( E x a m p l e | s t o r i e s ) \. ( t s x | m d x ) $ / . exec ( storyFileName ) || [ ] ;
256
+
257
+ const componentName = `${ storyName } (${ packageName } )` ;
258
+ const componentId = storyName ;
259
+
260
+ return { componentName, componentId } ;
261
+ }
262
+
263
+ if ( isReactExamplesStory ) {
264
+ // ./ComponentName/ComponentName.Something.Example.tsx
265
+ // ↓↓↓
266
+ // [., ComponentName, ComponentName.Something.Example.tsx]
267
+ const componentName = segments [ 1 ] ;
268
+ const componentId = segments [ 1 ] ;
269
+
270
+ return { componentName, componentId } ;
271
+ }
272
+
273
+ if ( isReactPackageStory ) {
274
+ // ./package-name/ComponentName/ComponentName.Something.Example.tsx
275
+ // ↓↓↓
276
+ // [., <package-name>, ComponentName, ComponentName.Something.Example.tsx]
277
+ const componentName = segments [ 1 ] ;
278
+ const componentId = segments [ 2 ] ;
279
+
280
+ return { componentName, componentId } ;
281
+ }
282
+
283
+ return {
284
+ componentName : `${ segments [ 2 ] } (${ segments [ 1 ] } )` ,
285
+ // Story URLs are generated based off the story name
286
+ // In the case of `react-components` a (package name) suffix is added to each story
287
+ // This results in a difference name and URL between individual storybooks and the react-components suite storybook
288
+ // https://storybook.js.org/docs/react/configure/sidebar-and-urls#permalinking-to-stories
289
+ // Use the id property in stories to ensure the same URL between individual and suite storybook
290
+ componentId : segments [ 2 ] ,
291
+ } ;
292
+ }
223
293
}
0 commit comments