-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathcomponent-page-template.tsx
280 lines (263 loc) · 8.48 KB
/
component-page-template.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
/* eslint-disable react/no-unstable-nested-components */
import React, { useContext, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { graphql, Link } from 'gatsby';
import { MDXProvider } from '@mdx-js/react';
import { MDXRenderer } from 'gatsby-plugin-mdx';
import classNames from 'classnames';
import {
Container,
Alert,
breakpoints,
useMediaQuery,
Stack,
} from '~paragon-react';
import { SettingsContext } from '../context/SettingsContext';
import CodeBlock from '../components/CodeBlock';
import GenericPropsTable from '../components/PropsTable';
import Layout from '../components/PageLayout';
import SEO from '../components/SEO';
import LinkedHeading from '../components/LinkedHeading';
import ComponentsUsage from '../components/insights/ComponentsUsage';
import LeaveFeedback from '../components/LeaveFeedback';
import PageEditBtn from '../components/PageEditBtn';
import ComponentVariablesTable from '../components/ComponentVariablesTable';
export interface IPageTemplate {
data: {
mdx: {
frontmatter: {
title: string,
status: string,
components: string,
notes: string,
},
tableOfContents: {
items: Array<{}>,
},
body: string,
},
components: {
nodes: [],
}
},
pageContext: {
cssVariablesData: string[],
componentsUsageInsights: string[],
githubEditPath: string,
}
}
export type ShortCodesTypes = {
displayName: string,
};
export default function PageTemplate({
data: { mdx, components: componentNodes },
pageContext: { cssVariablesData, componentsUsageInsights, githubEditPath },
}: IPageTemplate) {
const isMobile = useMediaQuery({ maxWidth: breakpoints.large.maxWidth });
const [showMinimizedTitle, setShowMinimizedTitle] = useState(false);
const { settings } = useContext(SettingsContext);
const components = componentNodes.nodes
.reduce((acc: { [x: string]: { displayName: string, props?: [] }; }, currentValue: { displayName: string; }) => {
acc[currentValue.displayName] = currentValue;
return acc;
}, {});
const shortcodes = React.useMemo(() => {
function PropsTable({ displayName, ...props }: ShortCodesTypes) { // eslint-disable-line react/prop-types
if (components[displayName]) {
return <GenericPropsTable {...components[displayName]} {...props} />;
}
return null;
}
// Provide common components here
return {
h2: (props: HTMLElement) => <LinkedHeading h="2" {...props} />,
h3: (props: HTMLElement) => <LinkedHeading h="3" {...props} />,
h4: (props: HTMLElement) => <LinkedHeading h="4" {...props} />,
h5: (props: HTMLElement) => <LinkedHeading h="5" {...props} />,
h6: (props: HTMLElement) => <LinkedHeading h="6" {...props} />,
pre: (props:
JSX.IntrinsicAttributes & React.ClassAttributes<HTMLDivElement> &
React.HTMLAttributes<HTMLDivElement>) => <div {...props} />,
code: CodeBlock,
Link,
PropsTable,
};
}, [components]);
const cssVariablesTitle = 'Theme Variables';
const cssVariablesUrl = 'theme-variables';
const propsAPITitle = 'Props API';
const propsAPIUrl = 'props-api';
const usageInsightsTitle = 'Usage Insights';
const usageInsightsUrl = 'usage-insights';
const sortedComponentNames = mdx.frontmatter?.components || [];
const filteredComponentsUsageInsights = componentsUsageInsights.map(componentName => componentName.replace(/\./g, ''));
const isUsageInsights = (sortedComponentNames as []).some(value => filteredComponentsUsageInsights.includes(value));
const getTocData = () => {
const tableOfContents = JSON.parse(JSON.stringify(mdx.tableOfContents));
if (cssVariablesData?.length && !tableOfContents.items?.includes()) {
tableOfContents.items?.push({
title: cssVariablesTitle,
url: `#${cssVariablesUrl}`,
});
}
tableOfContents.items?.push({ title: propsAPITitle, url: `#${propsAPIUrl}` });
if (isUsageInsights) {
tableOfContents.items?.push({
title: usageInsightsTitle,
url: `#${usageInsightsUrl}`,
});
}
return tableOfContents;
};
const isDeprecated = mdx.frontmatter?.status?.toLowerCase().includes('deprecate') || false;
useEffect(() => setShowMinimizedTitle(!!isMobile), [isMobile]);
return (
<Layout
showMinimizedTitle={showMinimizedTitle}
isMdx
tocData={getTocData()}
githubEditPath={githubEditPath}
>
{/* eslint-disable-next-line react/jsx-pascal-case */}
<SEO title={mdx.frontmatter.title} />
<Container size={settings.containerWidth} className="py-5">
{isDeprecated && (
<Alert variant="warning">
<Alert.Heading>This component will be removed soon.</Alert.Heading>
<p className="small mb-0">{mdx.frontmatter.notes}</p>
</Alert>
)}
<Stack
className={classNames('justify-content-between', {
'flex-column-reverse align-items-start': isMobile,
})}
direction="horizontal"
>
<h1>
{mdx.frontmatter.title}
</h1>
<Stack
className={classNames('mb-2', { 'justify-content-end': isMobile })}
direction="horizontal"
gap={2}
>
<PageEditBtn githubEditPath={githubEditPath} />
<LeaveFeedback />
</Stack>
</Stack>
<MDXProvider components={shortcodes}>
<MDXRenderer>{mdx.body}</MDXRenderer>
</MDXProvider>
{!!cssVariablesData?.length && (
<div className="mb-5">
<h2 className="mb-4 pgn-doc__heading" id={cssVariablesUrl}>
{cssVariablesTitle}
<a href={`#${cssVariablesUrl}`} aria-label="Jump to CSS variables">
<span className="pgn-doc__anchor">#</span>
</a>
</h2>
<ComponentVariablesTable rawStylesheet={cssVariablesData} />
</div>
)}
{components[sortedComponentNames[0]]?.props && (
<h2 className="mb-5 pgn-doc__heading" id={propsAPIUrl}>
{propsAPITitle}
<a href={`#${propsAPIUrl}`} aria-label="Props API">
<span className="pgn-doc__anchor">#</span>
</a>
</h2>
)}
{typeof sortedComponentNames !== 'string' && sortedComponentNames?.map((componentName: string | number) => {
const node: { displayName: string } = components[componentName];
if (!node) {
return null;
}
return <GenericPropsTable key={node.displayName} {...node} />;
})}
{isUsageInsights && (
<>
<h2 className="pgn-doc__heading m-0" id={usageInsightsUrl}>
{usageInsightsTitle}
<a href={`#${usageInsightsUrl}`} aria-label="Usage Insights">
<span className="pgn-doc__anchor">#</span>
</a>
</h2>
<ComponentsUsage data={(sortedComponentNames as string[])} />
</>
)}
</Container>
</Layout>
);
}
PageTemplate.propTypes = {
data: PropTypes.shape({
mdx: PropTypes.shape({
frontmatter: PropTypes.shape({
title: PropTypes.string,
status: PropTypes.string,
}),
body: PropTypes.any, // eslint-disable-line react/forbid-prop-types
tableOfContents: PropTypes.shape({
items: PropTypes.arrayOf(PropTypes.object), // eslint-disable-line react/forbid-prop-types
}),
}),
components: PropTypes.shape({
nodes: PropTypes.arrayOf(PropTypes.shape({})),
}),
}).isRequired,
pageContext: PropTypes.shape({
scssVariablesData: PropTypes.shape({
openedx: PropTypes.string,
edxorg: PropTypes.string,
}),
}),
};
PageTemplate.defaultProps = {
pageContext: null,
};
export const pageQuery = graphql`
query ComponentPageQuery($id: String, $components: [String]) {
mdx(id: { eq: $id }) {
id
body
frontmatter {
title
status
notes
components
}
tableOfContents
}
components: allComponentMetadata(
filter: { displayName: { in: $components } }
) {
nodes {
...ComponentDocGenData
}
}
}
fragment ComponentDocGenData on ComponentMetadata {
displayName
props {
defaultValue {
value
}
name
type {
name
raw
value
}
required
docblock
doclets
description {
id
text
childMdx {
body
}
}
}
}
`;