-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathNav.tsx
More file actions
256 lines (230 loc) · 6.7 KB
/
Nav.tsx
File metadata and controls
256 lines (230 loc) · 6.7 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
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
'use client';
import {focusRing, size, style} from '@react-spectrum/s2/style' with {type: 'macro'};
import {getLibraryFromPage} from './library';
import {Link} from 'react-aria-components';
import type {PageProps} from '@parcel/rsc';
import {Picker, pressScale} from '@react-spectrum/s2';
import React, {createContext, useContext, useEffect, useRef, useState} from 'react';
export function Nav({pages, currentPage}: PageProps) {
let currentLibrary = getLibraryFromPage(currentPage);
let sections = new Map();
for (let page of pages) {
if (page.exports?.hideNav || page.exports?.hideFromSearch) {
continue;
}
let library = getLibraryFromPage(page);
if (library !== currentLibrary) {
continue;
}
let section = page.exports?.section ?? 'Components';
let sectionPages = sections.get(section) ?? [];
sectionPages.push(page);
sections.set(section, sectionPages);
}
let [maskSize, setMaskSize] = useState(0);
return (
<nav
onScroll={e => setMaskSize(Math.min(e.currentTarget.scrollTop, 32))}
style={{
maskImage: maskSize > 0 ? `linear-gradient(to bottom, transparent, black ${maskSize}px)` : undefined
}}
className={style({
position: 'sticky',
top: 40,
height: 'fit',
maxHeight: 'calc(100vh - 72px)',
overflow: 'auto',
paddingX: 12,
minWidth: 180,
display: {
default: 'none',
lg: 'block'
}
})}>
{[...sections].sort((a, b) => a[0].localeCompare(b[0])).map(([name, pages]) => (
<SideNavSection title={name} key={name}>
<SideNav>
{pages
.sort((a, b) => {
let aIntro = isIntroduction(a);
let bIntro = isIntroduction(b);
if (aIntro && !bIntro) {
return -1;
}
if (!aIntro && bIntro) {
return 1;
}
return title(a).localeCompare(title(b));
})
.map(page => (
<SideNavItem key={page.url}><SideNavLink href={page.url} isSelected={page.url === currentPage.url}>{title(page)}</SideNavLink></SideNavItem>
))}
</SideNav>
</SideNavSection>
))}
</nav>
);
}
function title(page) {
return page.exports?.title ?? page.tableOfContents?.[0]?.title ?? page.name;
}
function isIntroduction(page) {
let navTitle = page.exports?.navigationTitle;
if (typeof navTitle === 'string' && navTitle.trim().toLowerCase() === 'introduction') {
return true;
}
let t = title(page);
return typeof t === 'string' && t.trim().toLowerCase() === 'introduction';
}
function SideNavSection({title, children}) {
return (
<section className={style({marginBottom: 24})}>
<div className={style({font: 'ui-sm', color: 'gray-600', minHeight: 32, paddingX: 12, display: 'flex', alignItems: 'center'})}>{title}</div>
{children}
</section>
);
}
const SideNavContext = createContext('');
export function SideNav({children}) {
return (
<ul
className={style({
listStyleType: 'none',
padding: 0,
paddingStart: {
default: 0,
':is(li > ul)': 16
},
margin: 0,
display: 'flex',
flexDirection: 'column',
gap: 8,
width: 'full',
boxSizing: 'border-box'
})}>
{children}
</ul>
);
}
export function SideNavItem(props) {
return (
<li>
{props.children}
</li>
);
}
export function SideNavLink(props) {
let linkRef = useRef(null);
let selected = useContext(SideNavContext);
return (
<Link
{...props}
ref={linkRef}
aria-current={props.isSelected || selected === props.href ? 'page' : undefined}
style={pressScale(linkRef)}
className={style({
...focusRing(),
minHeight: 32,
boxSizing: 'border-box',
paddingX: 4,
// paddingY: centerPadding(),
display: 'flex',
alignItems: 'center',
gap: size(6),
font: 'ui',
fontWeight: {
default: 'normal',
isCurrent: 'bold'
},
textDecoration: 'none',
borderRadius: 'default',
transition: 'default'
})}>
{(renderProps) => (<>
<span
className={style({
width: 2,
height: '[1lh]',
borderRadius: 'full',
transition: 'default',
backgroundColor: {
default: 'transparent',
isHovered: 'gray-400',
isCurrent: 'gray-800'
}
})(renderProps)} />
{props.children}
</>)}
</Link>
);
}
function useCurrentSection() {
let [selected, setSelected] = useState('');
useEffect(() => {
let elements = Array.from(document.querySelectorAll('article > :is(h2,h3,h4,h5)'));
let visible = new Set();
let observer = new IntersectionObserver(entries => {
for (let entry of entries) {
if (entry.isIntersecting) {
visible.add(entry.target);
} else {
visible.delete(entry.target);
}
let firstVisible = elements.find(e => visible.has(e));
if (firstVisible) {
setSelected('#' + firstVisible.id);
}
}
}, {rootMargin: '0px 0px -50% 0px'});
for (let element of elements) {
observer.observe(element);
}
return () => observer.disconnect();
}, []);
return selected;
}
export function OnPageNav({children}) {
let selected = useCurrentSection();
return (
<SideNavContext.Provider value={selected}>
{children}
</SideNavContext.Provider>
);
}
export function MobileOnPageNav({children, currentPage}) {
let [selected, setSelected] = useState('');
useEffect(() => {
let elements = Array.from(document.querySelectorAll('article > :is(h1,h2,h3,h4,h5)'));
elements.reverse();
let visible = new Set();
let observer = new IntersectionObserver(entries => {
for (let entry of entries) {
if (entry.isIntersecting) {
visible.add(entry.target);
} else {
visible.delete(entry.target);
}
}
let lastVisible = elements.find(e => visible.has(e));
if (lastVisible) {
setSelected('#' + lastVisible.id!);
} else {
setSelected('#' + elements.at(-1)!.id);
}
}, {
rootMargin: '9999999px 0px -100% 0px',
// @ts-ignore
scrollMargin: '0px 0px 62px 0px',
threshold: 0.5
});
for (let element of elements) {
observer.observe(element);
}
return () => observer.disconnect();
}, [currentPage]);
return (
<Picker aria-label="Table of contents" selectedKey={selected} isQuiet size="L">
{children}
</Picker>
);
}