Skip to content

Commit b1584a1

Browse files
committed
Fix: Docs blinking when navigating between hashes
1 parent 8f88f84 commit b1584a1

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

web/src/pages/Docs.tsx

+18-22
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
the iFrameRef is not really compatiple with ts,
44
*/
55

6-
import React, { useEffect, useRef, useState } from 'react'
6+
import React, { useEffect, useRef, useMemo, useState } from 'react'
77
import { useLocation, useParams, useSearchParams } from 'react-router-dom'
88
import DocumentControlButtons from '../components/DocumentControlButtons'
99
import ProjectDetails from '../models/ProjectDetails'
@@ -36,22 +36,29 @@ export default function Docs (): JSX.Element {
3636
const location = useLocation()
3737
const iFrameRef = useRef<HTMLIFrameElement>(null)
3838

39+
const iFrame = useMemo(() => {
40+
return (<iframe
41+
ref={iFrameRef}
42+
key={uniqueId()}
43+
src={ProjectRepository.getProjectDocsURL(project, version, page, hash)}
44+
title="docs"
45+
className={styles['docs-iframe']}
46+
onLoad={() => {
47+
// @ts-expect-error ts can't find contentWindow
48+
onIFrameLocationChanged(iFrameRef.current?.contentWindow.location.href)
49+
}}
50+
/>
51+
)
52+
}, [project, version, page])
53+
3954
document.title = `${project} | docat`
4055

4156
if (projectParam === '') {
4257
setLoadingFailed(true)
4358
}
4459

4560
const updateURL = (newProject: string, newVersion: string, newPage: string, newHash: string, newHideUi: boolean): void => {
46-
let url = `#/${newProject}/${newVersion}/${newPage}`
47-
48-
if (newHash.length > 0) {
49-
url += newHash
50-
}
51-
52-
if (newHideUi) {
53-
url += '?hide-ui=true'
54-
}
61+
const url = `#/${newProject}/${newVersion}/${newPage}${newHash}${newHideUi ? '?hide-ui=true' : ''}`
5562

5663
if (project === newProject && version === newVersion && page === newPage && hash === newHash && hideUi === newHideUi) {
5764
// no change
@@ -217,18 +224,7 @@ export default function Docs (): JSX.Element {
217224

218225
return (
219226
<>
220-
<iframe
221-
ref={iFrameRef}
222-
key={uniqueId()}
223-
src={ProjectRepository.getProjectDocsURL(project, version, page, hash)}
224-
title="docs"
225-
className={styles['docs-iframe']}
226-
onLoad={() => {
227-
// @ts-expect-error ts can't find contentWindow
228-
onIFrameLocationChanged(iFrameRef.current?.contentWindow.location.href)
229-
}}
230-
/>
231-
227+
{iFrame}
232228
{!hideUi && (
233229
<DocumentControlButtons
234230
version={version}

0 commit comments

Comments
 (0)