Skip to content

Commit 02fb7bd

Browse files
authored
fix scroll bug (webpack#6125)
* fix scroll bug * downgrade wds * revert page change * fix anchor issue * revert wds change * disconnect observer when unmount * watch pathname as well
1 parent 3e476d4 commit 02fb7bd

File tree

3 files changed

+173
-118
lines changed

3 files changed

+173
-118
lines changed

src/components/Page/Page.jsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,38 @@ export default function Page(props) {
4848
const { hash, pathname } = useLocation();
4949

5050
useEffect(() => {
51+
let observer;
5152
if (contentLoaded) {
5253
if (hash) {
53-
const element = document.querySelector(hash);
54-
if (element) {
55-
element.scrollIntoView();
54+
const target = document.querySelector('#md-content');
55+
// two cases here
56+
// 1. server side rendered page, so hash target is already there
57+
if (document.querySelector(hash)) {
58+
document.querySelector(hash).scrollIntoView();
59+
} else {
60+
// 2. dynamic loaded content
61+
// we need to observe the dom change to tell if hash exists
62+
observer = new MutationObserver(() => {
63+
const element = document.querySelector(hash);
64+
if (element) {
65+
element.scrollIntoView();
66+
}
67+
});
68+
observer.observe(target, {
69+
childList: true,
70+
attributes: false,
71+
subtree: false,
72+
});
5673
}
5774
} else {
5875
window.scrollTo(0, 0);
5976
}
6077
}
78+
return () => {
79+
if (observer) {
80+
observer.disconnect();
81+
}
82+
};
6183
}, [contentLoaded, pathname, hash]);
6284

6385
const numberOfContributors = contributors.length;
@@ -102,7 +124,7 @@ export default function Page(props) {
102124
</div>
103125
) : null}
104126

105-
{contentRender}
127+
<div id="md-content">{contentRender}</div>
106128

107129
{loadRelated && (
108130
<div className="print:hidden">

src/components/Site/Site.jsx

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ function Site(props) {
276276
<Route path="app-shell" element={<Fragment />} />
277277
{pages.map((page) => {
278278
let path = page.path.replace('src/content/', '');
279-
let content = props.import(path);
280279
const { previous, next } = getAdjacentPages(
281280
sidebarPages,
282281
page,
@@ -287,20 +286,15 @@ function Site(props) {
287286
key={page.url}
288287
path={page.url}
289288
element={
290-
<Fragment>
291-
<Sponsors />
292-
<Sidebar
293-
className="site__sidebar"
294-
currentPage={location.pathname}
295-
pages={sidebarPages}
296-
/>
297-
<Page
298-
{...page}
299-
content={content}
300-
previous={previous}
301-
next={next}
302-
/>
303-
</Fragment>
289+
<PageElement
290+
currentPage={location.pathname}
291+
sidebarPages={sidebarPages}
292+
page={page}
293+
next={next}
294+
previous={previous}
295+
import={props.import}
296+
path={path}
297+
/>
304298
}
305299
/>
306300
);
@@ -315,3 +309,27 @@ function Site(props) {
315309
);
316310
}
317311
export default Site;
312+
PageElement.propTypes = {
313+
currentPage: PropTypes.string,
314+
sidebarPages: PropTypes.array,
315+
previous: PropTypes.object,
316+
next: PropTypes.object,
317+
page: PropTypes.object,
318+
import: PropTypes.func,
319+
path: PropTypes.string,
320+
};
321+
function PageElement(props) {
322+
const { currentPage, sidebarPages, page, previous, next } = props;
323+
const content = props.import(props.path);
324+
return (
325+
<Fragment>
326+
<Sponsors />
327+
<Sidebar
328+
className="site__sidebar"
329+
currentPage={currentPage}
330+
pages={sidebarPages}
331+
/>
332+
<Page {...page} content={content} previous={previous} next={next} />
333+
</Fragment>
334+
);
335+
}

0 commit comments

Comments
 (0)