Skip to content

fix scroll bug #6125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/components/Page/Page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,38 @@ export default function Page(props) {
const { hash, pathname } = useLocation();

useEffect(() => {
let observer;
if (contentLoaded) {
if (hash) {
const element = document.querySelector(hash);
if (element) {
element.scrollIntoView();
const target = document.querySelector('#md-content');
// two cases here
// 1. server side rendered page, so hash target is already there
if (document.querySelector(hash)) {
document.querySelector(hash).scrollIntoView();
} else {
// 2. dynamic loaded content
// we need to observe the dom change to tell if hash exists
observer = new MutationObserver(() => {
const element = document.querySelector(hash);
if (element) {
element.scrollIntoView();
}
});
observer.observe(target, {
childList: true,
attributes: false,
subtree: false,
});
}
} else {
window.scrollTo(0, 0);
}
}
return () => {
if (observer) {
observer.disconnect();
}
};
}, [contentLoaded, pathname, hash]);

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

{contentRender}
<div id="md-content">{contentRender}</div>

{loadRelated && (
<div className="print:hidden">
Expand Down
48 changes: 33 additions & 15 deletions src/components/Site/Site.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ function Site(props) {
<Route path="app-shell" element={<Fragment />} />
{pages.map((page) => {
let path = page.path.replace('src/content/', '');
let content = props.import(path);
const { previous, next } = getAdjacentPages(
sidebarPages,
page,
Expand All @@ -287,20 +286,15 @@ function Site(props) {
key={page.url}
path={page.url}
element={
<Fragment>
<Sponsors />
<Sidebar
className="site__sidebar"
currentPage={location.pathname}
pages={sidebarPages}
/>
<Page
{...page}
content={content}
previous={previous}
next={next}
/>
</Fragment>
<PageElement
currentPage={location.pathname}
sidebarPages={sidebarPages}
page={page}
next={next}
previous={previous}
import={props.import}
path={path}
/>
}
/>
);
Expand All @@ -315,3 +309,27 @@ function Site(props) {
);
}
export default Site;
PageElement.propTypes = {
currentPage: PropTypes.string,
sidebarPages: PropTypes.array,
previous: PropTypes.object,
next: PropTypes.object,
page: PropTypes.object,
import: PropTypes.func,
path: PropTypes.string,
};
function PageElement(props) {
const { currentPage, sidebarPages, page, previous, next } = props;
const content = props.import(props.path);
return (
<Fragment>
<Sponsors />
<Sidebar
className="site__sidebar"
currentPage={currentPage}
pages={sidebarPages}
/>
<Page {...page} content={content} previous={previous} next={next} />
</Fragment>
);
}
Loading