/
ComponentExample.js
248 lines (215 loc) · 7.41 KB
/
ComponentExample.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { KnobProvider } from '@stardust-ui/docs-components'
import cx from 'classnames'
import copyToClipboard from 'copy-to-clipboard'
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import { InView } from 'react-intersection-observer'
import { Checkbox, Grid, Label } from 'semantic-ui-react'
import { examplePathToHash, scrollToAnchor } from 'docs/src/utils'
import CarbonAdNative from 'docs/src/components/CarbonAd/CarbonAdNative'
import formatCode from 'docs/src/utils/formatCode'
import ComponentControls from '../ComponentControls'
import ExampleEditor from '../ExampleEditor'
import ComponentDocContext from '../ComponentDocContext'
import ComponentExampleKnobs from './ComponentExampleKnobs'
import ComponentExampleTitle from './ComponentExampleTitle'
const childrenStyle = {
paddingBottom: 0,
paddingTop: 0,
maxWidth: '50rem',
}
const componentControlsStyle = {
flex: '0 0 auto',
width: 'auto',
}
/* eslint-disable react/prop-types */
const knobComponents = {
KnobControl: (props) => (
<div style={{ display: 'flex', alignItems: 'center', flexGrow: 1, marginLeft: 5 }}>
{props.children}
</div>
),
KnobBoolean: (props) => (
<Checkbox
checked={props.value}
onChange={(e, data) => props.setValue(data.checked)}
type='checkbox'
value={props.value}
/>
),
KnobLabel: (props) => (
<Label size='small' style={{ fontFamily: 'monospace' }}>
{props.name}
</Label>
),
}
/* eslint-enable react/prop-types */
/**
* Renders a `component` and the raw `code` that produced it.
* Allows toggling the raw `code` code block.
*/
class ComponentExample extends Component {
static propTypes = {
children: PropTypes.node,
description: PropTypes.node,
examplePath: PropTypes.string.isRequired,
history: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
onVisibilityChange: PropTypes.func.isRequired,
renderHtml: PropTypes.bool,
sourceCode: PropTypes.string.isRequired,
suiVersion: PropTypes.string,
title: PropTypes.node,
}
static defaultProps = {
renderHtml: true,
}
constructor(props) {
super(props)
const anchorName = examplePathToHash(props.examplePath)
const hashName = `#${anchorName}`
this.state = {
anchorName,
hashName,
originalSourceCode: props.sourceCode,
showCode: hashName === props.location.hash,
sourceCode: props.sourceCode,
}
}
static getDerivedStateFromProps(props, state) {
const willBeActiveHash = state.hashName === props.location.hash
const derivedState = { isActiveHash: willBeActiveHash }
// deactivate examples when switching from one to the next
if (state.isActiveHash && !willBeActiveHash) {
derivedState.showCode = false
derivedState.showHTML = false
}
if (process.env.NODE_ENV !== 'production') {
const wasCodeChanged = state.originalSourceCode !== state.sourceCode
const wasCodeUpdated = props.sourceCode !== state.originalSourceCode
if (wasCodeUpdated) {
if (wasCodeChanged) {
/* eslint-disable-next-line no-console */
console.warn(
`[HMR] the code of example (${props.examplePath}) was not reload because it was modified, please reset your changes.`,
)
} else {
derivedState.originalSourceCode = props.sourceCode
derivedState.sourceCode = props.sourceCode
}
}
}
return derivedState
}
setHashAndScroll = () => {
const { history, location } = this.props
const { anchorName } = this.state
history.replace(`${location.pathname}#${anchorName}`)
scrollToAnchor()
}
handleCodeChange = (sourceCode) => this.setState({ sourceCode })
handleCodeFormat = () =>
this.setState((prevState) => ({ sourceCode: formatCode(prevState.sourceCode) }))
handleCodeReset = () =>
this.setState((prevState) => ({ sourceCode: prevState.originalSourceCode }))
handleDirectLinkClick = () => {
this.setHashAndScroll()
copyToClipboard(window && window.location.href)
}
handleShowCodeClick = (e) => {
const { showCode } = this.state
e.preventDefault()
const nextShowCode = !showCode
if (nextShowCode) this.setHashAndScroll()
this.setState({ showCode: nextShowCode })
}
handleVisibility = (willVisible) => {
const { examplePath, onVisibilityChange, title } = this.props
const { wasEverVisible } = this.state
if (willVisible && !wasEverVisible) {
this.setState({ wasEverVisible: true })
}
if (title) onVisibilityChange(examplePath, willVisible)
}
render() {
const { children, description, examplePath, renderHtml, suiVersion, title } = this.props
const {
anchorName,
isActiveHash,
showCode,
originalSourceCode,
sourceCode,
wasEverVisible,
} = this.state
return (
<InView onChange={(inView) => this.handleVisibility(inView)}>
<div id={anchorName} style={{ marginTop: '1rem' }}>
<Grid className={cx('docs-example', showCode && 'active')} padded='vertically'>
<Grid.Row columns='equal'>
<Grid.Column>
<ComponentExampleTitle
description={description}
title={title}
suiVersion={suiVersion}
/>
</Grid.Column>
<Grid.Column textAlign='right' style={componentControlsStyle}>
<ComponentControls
anchorName={anchorName}
disableHtml={!renderHtml}
exampleCode={sourceCode}
examplePath={examplePath}
onCopyLink={this.handleDirectLinkClick}
onShowCode={this.handleShowCodeClick}
showCode={showCode}
visible={wasEverVisible}
/>
</Grid.Column>
</Grid.Row>
<KnobProvider components={knobComponents}>
<ComponentExampleKnobs />
{children && (
<Grid.Row columns={1} style={childrenStyle}>
<Grid.Column>{children}</Grid.Column>
</Grid.Row>
)}
<Grid.Row style={{ paddingBottom: wasEverVisible && 0 }}>
<ExampleEditor
examplePath={examplePath}
hasCodeChanged={originalSourceCode !== sourceCode}
onCodeChange={this.handleCodeChange}
onCodeFormat={this.handleCodeFormat}
onCodeReset={this.handleCodeReset}
renderHtml={renderHtml}
showCode={showCode}
sourceCode={sourceCode}
title={title}
visible={wasEverVisible}
/>
</Grid.Row>
</KnobProvider>
{isActiveHash && <CarbonAdNative inverted={showCode} />}
</Grid>
</div>
</InView>
)
}
}
/* TODO: Replace this temporary component with hooks */
const Wrapper = (props) => {
const { exampleSources, ...rest } = React.useContext(ComponentDocContext)
/* eslint-disable-next-line react/prop-types */
const sourceCode = exampleSources[props.examplePath]
return (
<ComponentExample
{...props}
{...rest}
/* eslint-disable-next-line react/prop-types */
sourceCode={sourceCode}
>
{/* eslint-disable-next-line react/prop-types */}
{props.children}
</ComponentExample>
)
}
export default Wrapper