Skip to content

Commit c14824d

Browse files
authored
Merge pull request #587 from docat-org/fix/docs-blinking-when-changing-hash
Fix: Docs blinking when navigating between hashes or pages
2 parents 8f88f84 + 55e3b2e commit c14824d

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

web/src/pages/Docs.tsx

+23-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,34 @@ 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+
if (iFrameRef.current == null) {
48+
console.error('iFrameRef is null')
49+
return
50+
}
51+
52+
// @ts-expect-error ts can't find contentWindow
53+
onIFrameLocationChanged(iFrameRef.current.contentWindow.location.href)
54+
}}
55+
/>
56+
)
57+
}, [project, version])
58+
3959
document.title = `${project} | docat`
4060

4161
if (projectParam === '') {
4262
setLoadingFailed(true)
4363
}
4464

4565
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-
}
66+
const url = `#/${newProject}/${newVersion}/${newPage}${newHash}${newHideUi ? '?hide-ui=true' : ''}`
5567

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

218230
return (
219231
<>
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-
232+
{iFrame}
232233
{!hideUi && (
233234
<DocumentControlButtons
234235
version={version}

0 commit comments

Comments
 (0)