-
-
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
Implement preview hooks #6916
Conversation
This pull request is automatically deployed with Now. Latest deployment for this branch: https://monorepo-git-preview-hooks.storybook.now.sh |
Amazing @Hypnosphi |
# Conflicts: # addons/actions/package.json # addons/events/package.json # examples/html-kitchen-sink/package.json
Do you think we can create hooks like: useContext, useParameter & useChannel? The code needs documentation. I think this low level code is easy to forget how exactly it works. It feels to me, code comments (maybe jsdoc?) in the code would be great. |
By context, do you mean the story context? I think we can. I would prefer some name like What should |
@ndelangen I will mostly be far from computer in the following days so feel free to add whatever makes sense to you |
useChannelimport { STORY_CHANGED } from '@storybook/core-events';
export const Panel = () => {
const emit = useChannel({
STORY_CHANGED: (...args) => console.log(...args),
});
return (
<button onClick={() => emit('my-event-type', { some: 'data' })}>
clicking this will emit an event
</button>
);
} Allows for both setting subscriptions to events and getting the emitter for emitting custom event unto the channel. The messages can be listened for on both the iframe and the manager side. |
Here's how it's implemented in the lib/api: export const useChannel = (eventMap: EventMap) => {
const api = useStorybookApi();
useEffect(() => {
Object.entries(eventMap).forEach(([type, listener]) => api.on(type, listener));
return () => {
Object.entries(eventMap).forEach(([type, listener]) => api.off(type, listener));
};
});
return api.emit;
}; |
@shilman are there any blockers left here? |
# Conflicts: # addons/actions/package.json # addons/events/package.json # addons/events/src/index.ts # addons/ondevice-notes/package.json # dev-kits/addon-roundtrip/package.json # examples/dev-kits/package.json # examples/html-kitchen-sink/package.json # lib/client-api/package.json # lib/client-api/src/client_api.ts # lib/client-api/src/typings.d.ts # lib/client-api/tsconfig.json # lib/ui/src/index.js # yarn.lock
I think not, I'd love to merge this and start using it in devkits & addons! I'm pushing for this to make it into 5.2 |
@@ -1,14 +1,9 @@ | |||
// Based on http://backbonejs.org/docs/backbone.html#section-164 | |||
import { document, Element } from 'global'; | |||
import { isEqual } from 'lodash'; |
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
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
@ndelangen @Hypnosphi StoryContext?
@ndelangen @Hypnosphi I'd like to see examples & documentation before we release, but aside from the comment (question) above, I'm fine to merge this. |
@shilman I promise that I'll document this more during the beta. |
This PR adds a framework-agnostic implementation of API similar to React Hooks
Here, the story render lifecycle is an analogue of React render lifecycle, while decorators and story functions are "components".
The main purpose is simplifying addon development. E.g.
useEffect
serves as a replacement forREGISTER_SUBSCRIPTION
event.