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

FIX the customCanvas option #6968

Merged
merged 1 commit into from
Jun 4, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/react-native-server/src/client/manager/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const mapper = ({ state, api }) => ({
api,
storiesHash: state.storiesHash,
storyId: state.storyId,
viewMode: state.viewMode,
});

export default class ReactProvider extends Provider {
Expand Down Expand Up @@ -48,12 +49,12 @@ export default class ReactProvider extends Provider {
renderPreview() {
return (
<Consumer filter={mapper} pure>
{({ storiesHash, storyId, api }) => {
{({ storiesHash, storyId, api, viewMode }) => {
if (storiesHash[storyId]) {
const { kind, story } = storiesHash[storyId];
api.emit(Events.SET_CURRENT_STORY, { kind, story });
}
return <PreviewHelp />;
return viewMode === 'story' ? <PreviewHelp /> : null;
}}
</Consumer>
);
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/modules/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default function({ store }: { store: Store }) {
const api = {
toggleFullscreen(toggled?: boolean) {
return store.setState((state: State) => {
const value = typeof toggled !== 'undefined' ? toggled : !state.layout.isFullscreen;
const value = typeof toggled === 'boolean' ? toggled : !state.layout.isFullscreen;

return {
layout: {
Expand Down
68 changes: 38 additions & 30 deletions lib/ui/src/components/preview/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,24 @@ const renderIframe = (storyId, id, baseUrl, scale, queryParams) => (

const getElementList = memoize(10)((getFn, type, base) => base.concat(Object.values(getFn(type))));

const ActualPreview = ({ wrappers, id, storyId, active, baseUrl, scale, queryParams }) =>
wrappers.reduceRight(
const ActualPreview = ({
wrappers,
id,
storyId,
active,
baseUrl,
scale,
queryParams,
customCanvas,
}) => {
const data = [storyId, id, baseUrl, scale, queryParams];
const base = customCanvas ? customCanvas(...data) : renderIframe(...data);

return wrappers.reduceRight(
(acc, wrapper, index) => wrapper.render({ index, children: acc, id, storyId, active }),
renderIframe(storyId, id, baseUrl, scale, queryParams)
base
);
};

const defaultWrappers = [
{ render: p => <div hidden={!p.active}>{p.children}</div> },
Expand All @@ -67,18 +80,7 @@ const defaultWrappers = [
];

const getTools = memoize(10)(
(
getElements,
queryParams,
panels,
actions,
options,
storyId,
viewMode,
location,
path,
baseUrl
) => {
(getElements, queryParams, panels, api, options, storyId, viewMode, location, path, baseUrl) => {
const tools = getElementList(getElements, types.TOOL, [
panels.filter(p => p.id !== 'canvas').length
? {
Expand Down Expand Up @@ -139,7 +141,7 @@ const getTools = memoize(10)(
<DesktopOnly>
<IconButton
key="full"
onClick={actions.toggleFullscreen}
onClick={api.toggleFullscreen}
title={options.isFullscreen ? 'Exit full screen' : 'Go full screen'}
>
<Icons icon={options.isFullscreen ? 'close' : 'expand'} />
Expand Down Expand Up @@ -235,7 +237,8 @@ class Preview extends Component {
storyId,
queryParams,
getElements,
actions,
api,
customCanvas,
options,
description,
baseUrl,
Expand All @@ -250,17 +253,20 @@ class Preview extends Component {
match: p => p.viewMode === 'story',
render: p => (
<ZoomConsumer>
{({ value }) => (
<ActualPreview
active={p.active}
wrappers={wrappers}
id={id}
storyId={storyId}
baseUrl={baseUrl}
queryParams={queryParams}
scale={value}
/>
)}
{({ value }) => {
const props = {
active: p.active,
wrappers,
id,
storyId,
baseUrl,
queryParams,
scale: value,
customCanvas,
};

return <ActualPreview {...props} />;
}}
</ZoomConsumer>
),
title: 'Canvas',
Expand All @@ -271,7 +277,7 @@ class Preview extends Component {
getElements,
queryParams,
panels,
actions,
api,
options,
storyId,
viewMode,
Expand Down Expand Up @@ -309,10 +315,12 @@ class Preview extends Component {
Preview.propTypes = {
id: PropTypes.string.isRequired,
description: PropTypes.string,
customCanvas: PropTypes.func,
api: PropTypes.shape({
on: PropTypes.func,
off: PropTypes.func,
emit: PropTypes.func,
toggleFullscreen: PropTypes.func,
}).isRequired,
storyId: PropTypes.string,
path: PropTypes.string,
Expand All @@ -324,7 +332,6 @@ Preview.propTypes = {
isFullscreen: PropTypes.bool,
isToolshown: PropTypes.bool,
}).isRequired,
actions: PropTypes.shape({}).isRequired,
baseUrl: PropTypes.string,
};
Preview.defaultProps = {
Expand All @@ -333,6 +340,7 @@ Preview.defaultProps = {
path: undefined,
description: undefined,
baseUrl: 'iframe.html',
customCanvas: undefined,
};

export { Preview };
2 changes: 1 addition & 1 deletion lib/ui/src/containers/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const createMenu = memoize(1)((api, shortcutKeys, isFullscreen, showPanel, showN
{
id: 'F',
title: 'Go full screen',
onClick: () => api.toggleFullscreen(),
onClick: api.toggleFullscreen,
right: shortcutToHumanString(shortcutKeys.fullScreen),
left: isFullscreen ? 'check' : <ListItemIcon />,
},
Expand Down
32 changes: 12 additions & 20 deletions lib/ui/src/containers/preview.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import memoize from 'memoizerific';

import { Consumer } from '@storybook/api';

Expand All @@ -11,28 +10,21 @@ const replacer = match => ` ${match} `;
const addExtraWhiteSpace = input =>
input.replace(nonAlphanumSpace, replacer).replace(doubleSpace, ' ');

const createPreviewActions = memoize(1)(api => ({
toggleFullscreen: () => api.toggleFullscreen(),
}));
const mapper = ({ api, state: { layout, location, selected, customQueryParams } }) =>
api.renderPreview
? { renderPreview: api.renderPreview }
: {
api,
getElements: api.getElements,
actions: createPreviewActions(api),
options: layout,
description: selected ? addExtraWhiteSpace(`${selected.kind} - ${selected.name}`) : '',
...api.getUrlState(),
queryParams: customQueryParams,
location,
};
const mapper = ({ api, state: { layout, location, selected, customQueryParams } }) => ({
api,
getElements: api.getElements,
options: layout,
description: selected ? addExtraWhiteSpace(`${selected.kind} - ${selected.name}`) : '',
...api.getUrlState(),
queryParams: customQueryParams,
location,
});

const PreviewConnected = React.memo(props => (
<Consumer filter={mapper}>
{fromState =>
fromState.renderPreview ? fromState.renderPreview() : <Preview {...props} {...fromState} />
}
{fromState => {
return <Preview {...props} {...fromState} customCanvas={fromState.api.renderPreview} />;
}}
</Consumer>
));
PreviewConnected.displayName = 'PreviewConnected';
Expand Down