Skip to content
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

UI: Fix navigation after no story error #15349

Merged
merged 4 commits into from Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 0 additions & 76 deletions lib/csf-tools/__testfixtures__/mdx/component-args.output.snapshot
Expand Up @@ -75,79 +75,3 @@ componentMeta.parameters.docs = {
export default componentMeta;
"
`;

exports[`docs-mdx-compiler-plugin fixtures component-args 1`] = `
"/* @jsxRuntime classic */
/* @jsx mdx */
import { assertIsFn, AddContext } from '@storybook/addon-docs';

import { Button } from '@storybook/react/demo';
import { Story, Meta } from '@storybook/addon-docs';

const layoutProps = {};
const MDXLayout = 'wrapper';
function MDXContent({ components, ...props }) {
return (
<MDXLayout {...layoutProps} {...props} components={components} mdxType=\\"MDXLayout\\">
<Meta
title=\\"Button\\"
args={{
a: 1,
b: 2,
}}
argTypes={{
a: {
name: 'A',
},
b: {
name: 'B',
},
}}
mdxType=\\"Meta\\"
/>
<h1>{\`Args\`}</h1>
<Story name=\\"component notes\\" mdxType=\\"Story\\">
<Button mdxType=\\"Button\\">Component notes</Button>
</Story>
</MDXLayout>
);
}

MDXContent.isMDXComponent = true;

export const componentNotes = () => <Button>Component notes</Button>;
componentNotes.storyName = 'component notes';
componentNotes.parameters = { storySource: { source: '<Button>Component notes</Button>' } };

const componentMeta = {
title: 'Button',
args: {
a: 1,
b: 2,
},
argTypes: {
a: {
name: 'A',
},
b: {
name: 'B',
},
},
includeStories: ['componentNotes'],
};

const mdxStoryNameToKey = { 'component notes': 'componentNotes' };

componentMeta.parameters = componentMeta.parameters || {};
componentMeta.parameters.docs = {
...(componentMeta.parameters.docs || {}),
page: () => (
<AddContext mdxStoryNameToKey={mdxStoryNameToKey} mdxComponentMeta={componentMeta}>
<MDXContent />
</AddContext>
),
};

export default componentMeta;
"
`;
50 changes: 0 additions & 50 deletions lib/csf-tools/__testfixtures__/mdx/component-id.output.snapshot
@@ -1,55 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`docs-mdx-compiler-plugin component-id 1`] = `
"/* @jsxRuntime classic */
/* @jsx mdx */
import { assertIsFn, AddContext } from '@storybook/addon-docs';

import { Button } from '@storybook/react/demo';
import { Story, Meta } from '@storybook/addon-docs';

const layoutProps = {};
const MDXLayout = 'wrapper';
function MDXContent({ components, ...props }) {
return (
<MDXLayout {...layoutProps} {...props} components={components} mdxType=\\"MDXLayout\\">
<Meta title=\\"Button\\" component={Button} id=\\"button-id\\" mdxType=\\"Meta\\" />
<Story name=\\"component notes\\" mdxType=\\"Story\\">
<Button mdxType=\\"Button\\">Component notes</Button>
</Story>
</MDXLayout>
);
}

MDXContent.isMDXComponent = true;

export const componentNotes = () => <Button>Component notes</Button>;
componentNotes.storyName = 'component notes';
componentNotes.parameters = { storySource: { source: '<Button>Component notes</Button>' } };

const componentMeta = {
title: 'Button',
id: 'button-id',
component: Button,
includeStories: ['componentNotes'],
};

const mdxStoryNameToKey = { 'component notes': 'componentNotes' };

componentMeta.parameters = componentMeta.parameters || {};
componentMeta.parameters.docs = {
...(componentMeta.parameters.docs || {}),
page: () => (
<AddContext mdxStoryNameToKey={mdxStoryNameToKey} mdxComponentMeta={componentMeta}>
<MDXContent />
</AddContext>
),
};

export default componentMeta;
"
`;

exports[`docs-mdx-compiler-plugin component-id.mdx 1`] = `
"/* @jsxRuntime classic */
/* @jsx mdx */
Expand Down
37 changes: 20 additions & 17 deletions lib/ui/src/components/preview/preview.tsx
Expand Up @@ -136,6 +136,7 @@ const Preview = React.memo<PreviewProps>((props) => {
id: previewId,
options,
viewMode,
storyId,
story = undefined,
description,
baseUrl,
Expand All @@ -148,25 +149,27 @@ const Preview = React.memo<PreviewProps>((props) => {
const shouldScale = viewMode === 'story';
const { isToolshown } = options;

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

useEffect(() => {
// Don't emit the event on first ("real") render, only when story or mode changes
if (initialRender.current) {
// We initially render without a story set, which isn't all that interesting, let's ignore
if (story) {
initialRender.current = false;
if (story && viewMode) {
// Don't emit the event on first ("real") render, only when story or mode changes
if (storyId !== previousStoryId.current || viewMode !== previousViewMode.current) {
previousStoryId.current = storyId;
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',
},
});
}
}
return;
}
if (story && viewMode && 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
1 change: 1 addition & 0 deletions lib/ui/src/components/preview/utils/types.tsx
Expand Up @@ -7,6 +7,7 @@ export interface PreviewProps {
api: API;
viewMode: ViewMode;
refs: State['refs'];
storyId: Story['id'];
story: Group | Story;
docsOnly: boolean;
options: {
Expand Down
1 change: 1 addition & 0 deletions lib/ui/src/containers/preview.tsx
Expand Up @@ -41,6 +41,7 @@ const mapper = ({ api, state }: Combo) => {
viewMode,
path,
refs,
storyId,
baseUrl: PREVIEW_URL || 'iframe.html',
queryParams: customQueryParams,
docsOnly,
Expand Down