Skip to content

Commit 9ddc478

Browse files
committed
fix(web): keep title in sync with iframe
1 parent 50543bc commit 9ddc478

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

web/src/components/IFrame.tsx

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { uniqueId } from 'lodash'
44
import styles from '../style/components/IFrame.module.css'
55
interface Props {
66
src: string
7-
onPageChanged: (page: string, hash: string) => void
7+
onPageChanged: (page: string, hash: string, title?: string) => void
88
onHashChanged: (hash: string) => void
9+
onTitleChanged: (title: string) => void
910
onNotFound: () => void
1011
}
1112

@@ -69,6 +70,10 @@ export default function IFrame(props: Props): JSX.Element {
6970
'hashchange',
7071
hashChangeEventListener
7172
)
73+
iFrameRef.current.contentWindow?.addEventListener(
74+
'titlechange',
75+
titleChangeEventListener
76+
)
7277

7378
const parts = url.split('/doc/').slice(1).join('/doc/').split('/')
7479
const urlPageAndHash = parts.slice(2).join('/')
@@ -77,8 +82,9 @@ export default function IFrame(props: Props): JSX.Element {
7782
: urlPageAndHash.length
7883
const urlPage = urlPageAndHash.slice(0, hashIndex)
7984
const urlHash = urlPageAndHash.slice(hashIndex)
85+
const title = iFrameRef.current?.contentDocument?.title
8086

81-
props.onPageChanged(urlPage, urlHash)
87+
props.onPageChanged(urlPage, urlHash, title)
8288
}
8389

8490
const hashChangeEventListener = (): void => {
@@ -102,6 +108,20 @@ export default function IFrame(props: Props): JSX.Element {
102108
props.onHashChanged(hash)
103109
}
104110

111+
const titleChangeEventListener = (): void => {
112+
if (iFrameRef.current == null) {
113+
console.error('titleChangeEvent from iframe but iFrameRef is null')
114+
return
115+
}
116+
117+
const title = iFrameRef.current?.contentDocument?.title
118+
if (title == null) {
119+
return
120+
}
121+
122+
props.onTitleChanged(title)
123+
}
124+
105125
return (
106126
<iframe
107127
ref={iFrameRef}

web/src/pages/Docs.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,14 @@ export default function Docs(): JSX.Element {
117117
window.history.pushState(null, '', url)
118118
}
119119

120-
const iFramePageChanged = (urlPage: string, urlHash: string): void => {
120+
const updateTitle = (newTitle: string): void => {
121+
document.title = newTitle
122+
}
123+
124+
const iFramePageChanged = (urlPage: string, urlHash: string, title?: string): void => {
125+
if (title != null && title !== document.title) {
126+
updateTitle(title)
127+
}
121128
if (urlPage === page.current) {
122129
return
123130
}
@@ -225,6 +232,7 @@ export default function Docs(): JSX.Element {
225232
src={iFrameSrc}
226233
onPageChanged={iFramePageChanged}
227234
onHashChanged={iFrameHashChanged}
235+
onTitleChanged={updateTitle}
228236
onNotFound={iFrameNotFound}
229237
/>
230238
{!hideUi && (

0 commit comments

Comments
 (0)