mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 19:51:07 +02:00
feat: add desktop back home button on posts page
This commit is contained in:
parent
b4bd409e4b
commit
cac96eca8c
7 changed files with 54 additions and 16 deletions
|
@ -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",
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
>
|
>
|
||||||
|
|
15
src/components/Widgets/BackHome.astro
Normal file
15
src/components/Widgets/BackHome.astro
Normal 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>
|
|
@ -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>
|
||||||
|
|
|
@ -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,6 +106,9 @@ const { Content, remarkPluginFrontmatter } = await post.render()
|
||||||
supportedLangs={supportedLangs}
|
supportedLangs={supportedLangs}
|
||||||
>
|
>
|
||||||
<article class="heti mb-12.6">
|
<article class="heti mb-12.6">
|
||||||
|
<div class="relative">
|
||||||
|
<!-- Back home -->
|
||||||
|
<BackHome />
|
||||||
<!-- Title -->
|
<!-- Title -->
|
||||||
<h1 class="post-title">
|
<h1 class="post-title">
|
||||||
<span
|
<span
|
||||||
|
@ -113,6 +118,7 @@ const { Content, remarkPluginFrontmatter } = await post.render()
|
||||||
{post.data.title}
|
{post.data.title}
|
||||||
</span>
|
</span>
|
||||||
</h1>
|
</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Date -->
|
<!-- Date -->
|
||||||
<div
|
<div
|
||||||
|
|
|
@ -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';
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue