-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
Implement preview hooks #6916
Merged
Merged
Implement preview hooks #6916
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
15f120d
Implement preview hooks
Hypnosphi 2c114ed
Replace REGISTER_SUBSCRIPTION with useEffect hook
Hypnosphi 55c714f
Allow using hooks after calling story function
Hypnosphi 88c2a98
Merge branch 'next' into preview-hooks
ndelangen a170f18
ADD a devtool for decorator addon using useEffect
ndelangen b4d2fb6
WIP
ndelangen 3d138f2
Merge branch 'next' into preview-hooks
ndelangen 39f723e
ADD useChannel & useParameter
ndelangen 532df1e
Fix useChannel tests
Hypnosphi b07a636
Fix useChannel tests
Hypnosphi 6cca1bb
Fix HTML stories
Hypnosphi e467287
Fix Lint errors
Hypnosphi aab38d2
Merge branch 'next' into preview-hooks
ndelangen 2f6fad1
FIX snapshots
ndelangen f7fb7e5
ADD a proper readme to devkit-decorator
ndelangen 7955e4d
FIX channel emit failing because it's missing `this` && ADD roundtrip…
ndelangen 6e17b8d
ADD deps array to ueChannel
ndelangen c8d437b
Merge remote-tracking branch 'origin/next' into preview-hooks
Hypnosphi 8c75a1b
Add useStoryContext
Hypnosphi 582b29c
Add inline docs
Hypnosphi 9571fe1
Merge branch 'next' into preview-hooks
ndelangen da40913
FIX remaining issues after merging in next
ndelangen ae2911b
FIX linting
ndelangen 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 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,6 +1,7 @@ | ||
node_modules | ||
*.log | ||
.idea | ||
*.iml | ||
.vscode | ||
*.sw* | ||
npm-shrinkwrap.json | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// TODO remove in 6.0 | ||
import addons from '@storybook/addons'; | ||
import CoreEvents from '@storybook/core-events'; | ||
import deprecate from 'util-deprecate'; | ||
|
||
import { EVENTS } from './constants'; | ||
|
||
let prevEvents; | ||
let currentEmit; | ||
|
||
const onEmit = event => { | ||
currentEmit(event.name, event.payload); | ||
}; | ||
|
||
const subscription = () => { | ||
const channel = addons.getChannel(); | ||
channel.on(EVENTS.EMIT, onEmit); | ||
return () => { | ||
prevEvents = null; | ||
addons.getChannel().emit(EVENTS.ADD, []); | ||
channel.removeListener(EVENTS.EMIT, onEmit); | ||
}; | ||
}; | ||
|
||
const addEvents = ({ emit, events }) => { | ||
if (prevEvents !== events) { | ||
addons.getChannel().emit(EVENTS.ADD, events); | ||
prevEvents = events; | ||
} | ||
currentEmit = emit; | ||
addons.getChannel().emit(CoreEvents.REGISTER_SUBSCRIPTION, subscription); | ||
}; | ||
|
||
export const WithEvents = deprecate(({ children, ...options }) => { | ||
addEvents(options); | ||
return children; | ||
}, `<WithEvents> usage is deprecated, use .addDecorator(withEvents({emit, events})) instead`); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { logger } from '@storybook/client-logger'; | ||
|
||
// eslint-disable-next-line no-undef | ||
if (__DEV__) { | ||
console.log("import '@storybook/addon-ondevice-notes/register' to register the notes addon"); | ||
logger.log("import '@storybook/addon-ondevice-notes/register' to register the notes addon"); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# example addon: decorator | ||
|
||
This addon exposes a function users can use to wrap their storyFn with. This is called a decorator in storybook's terminology. | ||
|
||
A decorator can perform analysis on the storyFn, detect how long it takes to render, detect side-effects, and have access to the storyContext. | ||
|
||
It's also possible to wrap the results with some additional html/jsx or provide additional data to the storyFn. | ||
|
||
Uses include: | ||
- do side-effects detection | ||
- wrap story with additional html | ||
- add data to storyFn |
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,35 @@ | ||
{ | ||
"name": "@storybook/addon-decorator", | ||
"version": "5.2.0-alpha.40", | ||
"description": "decorator addon for storybook", | ||
"keywords": [ | ||
"devkit", | ||
"addon", | ||
"storybook", | ||
"decorator" | ||
], | ||
"homepage": "https://github.com/storybookjs/storybook/tree/master/addons/actions", | ||
"bugs": { | ||
"url": "https://github.com/storybookjs/storybook/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/storybookjs/storybook.git", | ||
"directory": "addons/actions" | ||
}, | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"prepare": "node ../../scripts/prepare.js" | ||
}, | ||
"dependencies": { | ||
"@storybook/addons": "5.2.0-alpha.40", | ||
"@storybook/client-api": "5.2.0-alpha.40", | ||
"global": "^3.0.1", | ||
"core-js": "^3.0.1" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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/decorator'; |
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,21 @@ | ||
/* eslint-disable no-console */ | ||
import { document } from 'global'; | ||
import { useEffect } from '@storybook/client-api'; | ||
|
||
const root = document && document.getElementById('root'); | ||
|
||
export const createDecorator = () => (options: any) => (storyFn: () => any) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ndelangen @Hypnosphi StoryContext? |
||
useEffect(() => { | ||
if (root != null) { | ||
console.log('story was rendered'); | ||
return () => { | ||
console.log('story was removed'); | ||
}; | ||
} | ||
return undefined; | ||
}, [root, options]); | ||
|
||
return storyFn(); | ||
}; | ||
|
||
export const withDecorator = createDecorator(); |
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,3 @@ | ||
// TODO: following packages need definition files or a TS migration | ||
declare module 'global'; | ||
declare module 'react-inspector'; |
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,13 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
"types": ["webpack-env"] | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
], | ||
"exclude": [ | ||
"src/__tests__/**/*" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
import { addons } from '@storybook/addons'; | ||
import { useChannel } from '@storybook/client-api'; | ||
import { EVENTS } from './constants'; | ||
|
||
const run = async () => { | ||
const channel = await addons.ready(); | ||
|
||
channel.on(EVENTS.REQUEST, () => { | ||
// do something in the preview context | ||
// then report back to the manager | ||
channel.emit(EVENTS.RESULT, ['from the preview']); | ||
export const withRoundtrip = (storyFn: () => any) => { | ||
const emit = useChannel({ | ||
[EVENTS.REQUEST]: () => { | ||
emit(EVENTS.RESULT, ['from the preview']); | ||
}, | ||
}); | ||
return storyFn(); | ||
}; | ||
|
||
run(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
|
||
import { withDecorator } from '@storybook/addon-decorator'; | ||
import { withRoundtrip } from '@storybook/addon-roundtrip'; | ||
|
||
storiesOf('addons|decorator', module) | ||
.addDecorator(withRoundtrip) | ||
.addDecorator(withDecorator()) | ||
.add('with decorator', () => ( | ||
<div>This story is wrapped with a decorator that is aware of when a story updates/changes</div> | ||
)); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! This should shave off quite some bundlesize