-
Notifications
You must be signed in to change notification settings - Fork 2.9k
chore(react-examples): replace storybook deprecated api with static stories #32074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dmytrokirpa
merged 10 commits into
microsoft:master
from
dmytrokirpa:react-examples-stories
Jul 25, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
543b4f7
chore(react-examples): replace storybook deprecated api with static s…
dmytrokirpa f6ed97b
Merge branch 'master' into react-examples-stories
dmytrokirpa b258bff
Merge branch 'master' into react-examples-stories
dmytrokirpa e51268f
fix format issues
dmytrokirpa 5092dc2
Merge branch 'master' into react-examples-stories
dmytrokirpa a6d5e83
Merge branch 'master' into react-examples-stories
dmytrokirpa 3e4a82c
Merge branch 'master' into react-examples-stories
dmytrokirpa 454daf2
Merge branch 'master' into react-examples-stories
dmytrokirpa a2fc012
fix duplicated story title
dmytrokirpa a23e309
fix stories detection
dmytrokirpa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,125 +1,14 @@ | ||
| import * as React from 'react'; | ||
| import { initializeIcons } from '@fluentui/font-icons-mdl2'; | ||
| import { configure, addParameters, addDecorator } from '@storybook/react'; | ||
| import 'cypress-storybook/react'; | ||
| import { withPerformance } from 'storybook-addon-performance'; | ||
| import { withKeytipLayer, withStrictMode } from '@fluentui/storybook'; | ||
|
|
||
| initializeIcons(); | ||
|
|
||
| // This API is deprecated (in favor of `export const decorators = []`) but the new way appears not | ||
| // to work when using the legacy configure() API | ||
| addDecorator(withPerformance); | ||
| addDecorator(withStrictMode); | ||
| addDecorator(withKeytipLayer); | ||
| export const decorators = [withPerformance, withStrictMode, withKeytipLayer]; | ||
|
|
||
| // This API is deprecated (in favor of `export const parameters = {}`) but same issue as above | ||
| addParameters({ | ||
| export const parameters = { | ||
| a11y: /** @type {import('@storybook/addon-a11y').A11yParameters} */ ({ | ||
| manual: true, | ||
| }), | ||
| }); | ||
|
|
||
| // This API is deprecated and will no longer work in storybook 7 or with storyStoreV7. | ||
| // https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-configure | ||
| // The selective loading functionality currently implemented by preview-loader can be more easily | ||
| // handled by just settings `stories: [/*glob*/]` in main.js, but it's unclear if a replacement is | ||
| // available for transforming files before loading as stories. | ||
| configure(loadStories, module); | ||
|
|
||
| // ========================================================== | ||
| // Helpers for loading examples in component story format | ||
| // ========================================================== | ||
|
|
||
| /** | ||
| * @typedef {{ | ||
| * default: { title: string }; | ||
| * [storyName: string]: React.FunctionComponent | { title: string }; | ||
| * }} StoryGroup - Group of stories for a particular component (follows component story format) | ||
| * | ||
| * @typedef {{ | ||
| * [exportName: string]: React.ComponentType; | ||
| * }} ExampleModule - A loaded `*.Example.tsx` or `*.stories.tsx` file, exporting an example component | ||
| */ | ||
|
|
||
| /** | ||
| * Read the `*.Example.tsx` and/or `*.stories.tsx` files for the current package `start-storybook` | ||
| * is running in and generate a list of stories in component story format. | ||
| */ | ||
| function loadStories() { | ||
| /** @type {Map<string, StoryGroup>} */ | ||
| const stories = new Map(); | ||
|
|
||
| /** @type {__WebpackModuleApi.RequireContext[]} */ | ||
| const contexts = [ | ||
| // This will be updated by preview-loader with the actual current package name | ||
| require.context('../src/PACKAGE_NAME', true, /\.(Example|stories)\.tsx$/), | ||
| ]; | ||
|
|
||
| // This will be replaced with the actual package name by the custom webpack loader ./preview-loader.js | ||
| // (note: for some reason the replacement doesn't work if the const is outside the function) | ||
| const packageNamePlaceholder = /** @type {string} */ ('PACKAGE_NAME'); | ||
|
|
||
| if (packageNamePlaceholder === 'react') { | ||
| // For @fluentui/react's storybook, also include examples from @fluentui/react-focus | ||
| contexts.push(require.context('../src/react-focus', true, /\.(Example|stories)\.tsx$/)); | ||
| } | ||
|
|
||
| for (const req of contexts) { | ||
| req.keys().forEach(key => { | ||
| generateStoriesForExampleFile(key, stories, req); | ||
| }); | ||
| } | ||
|
|
||
| // convert stories Set to array | ||
| const sorted = [...stories.values()].sort((s1, s2) => (s1.default.title > s2.default.title ? 1 : -1)); | ||
|
|
||
| return sorted; | ||
| } | ||
|
|
||
| /** | ||
| * Generate stories (usually one story) for a `*.Example.tsx` or `*.stories.tsx` file | ||
| * @param {string} key - key for the module in require.context (the relative path to the module | ||
| * from the root path passed to require.context) | ||
| * @param {Map<string, StoryGroup>} stories - mapping from component path (`Components/<ComponentName>`) to stories | ||
| * @param {__WebpackModuleApi.RequireContext} req | ||
| */ | ||
| function generateStoriesForExampleFile(key, stories, req) { | ||
| // The key will be like this: | ||
| // ./ComponentName/ComponentName.Something.Example.tsx | ||
| // group 1: component name | ||
| const pathParts = key.match(/\/(\w+)\/[\w.]+$/); | ||
| if (!pathParts) { | ||
| console.error(`Invalid path found in storybook require.context: "${key}"`); | ||
| return; | ||
| } | ||
|
|
||
| const componentName = pathParts[1]; | ||
| const componentPath = `Components/${componentName}`; | ||
|
|
||
| if (!stories.has(componentPath)) { | ||
| stories.set(componentPath, { | ||
| // A default "export" with a title is required by component story format | ||
| default: { | ||
| title: componentPath, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| const storyGroup = /** @type {StoryGroup} */ (stories.get(componentPath)); | ||
| const exampleModule = /** @type {(key: string) => ExampleModule} */ (req)(key); | ||
|
|
||
| for (const [exportName, ExampleComponent] of Object.entries(exampleModule)) { | ||
| const storyName = exportName.replace(componentName, '').replace(/Example$/, ''); | ||
|
|
||
| if (typeof ExampleComponent === 'function') { | ||
| if (ExampleComponent.prototype.render) { | ||
| // class component -- make a wrapper function component | ||
| storyGroup[storyName] = () => React.createElement(ExampleComponent); | ||
| } else { | ||
| // function component | ||
| storyGroup[storyName] = /** @type {React.FunctionComponent} */ (ExampleComponent); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...ct-examples/src/azure-themes/stories/components/CalendarInlineMultidayDayView.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/react-examples/src/azure-themes/stories/components/ProgressIndicator.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/react-examples/src/azure-themes/stories/components/activityitem.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/react-examples/src/azure-themes/stories/components/choicegroup.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.