-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathjsx.tsx
68 lines (58 loc) · 1.65 KB
/
jsx.tsx
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from 'react';
import { ActionBar, SyntaxHighlighter } from '@storybook/components';
import { styled } from '@storybook/theming';
import copy from 'copy-to-clipboard';
import { Listener } from './register';
import jsxRenderer, { ComponentMap } from './renderer';
const Container = styled.div(({ theme }) => ({
padding: theme.layoutMargin
}));
interface JSXProps {
/** Whether the panel is active */
active: boolean;
ob(listener: Listener): void;
}
/** The panel that renders the jsx for the story */
const JSX = ({ ob, active }: JSXProps) => {
const [current, setCurrent] = React.useState<string | undefined>(undefined);
const [jsx, setJsx] = React.useState<Record<string, [string, ComponentMap]>>(
{}
);
React.useEffect(() => {
ob({
next: type => {
if (type === 'jsx') {
return (id: string, newJsx: string, components: ComponentMap) =>
setJsx({ ...jsx, [id]: [newJsx, components] });
}
return setCurrent;
}
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const [code, components] = current && jsx[current] ? jsx[current] : ['', {}];
const copyJsx = React.useCallback(() => copy(code), [code]);
return active ? (
<>
<Container>
<SyntaxHighlighter
language="jsx"
format={false}
renderer={jsxRenderer(components)}
wrapLines={false}
>
{code}
</SyntaxHighlighter>
</Container>
<ActionBar
actionItems={[
{
title: 'Copy',
onClick: copyJsx
}
]}
/>
</>
) : null;
};
export default JSX;