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

@ -2,7 +2,7 @@
"name": "astro-theme-retypeset", "name": "astro-theme-retypeset",
"type": "module", "type": "module",
"version": "0.0.1", "version": "0.0.1",
"packageManager": "pnpm@10.6.3", "packageManager": "pnpm@10.6.4",
"scripts": { "scripts": {
"dev": "astro dev", "dev": "astro dev",
"build": "astro build", "build": "astro build",

View file

@ -2,8 +2,7 @@
import { ui } from '@/i18n/ui' import { ui } from '@/i18n/ui'
import { getPageInfo } from '@/utils/page' import { getPageInfo } from '@/utils/page'
const { currentLang, isHome, isPost, isTag, isAbout, getLocalizedPath } const { currentLang, isHome, isPost, isTag, isAbout, getLocalizedPath } = getPageInfo(Astro.url.pathname)
= getPageInfo(Astro.url.pathname)
const currentUI = ui[currentLang as keyof typeof ui] const currentUI = ui[currentLang as keyof typeof ui]
const isPostActive = isHome || isPost const isPostActive = isHome || isPost

View file

@ -46,7 +46,7 @@ function getPostPath(post: Post) {
{/* mobile post time */} {/* mobile post time */}
<div <div
class="text-3.5 leading-6.875 font-time lg:(hidden)" class="text-3.5 leading-6.875 font-time lg:hidden"
transition:name={`time-${post.data.abbrlink || post.slug}-${lang}`} transition:name={`time-${post.data.abbrlink || post.slug}-${lang}`}
data-disable-transition-on-theme data-disable-transition-on-theme
> >

View file

@ -0,0 +1,15 @@
<button
id="back-button"
class="hidden"
lg="block absolute left--8 top-1/2 aspect-square w-4.5 translate-y--1/2 c-secondary active:scale-90 hover:c-primary"
aria-label="Back to home"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
aria-hidden="true"
fill="currentColor"
>
<path d="M16.6 1.9 4.3 12l12.3 10.1.3 1.6h.8l-.5-5.5h-.8l.1 2.5L6.6 12l9.9-8.7-.1 2.5h.8l.5-5.5h-.8z"></path>
</svg>
</button>

View file

@ -24,7 +24,8 @@ interface Props {
} }
const { postTitle, postDescription, postSlug, supportedLangs = [] } = Astro.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 { light: { background: lightMode }, dark: { background: darkMode } } = themeConfig.color
const fontStyle = themeConfig.global.fontStyle === 'serif' ? 'font-serif' : 'font-sans' const fontStyle = themeConfig.global.fontStyle === 'serif' ? 'font-serif' : 'font-sans'
const mobileFooterMargin = isPost && themeConfig.comment?.waline?.serverURL const mobileFooterMargin = isPost && themeConfig.comment?.waline?.serverURL
@ -149,5 +150,22 @@ const mobileFooterMargin = isPost && themeConfig.comment?.waline?.serverURL
document.addEventListener('astro:after-swap', setupThemeToggle) document.addEventListener('astro:after-swap', setupThemeToggle)
</script> </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> </body>
</html> </html>

View file

@ -2,6 +2,7 @@
import type { CollectionEntry } from 'astro:content' import type { CollectionEntry } from 'astro:content'
import Comments from '@/components/Comments/index.astro' import Comments from '@/components/Comments/index.astro'
import PostDate from '@/components/PostDate.astro' import PostDate from '@/components/PostDate.astro'
import BackHome from '@/components/Widgets/BackHome.astro'
import { allLocales, defaultLocale, moreLocales } from '@/config' import { allLocales, defaultLocale, moreLocales } from '@/config'
import { getTagPath } from '@/i18n/path' import { getTagPath } from '@/i18n/path'
import Layout from '@/layouts/Layout.astro' import Layout from '@/layouts/Layout.astro'
@ -18,6 +19,7 @@ export async function getStaticPaths() {
throw new Error(`Duplicate post slugs:\n${duplicates.join('\n')}`) throw new Error(`Duplicate post slugs:\n${duplicates.join('\n')}`)
} }
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Use a Map to store the relationship between post slugs and their supported languages // Use a Map to store the relationship between post slugs and their supported languages
// Set is used to store the supported languages for each post // Set is used to store the supported languages for each post
const slugToLangsMap = posts.reduce((map, post) => { const slugToLangsMap = posts.reduce((map, post) => {
@ -43,7 +45,7 @@ export async function getStaticPaths() {
]), ]),
) )
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
type PathItem = { type PathItem = {
params: { posts_slug: string } params: { posts_slug: string }
props: { post: any, lang: string, supportedLangs: string[] } props: { post: any, lang: string, supportedLangs: string[] }
@ -104,15 +106,19 @@ const { Content, remarkPluginFrontmatter } = await post.render()
supportedLangs={supportedLangs} supportedLangs={supportedLangs}
> >
<article class="heti mb-12.6"> <article class="heti mb-12.6">
<!-- Title --> <div class="relative">
<h1 class="post-title"> <!-- Back home -->
<span <BackHome />
transition:name={`post-${post.data.abbrlink || post.slug}-${lang}`} <!-- Title -->
data-disable-transition-on-theme <h1 class="post-title">
> <span
{post.data.title} transition:name={`post-${post.data.abbrlink || post.slug}-${lang}`}
</span> data-disable-transition-on-theme
</h1> >
{post.data.title}
</span>
</h1>
</div>
<!-- Date --> <!-- Date -->
<div <div

View file

@ -121,7 +121,7 @@
font-weight:600 font-weight:600
} }
.heti .post-title { .heti .post-title {
--at-apply: 'c-primary mb-2 font-bold text-9 leading-12 text-balance leading-12'; --at-apply: 'c-primary mb-2 font-bold text-9 leading-12 text-balance lg:mbs-0';
} }
.heti h1 { .heti h1 {
--at-apply: 'mb-6'; --at-apply: 'mb-6';