-
Notifications
You must be signed in to change notification settings - Fork 423
/
Copy pathcomponent-layout.tsx
150 lines (145 loc) · 5.12 KB
/
component-layout.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
import {HEADER_HEIGHT} from '@primer/gatsby-theme-doctocat/src/components/header'
import PageFooter from '@primer/gatsby-theme-doctocat/src/components/page-footer'
import TableOfContents from '@primer/gatsby-theme-doctocat/src/components/table-of-contents'
import {Box, Heading, Text, Breadcrumbs} from '@primer/react'
import React from 'react'
import {withPrefix} from 'gatsby'
import {BaseLayout} from '../components/base-layout'
import {ComponentPageNav} from '../components/component-page-nav'
import navItems from '@primer/gatsby-theme-doctocat/src/nav.yml'
type NavItemData = {
title: string
url?: string
children?: NavItemData[]
}
export default function ComponentLayout({pageContext, children, path}) {
const {title, description, reactId, railsIds, figmaId, cssId} = pageContext.frontmatter
const getPageAncestry = (url: string, object: NavItemData[]) => {
const result: NavItemData[] = []
const buildArray = (node: NavItemData, path: string) => {
if (node.url === path) {
result.push({title: node.title, url: node.url})
} else if (node.children) {
for (const child of node.children) {
buildArray(child, path)
if (result.length > 0) {
result.unshift({title: node.title, url: node.url})
break
}
}
}
}
for (const node of object) {
buildArray(node, url)
if (result.length > 0) {
break
}
}
return result
}
const breadcrumbData = getPageAncestry(path, navItems).filter(item => item.url)
return (
<BaseLayout title={title} description={description}>
<Box sx={{maxWidth: 1200, width: '100%', pb: [4, 5, 6, 7], px: [4, 5, 6, 7], pt: [2, 3, 4, 5], mx: 'auto'}}>
{breadcrumbData.length > 1 ? (
<Breadcrumbs sx={{mb: 4}}>
{breadcrumbData
.map(item =>
item.url ? (
<Breadcrumbs.Item key={item.url} href={withPrefix(item.url)} selected={path === item.url}>
{item.title}
</Breadcrumbs.Item>
) : null,
)
.filter(item => item)}
</Breadcrumbs>
) : null}
<Heading as="h1" sx={{fontSize: 7}}>
{title}
</Heading>
{description ? (
<Text as="p" sx={{fontSize: 3, m: 0, mb: 3, maxWidth: '60ch'}}>
{description}
</Text>
) : null}
<Box sx={{mb: 4}}>
<ComponentPageNav
basePath={path}
includeReact={reactId}
includeRails={railsIds}
includeFigma={figmaId}
includeCSS={cssId}
current="overview"
/>
</Box>
<Box sx={{display: 'flex', flexDirection: 'row-reverse', alignItems: 'start', gap: [null, 7, 8, 9]}}>
<Box
sx={{
width: 220,
flex: '0 0 auto',
position: 'sticky',
top: HEADER_HEIGHT + 24,
maxHeight: `calc(100vh - ${HEADER_HEIGHT}px - 24px)`,
display: ['none', null, 'block'],
}}
>
{pageContext.tableOfContents.items ? (
<>
<Heading
as="h3"
sx={{fontSize: 1, display: 'inline-block', fontWeight: 'bold', pl: 3}}
id="toc-heading"
>
On this page
</Heading>
<TableOfContents aria-labelledby="toc-heading" items={pageContext.tableOfContents.items} />
</>
) : null}
</Box>
<Box sx={{minWidth: 0, flexGrow: 1}}>
{/* Narrow table of contents */}
{pageContext.tableOfContents.items ? (
<Box
sx={{
display: ['block', null, 'none'],
mb: 5,
borderColor: 'border.muted',
bg: 'canvas.subtle',
borderWidth: '1px',
borderStyle: 'solid',
borderRadius: 2,
}}
>
<Box sx={{px: 3, py: 2}}>
<Box
sx={{flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', display: 'flex'}}
>
<Heading as="h3" sx={{fontSize: 1, fontWeight: 'bold'}} id="toc-heading-narrow">
On this page
</Heading>
</Box>
</Box>
<Box sx={{borderTop: '1px solid', borderColor: 'border.muted'}}>
<TableOfContents aria-labelledby="toc-heading-narrow" items={pageContext.tableOfContents.items} />
</Box>
</Box>
) : null}
<Box
sx={{
'& > :first-child': {
mt: 0,
},
'& > :last-child': {
mb: 0,
},
}}
>
{children}
</Box>
</Box>
</Box>
<PageFooter editUrl={pageContext.editUrl} contributors={pageContext.contributors} />
</Box>
</BaseLayout>
)
}