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

Core: Retain iframe.html query parameters #17136

Merged
merged 4 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/components/src/blocks/Story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IFrame } from './IFrame';
import { EmptyBlock } from './EmptyBlock';
import { ZoomContext } from './ZoomContext';
import { Loader } from '..';
import { getStoryHref } from '../../../ui/src/components/preview/utils/getStoryHref';
ittaibaratz marked this conversation as resolved.
Show resolved Hide resolved

const { PREVIEW_URL } = global;
const BASE_URL = PREVIEW_URL || 'iframe.html';
Expand Down Expand Up @@ -56,7 +57,7 @@ const IFrameStory: FunctionComponent<IFrameStoryProps> = ({ id, title, height =
key="iframe"
id={`iframe--${id}`}
title={title}
src={`${BASE_URL}?id=${id}&viewMode=story`}
src={getStoryHref(BASE_URL, id, { viewMode: 'story' })}
allowFullScreen
scale={scale}
style={{
Expand Down
4 changes: 3 additions & 1 deletion lib/components/src/blocks/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { FlexBar } from '../bar/bar';
import { Icons } from '../icon/icon';
import { IconButton, IconButtonSkeleton } from '../bar/button';

import { getStoryHref } from '../../../ui/src/components/preview/utils/getStoryHref';

interface ZoomProps {
zoom: (val: number) => void;
resetZoom: () => void;
Expand Down Expand Up @@ -63,7 +65,7 @@ const Zoom: FunctionComponent<ZoomProps> = ({ zoom, resetZoom }) => (
const Eject: FunctionComponent<EjectProps> = ({ baseUrl, storyId }) => (
<IconButton
key="opener"
href={`${baseUrl}?id=${storyId}`}
href={getStoryHref(baseUrl, storyId)}
target="_blank"
title="Open canvas in new tab"
>
Expand Down
7 changes: 6 additions & 1 deletion lib/ui/src/components/preview/FramesRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Global, CSSObject, styled } from '@storybook/theming';
import { IFrame } from './iframe';
import { FramesRendererProps } from './utils/types';
import { stringifyQueryParams } from './utils/stringifyQueryParams';
import { getStoryHref } from './utils/getStoryHref';

const getActive = (refId: FramesRendererProps['refId']) => {
if (refId) {
Expand Down Expand Up @@ -66,7 +67,11 @@ export const FramesRenderer: FunctionComponent<FramesRendererProps> = ({
}, []);

const [frames, setFrames] = useState<Record<string, string>>({
'storybook-preview-iframe': `${baseUrl}?id=${storyId}&viewMode=${viewMode}${stringifiedQueryParams}`,
'storybook-preview-iframe': getStoryHref(baseUrl, storyId, {
...queryParams,
...(version && { version }),
viewMode,
}),
});

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/src/components/preview/tools/copy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import copy from 'copy-to-clipboard';
import { IconButton, Icons } from '@storybook/components';
import { Consumer, Combo } from '@storybook/api';
import { Addon } from '@storybook/addons';
import { stringifyQueryParams } from '../utils/stringifyQueryParams';
import { getStoryHref } from '../utils/getStoryHref';

const { PREVIEW_URL } = global;

Expand All @@ -30,7 +30,7 @@ export const copyTool: Addon = {
storyId ? (
<IconButton
key="copy"
onClick={() => copy(`${baseUrl}?id=${storyId}${stringifyQueryParams(queryParams)}`)}
onClick={() => copy(getStoryHref(baseUrl, storyId, queryParams))}
title="Copy canvas link"
>
<Icons icon="link" />
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/src/components/preview/tools/eject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { IconButton, Icons } from '@storybook/components';
import { Consumer, Combo } from '@storybook/api';
import { Addon } from '@storybook/addons';
import { stringifyQueryParams } from '../utils/stringifyQueryParams';
import { getStoryHref } from '../utils/getStoryHref';

const { PREVIEW_URL } = global;

Expand All @@ -29,7 +29,7 @@ export const ejectTool: Addon = {
storyId ? (
<IconButton
key="opener"
href={`${baseUrl}?id=${storyId}${stringifyQueryParams(queryParams)}`}
href={getStoryHref(baseUrl, storyId, queryParams)}
target="_blank"
title="Open canvas in new tab"
>
Expand Down
20 changes: 20 additions & 0 deletions lib/ui/src/components/preview/utils/getStoryHref.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import qs from 'qs';

export const getStoryHref = (
baseUrl: string,
storyId: string,
additionalParams: Record<string, string> = {}
) => {
const [url, paramsStr] = baseUrl.split('?');
const params = paramsStr
? {
...qs.parse(paramsStr),
...additionalParams,
id: storyId,
}
: {
...additionalParams,
id: storyId,
};
return `${url}${qs.stringify(params, { addQueryPrefix: true, encode: false })}`;
};