-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathMDXComponents.tsx
509 lines (475 loc) · 13.6 KB
/
MDXComponents.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
import {Children, useContext, useMemo} from 'react';
import * as React from 'react';
import cn from 'classnames';
import CodeBlock from './CodeBlock';
import {CodeDiagram} from './CodeDiagram';
import {ConsoleBlock, ConsoleLogLine, ConsoleBlockMulti} from './ConsoleBlock';
import ExpandableCallout from './ExpandableCallout';
import ExpandableExample from './ExpandableExample';
import {H1, H2, H3, H4, H5} from './Heading';
import InlineCode from './InlineCode';
import Intro from './Intro';
import BlogCard from './BlogCard';
import Link from './Link';
import {PackageImport} from './PackageImport';
import Recap from './Recap';
import Sandpack from './Sandpack';
import SandpackWithHTMLOutput from './SandpackWithHTMLOutput';
import Diagram from './Diagram';
import DiagramGroup from './DiagramGroup';
import SimpleCallout from './SimpleCallout';
import TerminalBlock from './TerminalBlock';
import YouWillLearnCard from './YouWillLearnCard';
import {Challenges, Hint, Solution} from './Challenges';
import {IconNavArrow} from '../Icon/IconNavArrow';
import ButtonLink from 'components/ButtonLink';
import {TocContext} from './TocContext';
import type {Toc, TocItem} from './TocContext';
import {TeamMember} from './TeamMember';
import {LanguagesContext} from './LanguagesContext';
import {finishedTranslations} from 'utils/finishedTranslations';
import ErrorDecoder from './ErrorDecoder';
import {IconCanary} from '../Icon/IconCanary';
function CodeStep({children, step}: {children: any; step: number}) {
return (
<span
data-step={step}
className={cn(
'code-step bg-opacity-10 dark:bg-opacity-20 relative rounded px-[6px] py-[1.5px] border-b-[2px] border-opacity-60',
{
'bg-blue-40 border-blue-40 text-blue-60 dark:text-blue-30':
step === 1,
'bg-yellow-40 border-yellow-40 text-yellow-60 dark:text-yellow-30':
step === 2,
'bg-purple-40 border-purple-40 text-purple-60 dark:text-purple-30':
step === 3,
'bg-green-40 border-green-40 text-green-60 dark:text-green-30':
step === 4,
}
)}>
{children}
</span>
);
}
const P = (p: JSX.IntrinsicElements['p']) => (
<p className="whitespace-pre-wrap my-4" {...p} />
);
const Strong = (strong: JSX.IntrinsicElements['strong']) => (
<strong className="font-bold" {...strong} />
);
const OL = (p: JSX.IntrinsicElements['ol']) => (
<ol className="ms-6 my-3 list-decimal" {...p} />
);
const LI = (p: JSX.IntrinsicElements['li']) => (
<li className="leading-relaxed mb-1" {...p} />
);
const UL = (p: JSX.IntrinsicElements['ul']) => (
<ul className="ms-6 my-3 list-disc" {...p} />
);
const Divider = () => (
<hr className="my-6 block border-b border-t-0 border-border dark:border-border-dark" />
);
const Wip = ({children}: {children: React.ReactNode}) => (
<ExpandableCallout type="wip">{children}</ExpandableCallout>
);
const Pitfall = ({children}: {children: React.ReactNode}) => (
<ExpandableCallout type="pitfall">{children}</ExpandableCallout>
);
const Deprecated = ({children}: {children: React.ReactNode}) => (
<ExpandableCallout type="deprecated">{children}</ExpandableCallout>
);
const Note = ({children}: {children: React.ReactNode}) => (
<ExpandableCallout type="note">{children}</ExpandableCallout>
);
const Canary = ({children}: {children: React.ReactNode}) => (
<ExpandableCallout type="canary">{children}</ExpandableCallout>
);
const CanaryBadge = ({title}: {title: string}) => (
<span
title={title}
className={
'text-base font-display px-1 py-0.5 font-bold bg-gray-10 dark:bg-gray-60 text-gray-60 dark:text-gray-10 rounded'
}>
<IconCanary
size="s"
className={'inline me-1 mb-0.5 text-sm text-gray-60 dark:text-gray-10'}
/>
Canary only
</span>
);
const Blockquote = ({
children,
...props
}: JSX.IntrinsicElements['blockquote']) => {
return (
<blockquote
className="mdx-blockquote py-4 px-8 my-8 shadow-inner-border dark:shadow-inner-border-dark bg-highlight dark:bg-highlight-dark bg-opacity-50 rounded-2xl leading-6 flex relative"
{...props}>
<span className="block relative">{children}</span>
</blockquote>
);
};
function LearnMore({
children,
path,
}: {
title: string;
path?: string;
children: any;
}) {
return (
<>
<section className="p-8 mt-16 mb-16 flex flex-row shadow-inner-border dark:shadow-inner-border-dark justify-between items-center bg-card dark:bg-card-dark rounded-2xl">
<div className="flex-col">
<h2 className="text-primary font-display dark:text-primary-dark font-bold text-2xl leading-tight">
Ready to learn this topic?
</h2>
{children}
{path ? (
<ButtonLink
className="mt-1"
label="Read More"
href={path}
type="primary">
Read More
<IconNavArrow displayDirection="end" className="inline ms-1" />
</ButtonLink>
) : null}
</div>
</section>
<hr className="border-border dark:border-border-dark mb-14" />
</>
);
}
function ReadBlogPost({path}: {path: string}) {
return (
<ButtonLink className="mt-1" label="Read Post" href={path} type="primary">
Read Post
<IconNavArrow displayDirection="end" className="inline ms-1" />
</ButtonLink>
);
}
function Math({children}: {children: any}) {
return (
<span
style={{
fontFamily: 'STIXGeneral-Regular, Georgia, serif',
fontSize: '1.2rem',
}}>
{children}
</span>
);
}
function MathI({children}: {children: any}) {
return (
<span
style={{
fontFamily: 'STIXGeneral-Italic, Georgia, serif',
fontSize: '1.2rem',
}}>
{children}
</span>
);
}
function YouWillLearn({
children,
isChapter,
}: {
children: any;
isChapter?: boolean;
}) {
let title = isChapter ? 'In this chapter' : 'आप सीखेंगे ';
return <SimpleCallout title={title}>{children}</SimpleCallout>;
}
// TODO: typing.
function Recipes(props: any) {
return <Challenges {...props} isRecipes={true} />;
}
function AuthorCredit({
author = 'Rachel Lee Nabors',
authorLink = 'https://nearestnabors.com/',
}: {
author: string;
authorLink: string;
}) {
return (
<div className="sr-only group-hover:not-sr-only group-focus-within:not-sr-only hover:sr-only">
<p className="bg-card dark:bg-card-dark text-center text-sm text-secondary dark:text-secondary-dark leading-tight p-2 rounded-lg absolute start-1/2 -top-4 -translate-x-1/2 -translate-y-full group-hover:flex group-hover:opacity-100 after:content-[''] after:absolute after:start-1/2 after:top-[95%] after:-translate-x-1/2 after:border-8 after:border-x-transparent after:border-b-transparent after:border-t-card after:dark:border-t-card-dark opacity-0 transition-opacity duration-300">
<cite>
Illustrated by{' '}
{authorLink ? (
<a
target="_blank"
rel="noreferrer"
className="text-link dark:text-link-dark"
href={authorLink}>
{author}
</a>
) : (
author
)}
</cite>
</p>
</div>
);
}
const IllustrationContext = React.createContext<{
isInBlock?: boolean;
}>({
isInBlock: false,
});
function Illustration({
caption,
src,
alt,
author,
authorLink,
}: {
caption: string;
src: string;
alt: string;
author: string;
authorLink: string;
}) {
const {isInBlock} = React.useContext(IllustrationContext);
return (
<div className="relative group before:absolute before:-inset-y-16 before:inset-x-0 my-16 mx-0 2xl:mx-auto max-w-4xl 2xl:max-w-6xl">
<figure className="my-8 flex justify-center">
<img
src={src}
alt={alt}
style={{maxHeight: 300}}
className="rounded-lg"
/>
{caption ? (
<figcaption className="text-center leading-tight mt-4">
{caption}
</figcaption>
) : null}
</figure>
{!isInBlock && <AuthorCredit author={author} authorLink={authorLink} />}
</div>
);
}
const isInBlockTrue = {isInBlock: true};
function IllustrationBlock({
sequential,
author,
authorLink,
children,
}: {
author: string;
authorLink: string;
sequential: boolean;
children: any;
}) {
const imageInfos = Children.toArray(children).map(
(child: any) => child.props
);
const images = imageInfos.map((info, index) => (
<figure key={index}>
<div className="bg-white rounded-lg p-4 flex-1 flex xl:p-6 justify-center items-center my-4">
<img
className="text-primary"
src={info.src}
alt={info.alt}
height={info.height}
/>
</div>
{info.caption ? (
<figcaption className="text-secondary dark:text-secondary-dark text-center leading-tight mt-4">
{info.caption}
</figcaption>
) : null}
</figure>
));
return (
<IllustrationContext.Provider value={isInBlockTrue}>
<div className="relative group before:absolute before:-inset-y-16 before:inset-x-0 my-16 mx-0 2xl:mx-auto max-w-4xl 2xl:max-w-6xl">
{sequential ? (
<ol className="mdx-illustration-block flex">
{images.map((x: any, i: number) => (
<li className="flex-1" key={i}>
{x}
</li>
))}
</ol>
) : (
<div className="mdx-illustration-block">{images}</div>
)}
<AuthorCredit author={author} authorLink={authorLink} />
</div>
</IllustrationContext.Provider>
);
}
type NestedTocRoot = {
item: null;
children: Array<NestedTocNode>;
};
type NestedTocNode = {
item: TocItem;
children: Array<NestedTocNode>;
};
function calculateNestedToc(toc: Toc): NestedTocRoot {
const currentAncestors = new Map<number, NestedTocNode | NestedTocRoot>();
const root: NestedTocRoot = {
item: null,
children: [],
};
const startIndex = 1; // Skip "Overview"
for (let i = startIndex; i < toc.length; i++) {
const item = toc[i];
const currentParent: NestedTocNode | NestedTocRoot =
currentAncestors.get(item.depth - 1) || root;
const node: NestedTocNode = {
item,
children: [],
};
currentParent.children.push(node);
currentAncestors.set(item.depth, node);
}
return root;
}
function InlineToc() {
const toc = useContext(TocContext);
const root = useMemo(() => calculateNestedToc(toc), [toc]);
if (root.children.length < 2) {
return null;
}
return <InlineTocItem items={root.children} />;
}
function InlineTocItem({items}: {items: Array<NestedTocNode>}) {
return (
<UL>
{items.map((node) => (
<LI key={node.item.url}>
<Link href={node.item.url}>{node.item.text}</Link>
{node.children.length > 0 && <InlineTocItem items={node.children} />}
</LI>
))}
</UL>
);
}
type TranslationProgress = 'complete' | 'in-progress';
function LanguageList({progress}: {progress: TranslationProgress}) {
const allLanguages = React.useContext(LanguagesContext) ?? [];
const languages = allLanguages
.filter(
({code}) =>
code !== 'en' &&
(progress === 'complete'
? finishedTranslations.includes(code)
: !finishedTranslations.includes(code))
)
.sort((a, b) => a.enName.localeCompare(b.enName));
return (
<UL>
{languages.map(({code, name, enName}) => {
return (
<LI key={code}>
<Link href={`https://${code}.react.dev/`}>
{enName} ({name})
</Link>{' '}
—{' '}
<Link href={`https://github.com/reactjs/${code}.react.dev`}>
Contribute
</Link>
</LI>
);
})}
</UL>
);
}
function YouTubeIframe(props: any) {
return (
<div className="relative h-0 overflow-hidden pt-[56.25%]">
<iframe
className="absolute inset-0 w-full h-full"
frameBorder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowFullScreen
title="YouTube video player"
{...props}
/>
</div>
);
}
function Image(props: any) {
const {alt, ...rest} = props;
return <img alt={alt} className="max-w-[calc(min(700px,100%))]" {...rest} />;
}
export const MDXComponents = {
p: P,
strong: Strong,
blockquote: Blockquote,
ol: OL,
ul: UL,
li: LI,
h1: H1,
h2: H2,
h3: H3,
h4: H4,
h5: H5,
hr: Divider,
a: Link,
img: Image,
BlogCard,
code: InlineCode,
pre: CodeBlock,
CodeDiagram,
ConsoleBlock,
ConsoleBlockMulti,
ConsoleLogLine,
DeepDive: (props: {
children: React.ReactNode;
title: string;
excerpt: string;
}) => <ExpandableExample {...props} type="DeepDive" />,
Diagram,
DiagramGroup,
FullWidth({children}: {children: any}) {
return children;
},
MaxWidth({children}: {children: any}) {
return <div className="max-w-4xl ms-0 2xl:mx-auto">{children}</div>;
},
Pitfall,
Deprecated,
Wip,
Illustration,
IllustrationBlock,
Intro,
InlineToc,
LanguageList,
LearnMore,
Math,
MathI,
Note,
Canary,
CanaryBadge,
PackageImport,
ReadBlogPost,
Recap,
Recipes,
Sandpack,
SandpackWithHTMLOutput,
TeamMember,
TerminalBlock,
YouWillLearn,
YouWillLearnCard,
Challenges,
Hint,
Solution,
CodeStep,
YouTubeIframe,
ErrorDecoder,
};
for (let key in MDXComponents) {
if (MDXComponents.hasOwnProperty(key)) {
const MDXComponent: any = (MDXComponents as any)[key];
MDXComponent.mdxName = key;
}
}