-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathContentsLayout.js
More file actions
235 lines (217 loc) · 7.87 KB
/
Copy pathContentsLayout.js
File metadata and controls
235 lines (217 loc) · 7.87 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
import { ClassTable } from '@/components/ClassTable'
import { PageHeader } from '@/components/PageHeader'
import { usePrevNext } from '@/hooks/usePrevNext'
import { SidebarContext, SidebarLayout } from '@/layouts/SidebarLayout'
import { ArrowLeftIcon, ArrowRightIcon, PencilAltIcon } from '@heroicons/react/outline'
import clsx from 'clsx'
import Link from 'next/link'
import { useRouter } from 'next/router'
import {
createContext,
Fragment,
isValidElement,
useCallback,
useContext,
useEffect,
useState,
} from 'react'
export const ContentsContext = createContext()
function TableOfContents({ tableOfContents, currentSection }) {
let sidebarContext = useContext(SidebarContext)
let isMainNav = Boolean(sidebarContext)
function closeNav() {
if (isMainNav) {
sidebarContext.setNavIsOpen(false)
}
}
return (
<>
<h5 className="mb-3 text-sm font-semibold uppercase tracking-wide text-gray-900 dark:text-white lg:text-xs">
On this page
</h5>
<ul className="overflow-x-hidden font-medium text-gray-500 dark:text-gray-400">
{tableOfContents.map((section) => {
let sectionIsActive =
currentSection === section.slug ||
section.children.findIndex(({ slug }) => slug === currentSection) > -1
return (
<Fragment key={section.slug}>
<li>
<a
href={`#${section.slug}`}
onClick={closeNav}
className={clsx(
'block transform py-2 transition-colors duration-200 hover:text-gray-900 dark:hover:text-white',
{
'text-gray-900 dark:text-white': sectionIsActive,
}
)}
>
{section.title}
</a>
</li>
{section.children.map((subsection) => {
let subsectionIsActive = currentSection === subsection.slug
return (
<li
className={clsx({
'ml-4': isMainNav,
'ml-2': !isMainNav,
})}
key={subsection.slug}
>
<a
href={`#${subsection.slug}`}
onClick={closeNav}
className={clsx(
'block py-2 font-medium transition-colors duration-200 hover:text-gray-900 dark:hover:text-white',
{
'text-gray-900 dark:text-white': subsectionIsActive,
}
)}
>
{subsection.title}
</a>
</li>
)
})}
</Fragment>
)
})}
</ul>
</>
)
}
function useTableOfContents(tableOfContents) {
let [currentSection, setCurrentSection] = useState(tableOfContents[0]?.slug)
let [headings, setHeadings] = useState([])
const registerHeading = useCallback((id, top) => {
setHeadings((headings) => [...headings.filter((h) => id !== h.id), { id, top }])
}, [])
const unregisterHeading = useCallback((id) => {
setHeadings((headings) => headings.filter((h) => id !== h.id))
}, [])
useEffect(() => {
if (tableOfContents.length === 0 || headings.length === 0) return
function onScroll() {
let y = window.pageYOffset
let windowHeight = window.innerHeight
let sortedHeadings = headings.concat([]).sort((a, b) => a.top - b.top)
if (y <= 0) {
setCurrentSection(sortedHeadings[0].id)
return
}
if (y + windowHeight >= document.body.scrollHeight) {
setCurrentSection(sortedHeadings[sortedHeadings.length - 1].id)
return
}
const middle = y + windowHeight / 2
let current = sortedHeadings[0].id
for (let i = 0; i < sortedHeadings.length; i++) {
if (middle >= sortedHeadings[i].top) {
current = sortedHeadings[i].id
}
}
setCurrentSection(current)
}
window.addEventListener('scroll', onScroll, {
capture: true,
passive: true,
})
onScroll()
return () => window.removeEventListener('scroll', onScroll, true)
}, [headings, tableOfContents])
return { currentSection, registerHeading, unregisterHeading }
}
export function ContentsLayoutOuter({ children, layoutProps, ...props }) {
const { currentSection, registerHeading, unregisterHeading } = useTableOfContents(
layoutProps.tableOfContents
)
return (
<SidebarLayout
sidebar={
<div className="mb-8">
<TableOfContents
tableOfContents={layoutProps.tableOfContents}
currentSection={currentSection}
/>
</div>
}
{...props}
>
<ContentsContext.Provider value={{ registerHeading, unregisterHeading }}>
{children}
</ContentsContext.Provider>
</SidebarLayout>
)
}
export function ContentsLayout({ children, meta, classes, tableOfContents }) {
const router = useRouter()
const toc = [
...(classes
? [{ title: 'Default class reference', slug: 'class-reference', children: [] }]
: []),
...tableOfContents,
]
const { currentSection, registerHeading, unregisterHeading } = useTableOfContents(toc)
let { prev, next } = usePrevNext()
return (
<div id={meta.containerId} className="flex w-full">
<div className="min-w-0 flex-auto px-4 pt-10 pb-24 sm:px-6 lg:pb-16 xl:px-8">
<PageHeader
title={meta.title}
description={meta.description}
border={!classes && meta.headerSeparator !== false}
/>
<ContentsContext.Provider value={{ registerHeading, unregisterHeading }}>
<div>
{classes && (
<ClassTable {...(isValidElement(classes) ? { custom: classes } : classes)} />
)}
{children}
</div>
</ContentsContext.Provider>
{(prev || next) && (
<div className="mt-16 flex font-medium leading-6">
{prev && (
<Link
href={prev.href}
className="mr-8 flex items-center rounded-md border-2 border-blue-500 px-2 py-1 text-gray-500 transition-colors duration-200 hover:text-gray-900 hover:shadow-md dark:border-blue-400 dark:text-gray-400 dark:hover:text-white"
>
<ArrowLeftIcon className="mr-2 h-5 w-5" />
{prev.shortTitle || prev.title}
</Link>
)}
{next && (
<Link
href={next.href}
className="ml-auto flex items-center rounded-md border-2 border-blue-500 px-2 py-1 text-right text-gray-500 transition-colors duration-200 hover:text-gray-900 hover:shadow-md dark:border-blue-400 dark:text-gray-400 dark:hover:text-white"
>
{next.shortTitle || next.title}
<ArrowRightIcon className="ml-2 h-5 w-5" />
</Link>
)}
</div>
)}
<div className="mt-12 border-t border-gray-200 dark:border-gray-700 ">
<Link
href={`https://github.com/javaistic/javaistic/edit/main/src/pages${router.pathname}.mdx`}
className="mt-10 flex items-center text-base font-medium hover:text-gray-900 dark:hover:text-white sm:w-1/3"
>
Edit this page on GitHub
<PencilAltIcon className="ml-2 h-5 w-5" />
</Link>
</div>
</div>
<div className="mr-8 mb-8 hidden w-64 flex-none pl-8 xl:block xl:text-sm">
<div className="sticky top-18 flex max-h-(screen-18) flex-col justify-between overflow-y-auto pt-10 pb-6">
{toc.length > 0 && (
<div className="mb-8">
<TableOfContents tableOfContents={toc} currentSection={currentSection} />
</div>
)}
</div>
</div>
</div>
)
}