-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathLiveEditor.js
77 lines (72 loc) · 1.66 KB
/
LiveEditor.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
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
69
70
71
72
73
74
75
76
77
import React from 'react'
import {
LiveProvider,
LivePreview,
LiveEditor,
LiveError
} from 'react-live'
import { ScopeConsumer } from 'react-scope-provider'
import { Box } from 'rebass'
import { color, borderColor } from 'styled-system'
import styled from 'styled-components'
import mdx from '@mdx-js/mdx'
const transformCode = src => `<React.Fragment>${src}</React.Fragment>`
const transformMdx = src => mdx.sync(src, { skipExport: true })
const Preview = styled(LivePreview)([], {
padding: '16px',
border: '1px solid',
borderRadius: '2px 2px 0 0',
}, borderColor)
Preview.defaultProps = {
borderColor: 'gray'
}
const Editor = styled(LiveEditor)([], {
fontFamily: 'Menlo, monospace',
fontSize: '13px',
margin: 0,
padding: '16px',
overflow: 'auto',
borderRadius: '0 0 2px 2px',
'&:focus': {
outline: 'none',
boxShadow: 'inset 0 0 0 1px #6cf',
}
}, color)
Editor.defaultProps = {
bg: 'gray'
}
const Err = styled(LiveError)([], {
fontFamily: 'Menlo, monospace',
fontSize: '13px',
padding: '8px',
color: 'white',
backgroundColor: 'red'
})
export default ({
code,
scope = {},
render,
mdx
}) => (
<Box mb={4}>
<ScopeConsumer defaultScope={scope}>
{scope => (
<LiveProvider
code={code}
scope={scope}
mountStylesheet={false}
transformCode={mdx ? transformMdx : transformCode}>
{typeof render === 'function' ? (
render({ code, scope })
) : (
<React.Fragment>
<Preview />
<Editor />
<Err />
</React.Fragment>
)}
</LiveProvider>
)}
</ScopeConsumer>
</Box>
)