-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 fix: Auto scroll to top when change route
- Loading branch information
1 parent
5159619
commit c0e9354
Showing
9 changed files
with
239 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { Layout } from '@lobehub/ui'; | ||
import { useResponsive, useTheme } from 'antd-style'; | ||
import { Helmet, useIntl, useLocation } from 'dumi'; | ||
import isEqual from 'fast-deep-equal'; | ||
import { memo, useCallback, useEffect } from 'react'; | ||
import { shallow } from 'zustand/shallow'; | ||
|
||
import Changelog from '@/pages/Changelog'; | ||
import Docs from '@/pages/Docs'; | ||
import Home from '@/pages/Home'; | ||
import Footer from '@/slots/Footer'; | ||
import Header from '@/slots/Header'; | ||
import Sidebar from '@/slots/Sidebar'; | ||
import Toc from '@/slots/Toc'; | ||
import { isHeroPageSel, siteTitleSel, tocAnchorItemSel, useSiteStore } from '@/store'; | ||
|
||
const DocumentLayout = memo(() => { | ||
const intl = useIntl(); | ||
const { hash } = useLocation(); | ||
const theme = useTheme(); | ||
const { mobile, laptop } = useResponsive(); | ||
|
||
const { loading, page, siteTitle, noToc } = useSiteStore((s) => { | ||
const isChanlogPage = s.location.pathname === '/changelog'; | ||
const isHomePage = isHeroPageSel(s); | ||
let page; | ||
|
||
if (isHomePage) { | ||
page = 'home'; | ||
} else if (isChanlogPage) { | ||
page = 'changelog'; | ||
} else { | ||
page = 'docs'; | ||
} | ||
|
||
return { | ||
loading: s.siteData.loading, | ||
noToc: tocAnchorItemSel(s).length === 0, | ||
page: page, | ||
siteTitle: siteTitleSel(s), | ||
}; | ||
}, shallow); | ||
|
||
const fm = useSiteStore((s) => s.routeMeta.frontmatter, isEqual); | ||
|
||
const hideSidebar = page !== 'docs' || mobile || fm.sidebar === false; | ||
const shouldHideToc = fm.toc === false || noToc; | ||
const hideToc = mobile ? shouldHideToc : !laptop || shouldHideToc; | ||
|
||
const HelmetBlock = useCallback( | ||
() => ( | ||
<Helmet> | ||
<html lang={intl.locale.replace(/-.+$/, '')} /> | ||
{fm.title && <meta content={fm.title} property="og:title" />} | ||
{fm.description && <meta content={fm.description} name="description" />} | ||
{fm.description && <meta content={fm.description} property="og:description" />} | ||
{fm.keywords && <meta content={fm.keywords.join(',')} name="keywords" />} | ||
{fm.keywords && <meta content={fm.keywords.join(',')} property="og:keywords" />} | ||
{!fm.title || page === 'home' ? ( | ||
<title>{siteTitle}</title> | ||
) : ( | ||
<title> | ||
{fm.title} - {siteTitle} | ||
</title> | ||
)} | ||
</Helmet> | ||
), | ||
[intl, fm, siteTitle, page], | ||
); | ||
|
||
// handle hash change or visit page hash after async chunk loaded | ||
useEffect(() => { | ||
const id = hash.replace('#', ''); | ||
|
||
if (!id) return; | ||
setTimeout(() => { | ||
const elm = document.querySelector(`#${decodeURIComponent(id)}`); | ||
if (elm) { | ||
elm.scrollIntoView(); | ||
window.scrollBy({ top: -80 }); | ||
} | ||
}, 1); | ||
}, [loading, hash]); | ||
|
||
useEffect(() => { | ||
document.body.scrollTo(0, 0); | ||
}, [siteTitle]); | ||
|
||
return ( | ||
<> | ||
<HelmetBlock /> | ||
<Layout | ||
asideWidth={theme.sidebarWidth} | ||
footer={<Footer />} | ||
header={<Header />} | ||
headerHeight={mobile && page !== 'home' ? theme.headerHeight + 36 : theme.headerHeight} | ||
sidebar={hideSidebar ? undefined : <Sidebar />} | ||
toc={hideToc ? undefined : <Toc />} | ||
tocWidth={hideToc ? 0 : theme.tocWidth} | ||
> | ||
{page === 'home' && <Home />} | ||
{page === 'changelog' && <Changelog />} | ||
{page === 'docs' && <Docs />} | ||
</Layout> | ||
</> | ||
); | ||
}); | ||
|
||
export default DocumentLayout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.