Skip to content

Commit

Permalink
Merge pull request #587 from docat-org/fix/docs-blinking-when-changin…
Browse files Browse the repository at this point in the history
…g-hash

Fix: Docs blinking when navigating between hashes or pages
  • Loading branch information
reglim authored Aug 30, 2023
2 parents 8f88f84 + 55e3b2e commit c14824d
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions web/src/pages/Docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
the iFrameRef is not really compatiple with ts,
*/

import React, { useEffect, useRef, useState } from 'react'
import React, { useEffect, useRef, useMemo, useState } from 'react'
import { useLocation, useParams, useSearchParams } from 'react-router-dom'
import DocumentControlButtons from '../components/DocumentControlButtons'
import ProjectDetails from '../models/ProjectDetails'
Expand Down Expand Up @@ -36,22 +36,34 @@ export default function Docs (): JSX.Element {
const location = useLocation()
const iFrameRef = useRef<HTMLIFrameElement>(null)

const iFrame = useMemo(() => {
return (<iframe
ref={iFrameRef}
key={uniqueId()}
src={ProjectRepository.getProjectDocsURL(project, version, page, hash)}
title="docs"
className={styles['docs-iframe']}
onLoad={() => {
if (iFrameRef.current == null) {
console.error('iFrameRef is null')
return
}

// @ts-expect-error ts can't find contentWindow
onIFrameLocationChanged(iFrameRef.current.contentWindow.location.href)
}}
/>
)
}, [project, version])

document.title = `${project} | docat`

if (projectParam === '') {
setLoadingFailed(true)
}

const updateURL = (newProject: string, newVersion: string, newPage: string, newHash: string, newHideUi: boolean): void => {
let url = `#/${newProject}/${newVersion}/${newPage}`

if (newHash.length > 0) {
url += newHash
}

if (newHideUi) {
url += '?hide-ui=true'
}
const url = `#/${newProject}/${newVersion}/${newPage}${newHash}${newHideUi ? '?hide-ui=true' : ''}`

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

return (
<>
<iframe
ref={iFrameRef}
key={uniqueId()}
src={ProjectRepository.getProjectDocsURL(project, version, page, hash)}
title="docs"
className={styles['docs-iframe']}
onLoad={() => {
// @ts-expect-error ts can't find contentWindow
onIFrameLocationChanged(iFrameRef.current?.contentWindow.location.href)
}}
/>

{iFrame}
{!hideUi && (
<DocumentControlButtons
version={version}
Expand Down

0 comments on commit c14824d

Please sign in to comment.