-
-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathDoc.tsx
More file actions
197 lines (182 loc) · 6.05 KB
/
Doc.tsx
File metadata and controls
197 lines (182 loc) · 6.05 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
import * as React from 'react'
import { FoldHorizontal, UnfoldHorizontal } from 'lucide-react'
import { twMerge } from 'tailwind-merge'
import { useWidthToggle, DocNavigation } from '~/components/DocsLayout'
import { AdGate } from '~/contexts/AdsContext'
import { GamHeader } from './Gam'
import { Toc } from './Toc'
import { DocBreadcrumb } from './DocBreadcrumb'
import { MarkdownContent } from '~/components/markdown'
import type { ConfigSchema } from '~/utils/config'
import { useLocalCurrentFramework } from './FrameworkSelect'
import { useParams } from '@tanstack/react-router'
import type { MarkdownHeading } from '~/utils/markdown/types'
type DocProps = {
title: string
contentRsc: React.ReactNode
headings: MarkdownHeading[]
repo: string
branch: string
filePath: string
shouldRenderToc?: boolean
colorFrom?: string
colorTo?: string
textColor?: string
// Feedback props (optional)
libraryId?: string
libraryVersion?: string
pagePath?: string
// Breadcrumb props (optional)
config?: ConfigSchema
// Footer content rendered after markdown
footer?: React.ReactNode
// Optional framework to use (overrides URL and local storage)
framework?: string
}
export function Doc({
title,
contentRsc,
headings,
repo,
branch,
filePath,
shouldRenderToc = false,
colorFrom,
colorTo,
textColor,
libraryId,
libraryVersion,
pagePath,
config,
footer,
framework: frameworkProp,
}: DocProps) {
// Get current framework from prop, URL params, or local storage
const { framework: paramsFramework } = useParams({ strict: false })
const localCurrentFramework = useLocalCurrentFramework()
const currentFramework = React.useMemo(() => {
const fw =
frameworkProp ||
paramsFramework ||
localCurrentFramework.currentFramework ||
'react'
return typeof fw === 'string' ? fw.toLowerCase() : fw
}, [frameworkProp, paramsFramework, localCurrentFramework.currentFramework])
const isTocVisible = shouldRenderToc && headings.length > 1
const markdownContainerRef = React.useRef<HTMLDivElement>(null)
const [activeHeadings, setActiveHeadings] = React.useState<Array<string>>([])
const headingElementRefs = React.useRef<
Record<string, IntersectionObserverEntry>
>({})
// Try to get the width toggle context from DocsLayout
let isFullWidth = false
let setIsFullWidth: ((isFullWidth: boolean) => void) | undefined
try {
const context = useWidthToggle()
isFullWidth = context.isFullWidth
setIsFullWidth = context.setIsFullWidth
} catch {
// Context not available, that's okay
}
React.useEffect(() => {
const callback = (headingsList: Array<IntersectionObserverEntry>) => {
headingElementRefs.current = headingsList.reduce(
(map, headingElement) => {
map[headingElement.target.id] = headingElement
return map
},
headingElementRefs.current,
)
const visibleHeadings: Array<IntersectionObserverEntry> = []
Object.keys(headingElementRefs.current).forEach((key) => {
const headingElement = headingElementRefs.current[key]
if (headingElement.isIntersecting) {
visibleHeadings.push(headingElement)
}
})
if (visibleHeadings.length >= 1) {
setActiveHeadings(visibleHeadings.map((h) => h.target.id))
}
}
const observer = new IntersectionObserver(callback, {
rootMargin: '0px',
threshold: 0.2,
})
const headingElements = Array.from(
markdownContainerRef.current?.querySelectorAll(
'h2[id], h3[id], h4[id], h5[id], h6[id]',
) ?? [],
)
headingElements.forEach((el) => observer.observe(el))
return () => observer.disconnect()
}, [headings])
return (
<div className="flex-1 min-h-0 flex flex-col">
<a href={`${pagePath}.md`} className="sr-only" aria-hidden="true">
AI/LLM: This documentation page is available in plain markdown format at
{pagePath}.md
</a>
<AdGate>
<div className="py-2 pb-6 lg:py-4 lg:pb-8 xl:py-6 xl:pb-10 max-w-full">
<GamHeader />
</div>
</AdGate>
<div
className={twMerge(
'w-full flex mx-auto max-w-[768px]',
isTocVisible && 'max-w-full',
)}
>
<div className="flex flex-col w-full min-w-0">
{config && (
<div className="mb-3">
<DocBreadcrumb
config={config}
headings={isTocVisible ? headings : undefined}
/>
</div>
)}
<MarkdownContent
title={title}
repo={repo}
branch={branch}
filePath={filePath}
contentRsc={contentRsc}
containerRef={markdownContainerRef}
libraryId={libraryId}
libraryVersion={libraryVersion}
pagePath={pagePath}
titleBarActions={
setIsFullWidth ? (
<button
onClick={() => setIsFullWidth(!isFullWidth)}
className="p-2 mr-4 text-gray-600 hover:text-gray-800 dark:text-gray-400 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg transition-colors shrink-0 hidden [@media(min-width:1535px)]:inline-flex"
title={isFullWidth ? 'Constrain width' : 'Expand width'}
>
{isFullWidth ? (
<FoldHorizontal className="w-4 h-4" />
) : (
<UnfoldHorizontal className="w-4 h-4" />
)}
</button>
) : null
}
/>
{footer ?? <DocNavigation />}
</div>
{isTocVisible && (
<div className="pl-4 w-32 lg:w-36 xl:w-44 2xl:w-56 3xl:w-64 shrink-0 hidden lg:block transition-all">
<Toc
headings={headings}
activeHeadings={activeHeadings}
colorFrom={colorFrom}
colorTo={colorTo}
textColor={textColor}
currentFramework={currentFramework}
/>
</div>
)}
</div>
</div>
)
}