mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-15 19:22:52 +02:00
feat: add language switcher component and update layout
- Introduced a new LanguageSwitcher component to facilitate language switching in the application. - Added an SVG icon for the language switcher button. - Updated the Header component to adjust margin for better spacing. - Modified the Navigation component's margin and top properties for improved layout. - Adjusted the ThemeToggle button size for consistency with the new design.
This commit is contained in:
parent
23b819319d
commit
b46534419b
6 changed files with 66 additions and 3 deletions
1
public/image/languageSwitcher.svg
Normal file
1
public/image/languageSwitcher.svg
Normal file
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 21 12.3 2h-1L4.7 21l-2.5.2v.8h6.3v-.8L5.7 21l2-5.9h7.5l2 5.9-3.3.2v.8h7.9v-.8zM8 14.3l3.4-10.1 3.5 10.1z" data-name="图层_1" style="fill:#3d3d3d"/></svg>
|
After Width: | Height: | Size: 228 B |
|
@ -14,7 +14,7 @@ const marginClass = {
|
|||
|
||||
<header>
|
||||
<h1
|
||||
class={`${marginClass} mt--1.6 text-12 c-primary font-bold font-title`}
|
||||
class={`${marginClass} mt--3.2 text-12 c-primary font-bold font-title`}
|
||||
aria-label="Title Tag"
|
||||
>
|
||||
<a href="/">
|
||||
|
|
60
src/components/LanguageSwitcher.astro
Normal file
60
src/components/LanguageSwitcher.astro
Normal file
|
@ -0,0 +1,60 @@
|
|||
---
|
||||
import themeConfig from '@/config'
|
||||
|
||||
const langs = ['', ...themeConfig.global.moreLocale]
|
||||
---
|
||||
|
||||
<script is:inline define:vars={{ langs }}>
|
||||
const langSwitch = document.getElementById('language-switcher')
|
||||
|
||||
langSwitch?.addEventListener('click', () => {
|
||||
const { pathname, search, hash } = window.location
|
||||
|
||||
// 获取当前语言和路径段
|
||||
const segments = pathname.split('/').filter(Boolean)
|
||||
const firstSegment = segments[0] || ''
|
||||
const currentLang = langs.includes(firstSegment) ? firstSegment : ''
|
||||
|
||||
// 获取下一个语言
|
||||
const currentIndex = langs.indexOf(currentLang)
|
||||
const nextLang = langs[(currentIndex + 1) % langs.length]
|
||||
|
||||
// 构建新的 URL
|
||||
let newPath
|
||||
if (currentLang) {
|
||||
// 如果当前有语言前缀,替换它
|
||||
segments[0] = nextLang || segments[0]
|
||||
newPath = nextLang ? `/${segments.join('/')}` : `/${segments.slice(1).join('/')}`
|
||||
}
|
||||
else {
|
||||
// 如果当前没有语言前缀
|
||||
newPath = nextLang ? `/${nextLang}${pathname}` : pathname
|
||||
}
|
||||
|
||||
// 确保路径至少是 "/"
|
||||
newPath = newPath || '/'
|
||||
|
||||
// 切换语言
|
||||
window.location.href = `${newPath}${search}${hash}`
|
||||
})
|
||||
</script>
|
||||
|
||||
<button
|
||||
id="language-switcher"
|
||||
class="absolute right-25.6 top-19.4 z-999 aspect-square w-7 c-secondary active:scale-92"
|
||||
aria-label="Language Switch Button"
|
||||
title="Language Switch Button"
|
||||
>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="h-full w-full">
|
||||
<path d="M19 21 12.3 2h-1L4.7 21l-2.5.2v.8h6.3v-.8L5.7 21l2-5.9h7.5l2 5.9-3.3.2v.8h7.9v-.8zM8 14.3l3.4-10.1 3.5 10.1z" fill="currentColor" />
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
<style>
|
||||
#language-switcher {
|
||||
transition: transform 0.3s ease;
|
||||
}
|
||||
#language-switcher:hover {
|
||||
transform: scale(1.1);
|
||||
}
|
||||
</style>
|
|
@ -51,7 +51,7 @@ const isTagActive = isTagPage(currentPath)
|
|||
const isAboutActive = isAboutPage(currentPath)
|
||||
---
|
||||
|
||||
<nav class="mb-18 mt-10 text-5.6 text-secondary font-semibold leading-11.4 font-navbar">
|
||||
<nav class="mb-19.4 mt-12 text-5.6 text-secondary font-semibold leading-13 font-navbar">
|
||||
<ul>
|
||||
<li>
|
||||
<a
|
||||
|
|
|
@ -46,7 +46,7 @@ window.addEventListener('pageshow', (event) => {
|
|||
<button
|
||||
aria-pressed="false"
|
||||
aria-label="Theme Toggle Button"
|
||||
class="absolute right-9.6 top-21 z-999 aspect-square w-8 c-secondary active:scale-92"
|
||||
class="absolute right-9.6 top-19.4 z-999 aspect-square w-7 c-secondary active:scale-92"
|
||||
>
|
||||
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" fill="currentColor">
|
||||
<path d="m12 1c-6.1 0-11 4.9-11 11s4.9 11 11 11 11-4.9 11-11-4.9-11-11-11m0 20c-5.8 0-10.5-4-10.5-9s4.7-9 10.5-9 10.5 4 10.5 9-4.7 9-10.5 9" />
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
---
|
||||
import Footer from '@/components/Footer.astro'
|
||||
import Header from '@/components/Header.astro'
|
||||
import LanguageSwitcher from '@/components/LanguageSwitcher.astro'
|
||||
import Navigation from '@/components/Navigation.astro'
|
||||
import PhotoSwipe from '@/components/PhotoSwipe.astro'
|
||||
import Scrollbar from '@/components/Scrollbar.astro'
|
||||
|
@ -44,6 +45,7 @@ const fontStyle = `font-${themeConfig.global.font}`
|
|||
|
||||
</div>
|
||||
<ThemeToggle />
|
||||
<LanguageSwitcher />
|
||||
<Scrollbar />
|
||||
<PhotoSwipe />
|
||||
</body>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue