forked from npm/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnav-items.js
More file actions
195 lines (174 loc) · 5.1 KB
/
nav-items.js
File metadata and controls
195 lines (174 loc) · 5.1 KB
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
import { BorderBox, Box, Flex, StyledOcticon, Link, themeGet } from '@primer/components'
import { LinkExternalIcon } from '@primer/octicons-react'
import { Link as GatsbyLink } from 'gatsby'
import preval from 'preval.macro'
import React from 'react'
import styled from 'styled-components'
import NavHierarchy from '../nav-hierarchy'
// This code needs to run at build-time so it can access the file system.
const repositoryUrl = preval`
const readPkgUp = require('read-pkg-up')
const getPkgRepo = require('get-pkg-repo')
try {
const repo = getPkgRepo(readPkgUp.sync().package)
module.exports = \`https://github.com/\${repo.user}/\${repo.project}\`
} catch (error) {
module.exports = ''
}
`
const getActiveProps = (className) => (props) => {
const location = NavHierarchy.getLocation(props.location.pathname)
const href = NavHierarchy.getLocation(props.href)
if (NavHierarchy.isActiveUrl(location, href)) {
return { className: `${className} active` }
}
return { className: `${className}` }
}
const ActiveLink = ({ className, children, ...props }) => (
<Link as={GatsbyLink} getProps={getActiveProps(className)} {...props}>
{children}
</Link>
)
const NavLink = styled(ActiveLink)`
color: inherit;
text-decoration: none;
:hover {
text-decoration: underline;
}
`
const TopLevelLink = styled(NavLink)`
&.active {
font-weight: ${themeGet('fontWeights.bold')};
color: ${themeGet('colors.gray.8')};
}
&.activePage {
color: ${themeGet('colors.gray.8')};
}
`
const SecondLevelLink = styled(NavLink)`
display: block;
font-size: ${themeGet('fontSizes.1')};
padding-top: ${themeGet('space.1')};
padding-bottom: ${themeGet('space.1')};
margin-top: ${themeGet('space.2')};
&.active {
font-weight: ${themeGet('fontWeights.bold')};
color: ${themeGet('colors.gray.8')};
}
`
const ThirdLevelLink = styled(NavLink)`
display: block;
font-size: ${themeGet('fontSizes.1')};
padding-top: ${themeGet('space.1')};
padding-bottom: ${themeGet('space.1')};
border-left: solid 1px ${themeGet('colors.gray.3')};
padding-left: calc(${themeGet('space.2')} + (${themeGet('space.1')} - 1px));
color: ${themeGet('colors.blue.5')};
&.active {
border-left: solid ${themeGet('space.1')} ${themeGet('colors.gray.3')};
padding-left: ${themeGet('space.2')};
color: ${themeGet('colors.gray.8')};
}
`
const Description = styled(Box)`
& {
color: ${themeGet('colors.gray.6')};
font-size: 0.8em;
font-weight: normal;
}
`
function topLevelItems (items, path) {
if (items == null) {
return null
}
return (
<>
{items.map((item) => {
const children = NavHierarchy.isActiveUrl(path, item.url) ? NavHierarchy.getHierarchy(item, { path: path, hideVariants: true }) : null
return (
<BorderBox
key={item.title}
borderWidth={0}
borderRadius={0}
borderTopWidth={1}
py={3}
px={4}
role="listitem"
>
<Flex flexDirection="column">
<TopLevelLink to={item.url} key={item.title}>{item.title}</TopLevelLink>
{secondLevelItems(children, path)}
</Flex>
</BorderBox>
)
})}
</>
)
}
function secondLevelItems (items, path) {
if (items == null) {
return null
}
return (
<Flex flexDirection="column" mt={2} role="list">
{items.map((item) => {
const children = NavHierarchy.isActiveUrl(path, item.url) ? NavHierarchy.getHierarchy(item, { path: path, hideVariants: true }) : null
return (
<Box key={item.title} role="listitem">
<SecondLevelLink key={item.url} to={item.url}>
{item.title}
{item.description != null ? (
<>
<Description>{item.description}</Description>
</>
) : null}
</SecondLevelLink>
{thirdLevelItems(children, path)}
</Box>
)
})}
</Flex>
)
}
function thirdLevelItems (items) {
if (items == null) {
return null
}
return (
<Flex flexDirection="column" mt={2} role="list">
{items.map((item) => (
<Box key={item.title} role="listitem">
<ThirdLevelLink key={item.url} to={item.url}>
{item.title}
</ThirdLevelLink>
</Box>
))}
</Flex>
)
}
function githubLink () {
if (!repositoryUrl) {
return null
}
return (
<BorderBox borderWidth={0} borderTopWidth={1} borderRadius={0} py={5} px={4}>
<Link href={repositoryUrl} color="inherit">
<Flex justifyContent="space-between" alignItems="center" color="gray.5">
Edit on GitHub
<StyledOcticon icon={LinkExternalIcon} color="gray.5" />
</Flex>
</Link>
</BorderBox>
)
}
function NavItems (props) {
const path = NavHierarchy.getLocation(props.location.pathname)
const items = NavHierarchy.getHierarchy(null, { path: path, hideVariants: true })
return (
<>
{topLevelItems(items, path)}
{props.editOnGitHub !== false ? githubLink(props) : null}
</>
)
}
export default NavItems