Skip to content

Commit

Permalink
Clean up logic for viewMode changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Jun 24, 2021
1 parent 007c239 commit 2d393aa
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/ui/src/components/preview/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,25 @@ const Preview = React.memo<PreviewProps>((props) => {
const { isToolshown } = options;

const previousStoryId = useRef(storyId);
const previousViewMode = useRef(viewMode);

useEffect(() => {
if (story && viewMode && viewMode.match(/docs|story/)) {
if (story && viewMode) {
// Don't emit the event on first ("real") render, only when story or mode changes
if (storyId !== previousStoryId.current) {
if (storyId !== previousStoryId.current || viewMode !== previousViewMode.current) {
previousStoryId.current = storyId;

const { refId, id } = story;
api.emit(SET_CURRENT_STORY, {
storyId: id,
viewMode,
options: {
target: refId ? `storybook-ref-${refId}` : 'storybook-preview-iframe',
},
});
previousViewMode.current = viewMode;

if (viewMode.match(/docs|story/)) {
const { refId, id } = story;
api.emit(SET_CURRENT_STORY, {
storyId: id,
viewMode,
options: {
target: refId ? `storybook-ref-${refId}` : 'storybook-preview-iframe',
},
});
}
}
}
}, [story, viewMode]);
Expand Down

0 comments on commit 2d393aa

Please sign in to comment.