feat: add desktop back home button on posts page

This commit is contained in:
radishzzz 2025-03-18 05:37:33 +00:00
parent b4bd409e4b
commit cac96eca8c
7 changed files with 54 additions and 16 deletions

View file

@ -24,7 +24,8 @@ interface Props {
}
const { postTitle, postDescription, postSlug, supportedLangs = [] } = Astro.props
const { isHome, isPost } = getPageInfo(Astro.url.pathname)
const { isHome, isPost, getLocalizedPath } = getPageInfo(Astro.url.pathname)
const localizedHome = getLocalizedPath('/')
const { light: { background: lightMode }, dark: { background: darkMode } } = themeConfig.color
const fontStyle = themeConfig.global.fontStyle === 'serif' ? 'font-serif' : 'font-sans'
const mobileFooterMargin = isPost && themeConfig.comment?.waline?.serverURL
@ -149,5 +150,22 @@ const mobileFooterMargin = isPost && themeConfig.comment?.waline?.serverURL
document.addEventListener('astro:after-swap', setupThemeToggle)
</script>
<!-- Back home >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
<script define:vars={{ localizedHome }}>
function setupBackButton() {
document.getElementById('back-button')?.addEventListener('click', () => {
if (document.referrer) {
window.history.back()
}
else {
window.location.href = localizedHome
}
})
}
setupBackButton()
document.addEventListener('astro:after-swap', setupBackButton)
</script>
</body>
</html>