-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
preview.js
39 lines (33 loc) · 1.17 KB
/
preview.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import React from 'react';
import memoize from 'memoizerific';
import { Consumer } from '@storybook/api';
import { Preview } from '../components/preview/preview';
const nonAlphanumSpace = /[^a-z0-9 ]/gi;
const doubleSpace = /\s\s/gi;
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 } }) =>
api.renderPreview
? { renderPreview: api.renderPreview }
: {
api,
getElements: api.getElements,
actions: createPreviewActions(api),
options: layout,
description: selected ? addExtraWhiteSpace(`${selected.kind} - ${selected.name}`) : '',
...api.getUrlState(),
location,
};
const PreviewConnected = React.memo(props => (
<Consumer filter={mapper}>
{fromState =>
fromState.renderPreview ? fromState.renderPreview() : <Preview {...props} {...fromState} />
}
</Consumer>
));
PreviewConnected.displayName = 'PreviewConnected';
export default PreviewConnected;