Skip to content

Commit 227ca33

Browse files
committed
replace mdxName usage
1 parent 78eb453 commit 227ca33

File tree

7 files changed

+35
-17
lines changed

7 files changed

+35
-17
lines changed

src/components/MDX/Challenges/Challenges.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ const parseChallengeContents = (
4242
let challenge: Partial<ChallengeContents> = {};
4343
let content: React.ReactElement[] = [];
4444
Children.forEach(children, (child) => {
45-
const {props, type} = child as React.ReactElement<{
45+
const {props} = child as React.ReactElement<{
4646
children?: string;
4747
id?: string;
48+
'data-mdx-name'?: string;
4849
}>;
49-
switch ((type as any).mdxName) {
50+
51+
switch (props?.['data-mdx-name']) {
5052
case 'Solution': {
5153
challenge.solution = child;
5254
challenge.content = content;

src/components/MDX/Challenges/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use client';
2+
13
/*
24
* Copyright (c) Facebook, Inc. and its affiliates.
35
*/

src/components/MDX/CodeDiagram.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function CodeDiagram({children, flip = false}: CodeDiagramProps) {
1616
return child.type === 'img';
1717
});
1818
const content = Children.toArray(children).map((child: any) => {
19-
if (child.type?.mdxName === 'pre') {
19+
if (child.props?.['data-mdx-name'] === 'pre') {
2020
return (
2121
<CodeBlock
2222
key={child.key}

src/components/MDX/ExpandableExample.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ interface ExpandableExampleProps {
2121
}
2222

2323
function ExpandableExample({children, excerpt, type}: ExpandableExampleProps) {
24-
if (!Array.isArray(children) || children[0].type.mdxName !== 'h4') {
24+
// Validate children using `data-mdx-name`
25+
if (
26+
!Array.isArray(children) ||
27+
children[0].props?.['data-mdx-name'] !== 'h4'
28+
) {
2529
throw Error(
2630
`Expandable content ${type} is missing a corresponding title at the beginning`
2731
);
2832
}
33+
2934
const isDeepDive = type === 'DeepDive';
3035
const isExample = type === 'Example';
3136
const id = children[0].props.id;
@@ -60,8 +65,7 @@ function ExpandableExample({children, excerpt, type}: ExpandableExampleProps) {
6065
className="list-none p-8"
6166
tabIndex={-1 /* there's a button instead */}
6267
onClick={(e) => {
63-
// We toggle using a button instead of this whole area,
64-
// with an escape case for the header anchor link
68+
// Toggle with a button instead of the whole area
6569
if (!(e.target instanceof SVGElement)) {
6670
e.preventDefault();
6771
}

src/components/MDX/Link.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function Link({
1717
const classes =
1818
'inline text-link dark:text-link-dark border-b border-link border-opacity-0 hover:border-opacity-100 duration-100 ease-in transition leading-normal';
1919
const modifiedChildren = Children.toArray(children).map((child: any) => {
20-
if (child.type?.mdxName && child.type?.mdxName === 'inlineCode') {
20+
if (child.props?.['data-mdx-name'] === 'inlineCode') {
2121
return cloneElement(child, {
2222
isLink: true,
2323
});

src/components/MDX/MDXComponents.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,17 @@ function Image(props: any) {
381381
return <img alt={alt} className="max-w-[calc(min(700px,100%))]" {...rest} />;
382382
}
383383

384-
export const MDXComponents = {
384+
function annotateMDXComponents(
385+
components: Record<string, React.ElementType>
386+
): Record<string, React.ElementType> {
387+
return Object.entries(components).reduce((acc, [key, Component]) => {
388+
acc[key] = (props) => <Component {...props} data-mdx-name={key} />;
389+
acc[key].displayName = `Annotated(${key})`; // Optional, for debugging
390+
return acc;
391+
}, {} as Record<string, React.ElementType>);
392+
}
393+
394+
export const MDXComponents = annotateMDXComponents({
385395
p: P,
386396
strong: Strong,
387397
blockquote: Blockquote,
@@ -450,11 +460,11 @@ export const MDXComponents = {
450460
CodeStep,
451461
YouTubeIframe,
452462
ErrorDecoder,
453-
};
463+
});
454464

455-
for (let key in MDXComponents) {
456-
if (MDXComponents.hasOwnProperty(key)) {
457-
const MDXComponent: any = (MDXComponents as any)[key];
458-
MDXComponent.mdxName = key;
459-
}
460-
}
465+
// for (let key in MDXComponents) {
466+
// if (MDXComponents.hasOwnProperty(key)) {
467+
// const MDXComponent: any = (MDXComponents as any)[key];
468+
// MDXComponent.mdxName = key;
469+
// }
470+
// }

src/components/MDX/PackageImport.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ interface PackageImportProps {
1212

1313
export function PackageImport({children}: PackageImportProps) {
1414
const terminal = Children.toArray(children).filter((child: any) => {
15-
return child.type?.mdxName !== 'pre';
15+
return child.props?.['data-mdx-name'] !== 'pre';
1616
});
1717
const code = Children.toArray(children).map((child: any, i: number) => {
18-
if (child.type?.mdxName === 'pre') {
18+
if (child.props?.['data-mdx-name'] === 'pre') {
1919
return (
2020
<CodeBlock
2121
{...child.props}

0 commit comments

Comments
 (0)