forked from blocks/blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.js
More file actions
163 lines (155 loc) · 4.2 KB
/
block.js
File metadata and controls
163 lines (155 loc) · 4.2 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/** @jsx jsx */
import React from 'react'
import { Link } from 'gatsby'
import { jsx, Styled } from 'theme-ui'
import * as components from '@theme-ui/components'
import { Global } from '@emotion/core'
import * as blocks from '@blocks/blocks/src'
import InlineRender from 'blocks-ui/dist/inline-render'
import * as controls from 'property-controls'
import SEO from './seo'
const { Container } = components
const isComponent = name => /^[A-Z]/.test(name)
const scope = {
React,
jsx,
Styled,
...components,
...blocks,
...controls
}
const PropertyControlsTable = ({ controls, name }) => (
<Styled.table sx={{ m: 0 }}>
<Styled.tr>
<Styled.th sx={{ width: '33%' }}>
<Styled.code>{name}</Styled.code>
</Styled.th>
<Styled.th sx={{ width: '16%' }}>Type</Styled.th>
<Styled.th sx={{ width: '33%' }}>Default Value</Styled.th>
</Styled.tr>
{Object.entries(controls).map(([key, val]) => (
<Styled.tr key={key}>
<Styled.td>{val.title || key}</Styled.td>
<Styled.td>
<Styled.code>{val.type}</Styled.code>
</Styled.td>
<Styled.td>{val.defaultValue || 'None'}</Styled.td>
</Styled.tr>
))}
</Styled.table>
)
export default ({ block }) => {
const component = blocks[block.displayName]
const components = Object.entries(component).reduce((acc, [key, val]) => {
if (isComponent(key)) {
const componentName = [block.displayName, key].join('.')
acc[componentName] = val
}
return acc
}, {})
return (
<Styled.root>
<Global
styles={{
'*': {
boxSizing: 'border-box'
},
body: {
margin: 0
}
}}
/>
<SEO title={block.displayName} />
<header
sx={{
display: 'flex',
width: '100%',
alignItems: 'center',
py: 2,
px: 3,
borderBottom: 'thin solid #e1e6eb'
}}
>
<Link to="/blocks">
<img
alt="Blocks logo"
src="https://user-images.githubusercontent.com/1424573/61592179-e0fda080-ab8c-11e9-9109-166cc7c86b43.png"
sx={{
mt: -1,
height: 24,
verticalAlign: 'middle',
mr: 2
}}
/>
</Link>
<Link
to="/blocks"
sx={{
color: 'inherit',
textDecoration: 'none',
fontSize: [1, 1, 1],
lineHeight: 1
}}
>
Blocks
</Link>
<span sx={{ px: 1, fontSize: 0 }}>/</span>
<Styled.h1
sx={{
m: 0,
lineHeight: 1,
fontWeight: 'body',
fontSize: [1, 1, 1]
}}
>
{block.displayName}
</Styled.h1>
</header>
<main>
<section
sx={{
mb: [3, 4, 5],
p: [3, 4, 5],
borderBottom: 'thin solid #e1e6eb',
backgroundColor: '#fafafa'
}}
>
<InlineRender
sx={{
backgroundColor: 'background'
}}
scope={scope}
code={block.transformed}
/>
</section>
<Container>
<Styled.h3>Property controls</Styled.h3>
<PropertyControlsTable
name={block.displayName}
controls={component.propertyControls}
/>
{Object.entries(components)
.filter(([_, val]) => !!val.propertyControls)
.map(([key, val]) => (
<PropertyControlsTable
key={key}
name={key}
controls={val.propertyControls}
/>
))}
<Styled.h3 sx={{ mt: [3, 4, 5] }}>Example usage</Styled.h3>
<Styled.pre>
{"/** @jsx jsx */\nimport { jsx } from 'theme-ui'\n" +
`import { ${block.displayName} } from '@blocks/components'` +
'\n\n' +
'export default () => (\n ' +
component.usage.trim() +
'\n)'}
</Styled.pre>
<Styled.h3>Source code</Styled.h3>
<Styled.pre>{block.src}</Styled.pre>
</Container>
</main>
</Styled.root>
)
}