Skip to content

Commit

Permalink
App React: Ensure that STORY_RENDERED is emitted only after calling s…
Browse files Browse the repository at this point in the history
…toryFn
  • Loading branch information
Hypnosphi committed Aug 17, 2019
1 parent 0f330e2 commit 12a30c7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
18 changes: 10 additions & 8 deletions app/react/src/client/preview/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import { RenderMainArgs } from './types';

const rootEl = document ? document.getElementById('root') : null;

function render(node: React.ReactElement, el: Element) {
ReactDOM.render(
process.env.STORYBOOK_EXAMPLE_APP ? <React.StrictMode>{node}</React.StrictMode> : node,
el
);
}
const render = (node: React.ReactElement, el: Element) =>
new Promise(resolve => {
ReactDOM.render(
process.env.STORYBOOK_EXAMPLE_APP ? <React.StrictMode>{node}</React.StrictMode> : node,
el,
resolve
);
});

export default function renderMain({
export default async function renderMain({
storyFn: StoryFn,
selectedKind,
selectedStory,
Expand Down Expand Up @@ -55,6 +57,6 @@ export default function renderMain({
ReactDOM.unmountComponentAtNode(rootEl);
}

render(element, rootEl);
await render(element, rootEl);
showMain();
}
25 changes: 25 additions & 0 deletions examples/official-storybook/stories/demo/button.stories.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import React, { useState } from 'react';
import { action } from '@storybook/addon-actions';
import { Button } from '@storybook/react/demo';
import { useChannel, useState as useStoryState } from '@storybook/client-api';

const withPropsInfo = storyFn => {
const [selected, setSelected] = useStoryState(true);

const emit = useChannel(
{
REQUEST_PROPS_CHANGE: () => setSelected(!selected),
},
[selected]
);

console.log('SELECTED:', selected);

return (
<div>
{storyFn()}
{selected && 'Article'}
<button type="button" onClick={() => emit('REQUEST_PROPS_CHANGE')}>
Toggle
</button>
</div>
);
};

export default {
title: 'Other|Demo/Button',
component: Button,
decorators: [withPropsInfo],
};

export const withText = () => <Button onClick={action('clicked')}>Hello Button</Button>;
Expand Down
6 changes: 4 additions & 2 deletions lib/core/src/client/preview/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ export default function start(render, { decorateStory } = {}) {
case 'story':
default: {
if (getDecorated) {
render(renderContext);
addons.getChannel().emit(Events.STORY_RENDERED, id);
(async () => {
await render(renderContext);
addons.getChannel().emit(Events.STORY_RENDERED, id);
})();
} else {
showNopreview();
addons.getChannel().emit(Events.STORY_MISSING, id);
Expand Down

0 comments on commit 12a30c7

Please sign in to comment.