-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
MarkdownDocsV2.js
331 lines (298 loc) · 10.3 KB
/
MarkdownDocsV2.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
import * as React from 'react';
import PropTypes from 'prop-types';
import { useRouter } from 'next/router';
import kebabCase from 'lodash/kebabCase';
import { useTheme } from '@mui/system';
import { exactProp } from '@mui/utils';
import { CssVarsProvider as JoyCssVarsProvider, useColorScheme } from '@mui/joy/styles';
import ComponentsApiContent from 'docs/src/modules/components/ComponentsApiContent';
import HooksApiContent from 'docs/src/modules/components/HooksApiContent';
import { getTranslatedHeader as getComponentTranslatedHeader } from 'docs/src/modules/components/ApiPage';
import RichMarkdownElement from 'docs/src/modules/components/RichMarkdownElement';
import { pathnameToLanguage } from 'docs/src/modules/utils/helpers';
import AppLayoutDocs from 'docs/src/modules/components/AppLayoutDocs';
import { useTranslate, useUserLanguage } from '@mui/docs/i18n';
import BrandingProvider from 'docs/src/BrandingProvider';
import Ad from 'docs/src/modules/components/Ad';
import { HEIGHT as AppFrameHeight } from 'docs/src/modules/components/AppFrame';
import { HEIGHT as TabsHeight } from 'docs/src/modules/components/ComponentPageTabs';
import AdGuest from 'docs/src/modules/components/AdGuest';
import { getPropsToC } from 'docs/src/modules/components/ApiPage/sections/PropertiesSection';
import { getClassesToC } from 'docs/src/modules/components/ApiPage/sections/ClassesSection';
function JoyModeObserver({ mode }) {
const { setMode } = useColorScheme();
React.useEffect(() => {
setMode(mode);
}, [mode, setMode]);
return null;
}
JoyModeObserver.propTypes = {
mode: PropTypes.oneOf(['light', 'dark']),
};
function getHookTranslatedHeader(t, header) {
const translations = {
demos: t('api-docs.demos'),
import: t('api-docs.import'),
'hook-name': t('api-docs.hookName'),
parameters: t('api-docs.parameters'),
'return-value': t('api-docs.returnValue'),
};
// TODO Drop runtime type-checking once we type-check this file
if (!translations.hasOwnProperty(header)) {
throw new TypeError(
`Unable to translate header '${header}'. Did you mean one of '${Object.keys(
translations,
).join("', '")}'`,
);
}
return translations[header] || header;
}
export default function MarkdownDocsV2(props) {
const theme = useTheme();
const router = useRouter();
const [activeTab, setActiveTab] = React.useState(router.query.docsTab ?? '');
const { canonicalAs } = pathnameToLanguage(router.asPath);
const {
disableAd = false,
disableToc = false,
demos = {},
docs,
demoComponents,
srcComponents,
componentsApiDescriptions,
componentsApiPageContents,
hooksApiDescriptions,
hooksApiPageContents,
} = props;
const userLanguage = useUserLanguage();
const t = useTranslate();
React.useEffect(() => {
setActiveTab(router.query.docsTab ?? '');
}, [router.query.docsTab]);
const localizedDoc = docs[userLanguage] || docs.en;
// Generate the TOC based on the tab
const demosToc = localizedDoc.toc.filter((item) => item.text !== 'API');
function createHookTocEntry(hookName, sectionName, hookProps = {}) {
const hookPropToc = [];
Object.keys(hookProps).forEach((propName) => {
hookPropToc.push({
text: propName,
hash: `${hookName}-${sectionName}-${propName}`,
children: [],
});
});
return {
text: getHookTranslatedHeader(t, sectionName),
hash: `${hookName}-${sectionName}`,
children: hookPropToc,
};
}
const hooksToc = [];
if (hooksApiPageContents) {
Object.keys(hooksApiPageContents).forEach((key) => {
const { name: hookName, parameters = {}, returnValue = {} } = hooksApiPageContents[key];
const hookNameKebabCase = kebabCase(hookName);
const hookToc = [
createHookTocEntry(hookNameKebabCase, 'import'),
createHookTocEntry(hookNameKebabCase, 'parameters', parameters),
createHookTocEntry(hookNameKebabCase, 'return-value', returnValue),
].filter(Boolean);
hooksToc.push({
text: hookName,
hash: hookNameKebabCase,
children: hookToc,
});
});
}
function createComponentTocEntry(
componentName,
sectionName,
options = { inheritance: false, themeDefaultProps: false },
) {
return {
text: getComponentTranslatedHeader(t, sectionName),
hash: `${componentName}-${sectionName}`,
children: [
...(options.inheritance
? [{ text: t('api-docs.inheritance'), hash: 'inheritance', children: [] }]
: []),
...(options.themeDefaultProps
? [{ text: t('api-docs.themeDefaultProps'), hash: 'theme-default-props', children: [] }]
: []),
],
};
}
const componentsApiToc = [];
if (componentsApiPageContents) {
Object.keys(componentsApiPageContents).forEach((key) => {
const { componentDescriptionToc = [] } = componentsApiDescriptions[key][userLanguage];
const {
name: componentName,
inheritance,
slots,
themeDefaultProps,
classes,
props: componentProps,
} = componentsApiPageContents[key];
const componentNameKebabCase = kebabCase(componentName);
const componentApiToc = [
createComponentTocEntry(componentNameKebabCase, 'import'),
...componentDescriptionToc,
getPropsToC({
t,
componentName: componentNameKebabCase,
componentProps,
inheritance,
themeDefaultProps,
hash: `${componentNameKebabCase}-props`,
}),
slots?.length > 0 && createComponentTocEntry(componentNameKebabCase, 'slots'),
...getClassesToC({
t,
componentName: componentNameKebabCase,
componentClasses: classes,
hash: `${componentNameKebabCase}-classes`,
}),
].filter(Boolean);
componentsApiToc.push({
text: componentName,
hash: componentNameKebabCase,
children: componentApiToc,
});
});
}
const isJoy = canonicalAs.startsWith('/joy-ui/');
const CssVarsProvider = isJoy ? JoyCssVarsProvider : React.Fragment;
const Wrapper = isJoy ? BrandingProvider : React.Fragment;
const wrapperProps = {
...(isJoy && { mode: theme.palette.mode }),
};
const commonElements = [];
let i = 0;
let done = false;
// process the elements before the tabs component
while (i < localizedDoc.rendered.length && !done) {
const renderedMarkdownOrDemo = localizedDoc.rendered[i];
if (renderedMarkdownOrDemo.component && renderedMarkdownOrDemo.component.indexOf('Tabs') >= 0) {
done = true;
}
commonElements.push(
<RichMarkdownElement
key={`common-elements-${i}`}
activeTab={activeTab}
demoComponents={demoComponents}
demos={demos}
disableAd={disableAd}
localizedDoc={localizedDoc}
renderedMarkdownOrDemo={renderedMarkdownOrDemo}
srcComponents={srcComponents}
theme={theme}
WrapperComponent={Wrapper}
wrapperProps={wrapperProps}
/>,
);
i += 1;
}
let activeToc = demosToc;
if (activeTab === 'hooks-api') {
activeToc = hooksToc;
}
if (activeTab === 'components-api') {
activeToc = componentsApiToc;
}
const hasTabs = localizedDoc.rendered.some((renderedMarkdownOrDemo) => {
if (
typeof renderedMarkdownOrDemo === 'object' &&
renderedMarkdownOrDemo.component &&
renderedMarkdownOrDemo.component === 'modules/components/ComponentPageTabs.js'
) {
return true;
}
return false;
});
return (
<AppLayoutDocs
cardOptions={{
description: localizedDoc.headers.cardDescription,
title: localizedDoc.headers.cardTitle,
}}
description={localizedDoc.description}
disableAd={disableAd}
disableToc={disableToc}
location={localizedDoc.location}
title={localizedDoc.title}
toc={activeToc}
disableLayout
hasTabs={hasTabs}
>
<div
style={{
'--MuiDocs-header-height': hasTabs
? `${AppFrameHeight + TabsHeight}px`
: `${AppFrameHeight}px`,
}}
>
{disableAd ? null : (
<BrandingProvider>
<AdGuest classSelector={hasTabs ? '.component-tabs' : undefined}>
<Ad />
</AdGuest>
</BrandingProvider>
)}
<CssVarsProvider>
{isJoy && <JoyModeObserver mode={theme.palette.mode} />}
{commonElements}
{activeTab === '' &&
localizedDoc.rendered
// for the "hook only" edge case, for example Base UI autocomplete
.slice(
i,
localizedDoc.rendered.length - (localizedDoc.headers.components.length > 0 ? 1 : 0),
)
.map((renderedMarkdownOrDemo, index) => (
<RichMarkdownElement
key={`demos-section-${index}`}
activeTab={activeTab}
demoComponents={demoComponents}
demos={demos}
disableAd={disableAd}
localizedDoc={localizedDoc}
renderedMarkdownOrDemo={renderedMarkdownOrDemo}
srcComponents={srcComponents}
theme={theme}
WrapperComponent={Wrapper}
wrapperProps={wrapperProps}
/>
))}
{activeTab === 'components-api' && (
<ComponentsApiContent
descriptions={componentsApiDescriptions}
pageContents={componentsApiPageContents}
/>
)}
{activeTab === 'hooks-api' && (
<HooksApiContent
descriptions={hooksApiDescriptions}
pagesContents={hooksApiPageContents}
/>
)}
</CssVarsProvider>
</div>
</AppLayoutDocs>
);
}
MarkdownDocsV2.propTypes = {
componentsApiDescriptions: PropTypes.object,
componentsApiPageContents: PropTypes.object,
demoComponents: PropTypes.object,
demos: PropTypes.object,
disableAd: PropTypes.bool,
disableToc: PropTypes.bool,
docs: PropTypes.object.isRequired,
hooksApiDescriptions: PropTypes.object,
hooksApiPageContents: PropTypes.object,
srcComponents: PropTypes.object,
};
if (process.env.NODE_ENV !== 'production') {
MarkdownDocsV2.propTypes = exactProp(MarkdownDocsV2.propTypes);
}