-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
3,086 additions
and
388 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@modern-js/storybook': patch | ||
'@modern-js/storybook-builder': patch | ||
--- | ||
|
||
feat: add storybook-framework | ||
feat: 支持 storybook-framework |
This file contains 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 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,3 +1,4 @@ | ||
export * from './createLoadedConfig'; | ||
export * from './createResolvedConfig'; | ||
export * from './createDefaultConfig'; | ||
export { loadConfig } from './loadConfig'; |
This file contains 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 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 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 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 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
56 changes: 56 additions & 0 deletions
56
packages/storybook/builder/src/addons/components/modern.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react'; | ||
import { createApp } from '@modern-js/runtime'; | ||
import type { Plugin, RouterConfig } from '@modern-js/runtime'; | ||
import router from '@modern-js/runtime/router'; | ||
import state from '@modern-js/runtime/model'; | ||
import type { StoryFn as StoryFunction } from '@storybook/addons'; | ||
import type { IConfig } from '../type'; | ||
|
||
export const WrapProviders = ( | ||
storyFn: StoryFunction<JSX.Element>, | ||
config: IConfig, | ||
) => { | ||
const App = createApp({ | ||
plugins: resolvePlugins(config.modernConfigRuntime), | ||
})(storyFn); | ||
|
||
return <App />; | ||
}; | ||
|
||
const allowedRuntimeAPI = { | ||
router: 'router', | ||
state: 'state', | ||
}; | ||
const allowedRuntimeAPIValues = Object.values(allowedRuntimeAPI); | ||
|
||
export const resolvePlugins = (runtime: IConfig['modernConfigRuntime']) => { | ||
const plugins: Plugin[] = []; | ||
|
||
if (!runtime) { | ||
return plugins; | ||
} | ||
|
||
Object.keys(runtime).forEach(api => { | ||
if (allowedRuntimeAPIValues.includes(api)) { | ||
if (api === allowedRuntimeAPI.state) { | ||
if (typeof runtime.state === 'boolean') { | ||
if (runtime.state) { | ||
plugins.push(state({})); | ||
} | ||
} else if (typeof runtime.state === 'object') { | ||
plugins.push(state(runtime.state)); | ||
} | ||
} else if (api === allowedRuntimeAPI.router) { | ||
// TODO: React Router v6 is not supported yet | ||
plugins.push( | ||
router({ | ||
...{ serverBase: ['/'] }, | ||
...(runtime.router as RouterConfig), | ||
}), | ||
); | ||
} | ||
} | ||
}); | ||
|
||
return plugins; | ||
}; |
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const ADDON_ID = 'storybook/modern-runtime'; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
declare const module: any; | ||
|
||
if (module?.hot?.decline) { | ||
module.hot.decline(); | ||
} | ||
|
||
// make it work with --isolatedModules | ||
export default {}; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// import { withPluginRuntime } from '../withPluginRuntime'; | ||
|
||
// FIXME: @modern-js/runtime now is ESM only, but this package | ||
// currently focus on CommonJS, so it cannot import @modern-js/runtime | ||
// directly. | ||
// export const decorators = [withPluginRuntime]; | ||
export const decorators = []; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface IConfig { | ||
modernConfigRuntime: any; | ||
modernConfigDesignToken: any; | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/storybook/builder/src/addons/withPluginRuntime.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { type StoryFn as StoryFunction, useParameter } from '@storybook/addons'; | ||
import { WrapProviders } from './components/modern'; | ||
import type { IConfig } from './type'; | ||
|
||
export const withPluginRuntime = ( | ||
storyFn: StoryFunction<JSX.Element>, | ||
// context: StoryContext | ||
) => { | ||
const modernConfigRuntime = useParameter<IConfig['modernConfigRuntime']>( | ||
'modernConfigRuntime', | ||
); | ||
const modernConfigDesignToken = useParameter< | ||
IConfig['modernConfigDesignToken'] | ||
>('modernConfigDesignToken'); | ||
|
||
return WrapProviders(storyFn, { | ||
modernConfigRuntime: modernConfigRuntime || {}, | ||
modernConfigDesignToken, | ||
}); | ||
}; |
Oops, something went wrong.