Skip to content

Commit a86b731

Browse files
committed
API: Enable MDX descriptions inside Function tags
This is surely going to fail in some edge cases, but I'd rather not mess with it any more than necessary right now.
1 parent 9cfa3cc commit a86b731

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

src/components/API/API.jsx

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import styles from "./styles.module.css";
44

55
class Description extends React.Component {
66
render() {
7-
const item = this.props.for || "Whoops";
8-
const placeholderTitle = `${item}: There's nothing here yet :(`;
9-
const fallback = <Placeholder title={placeholderTitle} />;
7+
const fallback = (
8+
<>
9+
<div className={styles.descriptionBox}></div>
10+
</>
11+
);
1012

11-
const children = this.props.childre;
13+
const children = this.props.children;
1214
if (!this.props.children) return fallback;
13-
if (this.props.children) return fallback;
1415

15-
const hasMultipleChildren = children instanceof Array;
16+
const hasMultipleChildren =
17+
children instanceof Array && children.length > 0;
1618
const content = hasMultipleChildren ? children : [children];
1719

1820
const description = (
@@ -25,9 +27,30 @@ class Description extends React.Component {
2527
}
2628

2729
class Function extends React.Component {
30+
mdxTypeToPlaceholderComponent(mdxType) {
31+
const placeholdersByType = {
32+
Description: <Description for={mdxType} />,
33+
Parameters: <Description for={mdxType} />,
34+
Returns: <Description for={mdxType} />,
35+
Structures: <Description for={mdxType} />,
36+
};
37+
return placeholdersByType[mdxType];
38+
}
39+
findFirstChildByType(mdxType) {
40+
const fallback = this.mdxTypeToPlaceholderComponent(mdxType);
41+
const children =
42+
this.props.children instanceof Array ? this.props.children : [];
43+
if (!children || children.length === 0) return fallback;
44+
return children.find((child) => child.props.mdxType == mdxType) || fallback;
45+
}
2846
render() {
2947
const since = this.props.since;
30-
const children = this.props.children;
48+
let children = this.props.children;
49+
50+
const description = this.findFirstChildByType("Description");
51+
const parameters = this.findFirstChildByType("Parameters");
52+
const returnValues = this.findFirstChildByType("Returns");
53+
const structs = this.findFirstChildByType("Structures");
3154

3255
const isRunningInDevelopmentMode = process.env.NODE_ENV !== "production";
3356
if (isRunningInDevelopmentMode) {
@@ -57,12 +80,26 @@ class Function extends React.Component {
5780
return (
5881
<>
5982
{sinceBlock}
60-
<div className={styles.function}>{children}</div>
83+
<div className={styles.flexColumn}>
84+
<div className={styles.flexRow}>{description}</div>
85+
<div className={styles.flexRow}>
86+
{parameters}
87+
{returnValues}
88+
</div>
89+
{structs}
90+
</div>
91+
<hr />
6192
</>
6293
);
6394
}
6495
}
6596

97+
class Structures extends React.Component {
98+
render() {
99+
return <>{this.props.children}</>;
100+
}
101+
}
102+
66103
class NilableInfo extends React.Component {
67104
render() {
68105
return <>?</>;
@@ -417,4 +454,5 @@ export {
417454
Placeholder,
418455
Blocking,
419456
Description,
457+
Structures,
420458
};

src/components/API/styles.module.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ body {
136136
}
137137

138138
.descriptionBox {
139-
padding: 1rem 1rem;
139+
padding: 0rem 1rem;
140+
margin-top: 1rem;
140141
}
141142
.sinceBlock {
142143
border: 1px solid var(--ifm-color-primary-darkest);

0 commit comments

Comments
 (0)