-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathexample.js
340 lines (321 loc) · 9.68 KB
/
example.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import React, { useContext, useEffect } from 'react';
import { useLocation } from '@reach/router';
import {
Button,
CodeBlock,
Flex,
CodeBlockCode,
debounce,
Label,
Switch,
Tooltip,
Stack,
StackItem
} from '@patternfly/react-core';
import * as reactCoreModule from '@patternfly/react-core';
import * as reactCoreNextModule from '@patternfly/react-core/next';
import * as reactCoreDeprecatedModule from '@patternfly/react-core/deprecated';
import * as reactTableModule from '@patternfly/react-table';
import * as reactTableDeprecatedModule from '@patternfly/react-table/deprecated';
import { css } from '@patternfly/react-styles';
import { getParameters } from 'codesandbox/lib/api/define';
import { ExampleToolbar } from './exampleToolbar';
import { AutoLinkHeader } from '../autoLinkHeader/autoLinkHeader';
import {
slugger,
getStaticParams,
getReactParams,
getExampleClassName,
getExampleId,
liveCodeTypes,
} from '../../helpers';
import { convertToReactComponent } from '@patternfly/ast-helpers';
import missingThumbnail from './missing-thumbnail.jpg';
import { RtlContext } from '../../layouts';
const errorComponent = (err) => <pre>{err.toString()}</pre>;
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { error: null, errorInfo: null };
}
componentDidCatch(error, errorInfo) {
errorInfo._suppressLogging = true;
this.setState({
error: error,
errorInfo: errorInfo,
});
}
componentDidUpdate(prevProps) {
if (prevProps.children !== this.props.children) {
this.setState({ error: null, errorInfo: null });
}
}
render() {
if (this.state.errorInfo) {
return errorComponent(this.state.error);
}
return this.props.children;
}
}
// Props come from mdx-ast-to-mdx-hast.js
export const Example = ({
// The ts/js/html code for the example
code,
// The language of the code
lang = '',
// Second parameter to sourceMD for file containing this code
// Should match tab name
source,
// Whether to disable the live code editor
noLive = !liveCodeTypes.includes(lang),
// Nearest parent h3
title = 'Untitled',
// Whether the example is fullscreen only and we should show a thumbnail
isFullscreen,
// Whether the example is open on the fullscreen page
isFullscreenPreview,
// The image src thumbnail for the example
thumbnail = missingThumbnail,
// Whether the example shows demo capability
isDemo,
// Whether the example is open to further evolution
isBeta,
// Whether the example is deprecated
isDeprecated,
// Slugified source + title
id,
// Section in frontmatter of MD file (components, demos, etc)
section,
// Extra constants for example (images, extra JS files, etc)
liveContext,
// Content that appears between h3 and code block to explain example
children,
// Show dark theme switcher on full page examples
hasDarkThemeSwitcher = process.env.hasDarkThemeSwitcher,
// Show dark theme switcher on full page examples
hasRTLSwitcher = process.env.hasRTLSwitcher,
// Map of relative imports matched to their npm package import path (passed to Codesandbox)
relativeImports,
// md file location in node_modules, used to resolve relative import paths in examples
relPath = '',
// absolute url to hosted file
sourceLink = '',
}) => {
if (isFullscreenPreview) {
isFullscreen = false;
}
//append a class to the document body on fullscreen examples to indicate to screenshot/automated visual regression tools that the page has loaded
const addPageLoadedClass = () => document.body.classList.add('page-loaded');
useEffect(() => {
if (!isFullscreenPreview) return;
document.readyState === 'complete'
? addPageLoadedClass()
: window.addEventListener('load', addPageLoadedClass);
return () => window.removeEventListener('load', addPageLoadedClass);
}, []);
if (!lang) {
// Inline code
return <code className="ws-code">{code}</code>;
} else if (noLive) {
// Code block
return (
<CodeBlock>
<CodeBlockCode>{code}</CodeBlockCode>
</CodeBlock>
);
}
const [editorCode, setEditorCode] = React.useState(code);
const loc = useLocation();
const isRTL = useContext(RtlContext);
const scope = {
...liveContext,
// These 2 are in the bundle anyways for the site since we dogfood
...reactCoreModule,
...reactTableModule,
...(source === 'react-next' ? reactCoreNextModule : {}),
...(source === 'react-deprecated'
? { ...reactCoreDeprecatedModule, ...reactTableDeprecatedModule }
: {}),
};
let livePreview = null;
if (lang === 'html') {
livePreview = (
<div
className={css(
'ws-preview-html', // core uses this class name to apply styles to specific examples
isFullscreenPreview && 'pf-v6-u-h-100'
)}
dangerouslySetInnerHTML={{ __html: editorCode }}
/>
);
} else {
try {
const { code: transformedCode, hasTS } =
convertToReactComponent(editorCode);
if (hasTS) {
lang = 'ts';
} else {
lang = 'js';
}
const componentNames = Object.keys(scope);
const componentValues = Object.values(scope);
const getPreviewComponent = new Function(
'React',
...componentNames,
transformedCode
);
const PreviewComponent = getPreviewComponent(React, ...componentValues);
livePreview = (
<ErrorBoundary>
<PreviewComponent />
</ErrorBoundary>
);
} catch (err) {
livePreview = errorComponent(err);
}
}
const previewId = getExampleId(source, section[0], id, title);
const className = getExampleClassName(source, section[0], id);
if (isFullscreenPreview) {
return (
<div id={previewId} className={css(className, 'pf-v6-u-h-100')}>
{livePreview}
{(hasDarkThemeSwitcher || hasRTLSwitcher) && (
<Flex
direction={{ default: 'column' }}
gap={{ default: 'gapLg' }}
className="ws-full-page-utils pf-v6-m-dir-ltr "
>
{hasDarkThemeSwitcher && (
<Switch
id="ws-example-theme-switch"
label="Dark theme"
defaultChecked={false}
onChange={() =>
document
.querySelector('html')
.classList.toggle('pf-v6-theme-dark')
}
/>
)}
{hasRTLSwitcher && (
<Switch
id="ws-example-rtl-switch"
label="RTL"
defaultChecked={false}
onChange={() => {
const html = document.querySelector('html');
const curDir = html.dir;
html.dir = curDir !== 'rtl' ? 'rtl' : 'ltr';
}}
/>
)}
</Flex>
)}
</div>
);
}
const codeBoxParams = getParameters(
lang === 'html'
? getStaticParams(title, editorCode)
: getReactParams(
title,
editorCode,
scope,
lang,
relativeImports,
relPath,
sourceLink
)
);
const fullscreenLink =
loc.pathname.replace(/\/$/, '') +
(loc.pathname.endsWith(source) ? '' : `/${source}`) +
'/' +
slugger(title);
const hasMetaText = isBeta || isDemo || isDeprecated || false;
const tooltips = (<React.Fragment>
{isBeta && (
<Tooltip content="This beta component is currently under review and is still open for further evolution.">
<Button variant="plain" hasNoPadding>
<Label isCompact color="blue">
Beta
</Label>
</Button>
</Tooltip>
)}
{isDemo && (
<Tooltip content="Demos show how multiple components can be used in a single design.">
<Button variant="plain" hasNoPadding>
<Label isCompact color="purple">
Demo
</Label>
</Button>
</Tooltip>
)}
{isDeprecated && (
<Tooltip content="Deprecated components are available for use but are no longer being maintained or enhanced.">
<Button variant="plain" hasNoPadding>
<Label isCompact color="grey">
Deprecated
</Label>
</Button>
</Tooltip>
)}
</React.Fragment>);
const metaText = hasMetaText && tooltips
return (
<Stack hasGutter>
<StackItem>
<AutoLinkHeader
metaText={metaText}
headingLevel="h3"
>
{title}
</AutoLinkHeader>
{children}
</StackItem>
<StackItem className="ws-example-preview">
{isFullscreen ? (
<div>
<a
className="ws-preview__thumbnail-link"
href={fullscreenLink}
target="_blank"
aria-label={`Open fullscreen ${title} example`}
>
<img
src={thumbnail.src}
width={thumbnail.width}
height={thumbnail.height}
alt={`${title} screenshot`}
/>
</a>
</div>
) : (
<div
id={previewId}
className={css(
className,
isRTL && 'pf-v6-m-dir-rtl'
)}
>
{livePreview}
</div>
)}
</StackItem>
<StackItem className="ws-example-toolbar">
<ExampleToolbar
lang={lang}
isFullscreen={isFullscreen}
fullscreenLink={fullscreenLink}
originalCode={code}
code={editorCode}
setCode={debounce(setEditorCode, 300)}
codeBoxParams={codeBoxParams}
exampleTitle={title}
/>
</StackItem>
</Stack>
);
};