feat: 暗黑主题

This commit is contained in:
2026-07-13 00:04:11 +08:00
parent 0b9c253402
commit 36b04537b4
8 changed files with 1424 additions and 42 deletions

View File

@@ -1,6 +1,18 @@
---
---
<footer>
<div class="content">
<a target="_blank" href="https://beian.miit.gov.cn/#/Integrated/index">
备案号鄂ICP备2023000060号-2
</a>
</div>
</footer>
<style>
footer {
background-color: #ffffff;
background: #ffffff;
height: 70px;
overflow: hidden;
flex-shrink: 0;
@@ -17,12 +29,13 @@
font-size: 14px;
justify-content: center;
}
</style>
<footer>
<div class="content">
<a target="_blank" href="https://beian.miit.gov.cn/#/Integrated/index">
备案号鄂ICP备2023000060号-2
</a>
</div>
</footer>
:global(.dark) footer {
background: black;
box-shadow: rgba(255, 255, 255, .3) 0px -1px 3px 0px;
}
:global(.dark) .content {
color: #ffffff;
}
</style>

View File

@@ -1,7 +1,27 @@
---
import { metadata } from "../consts";
import {metadata} from "../consts";
import ThemeIcon from './ThemeIcon.astro'
---
<header>
<nav class="main-nav">
<h1>
<a href="/">{metadata.siteTitle}</a>
</h1>
<div class="main-nav-right">
<ul class="menu">
<li class="menu-item"><a href="/">首页</a></li>
<!--<li class="menu-item"><a href="/">文章</a></li>-->
<!--<li class="menu-item"><a href="/">分类</a></li>-->
<!--<li class="menu-item"><a href="/">关于</a></li>-->
</ul>
<ThemeIcon/>
</div>
</nav>
</header>
<style>
header {
height: 60px;
@@ -23,8 +43,14 @@ import { metadata } from "../consts";
justify-content: space-between;
}
.main-nav-right {
display: flex;
align-items: center;
gap: 12px;
}
@media (max-width: 768px) {
.main-nav{
.main-nav {
padding: 16px;
}
}
@@ -33,6 +59,7 @@ import { metadata } from "../consts";
user-select: none;
cursor: pointer;
font-size: 24px;
color: #000000;
}
.main-nav .menu {
@@ -43,7 +70,7 @@ import { metadata } from "../consts";
align-items: center;
}
.menu-item{
.menu-item {
text-align: center;
}
@@ -54,18 +81,17 @@ import { metadata } from "../consts";
.main-nav .menu-item a:hover {
color: #64748b;
}
</style>
<header>
<nav class="main-nav">
<h1>
<a href="/">{metadata.siteTitle}</a>
</h1>
<ul class="menu">
<li class="menu-item"><a href="/">首页</a></li>
<!--<li class="menu-item"><a href="/">文章</a></li>-->
<li class="menu-item"><a href="/">分类</a></li>
<!--<li class="menu-item"><a href="/">关于</a></li>-->
</ul>
</nav>
</header>
:global(.dark) header {
background: #000000;
box-shadow: rgba(255, 255, 255, .3) 0px 1px 2px 0px;
}
:global(.dark) .main-nav h1 {
color: #ffffff;
}
:global(.dark) .main-nav .menu-item a:hover {
color: rgba(255, 255, 255, .8);
}
</style>

View File

@@ -60,13 +60,13 @@ posts.map((post) => {
}
.post {
background: #fff;
padding: 16px;
border-radius: 12px;
box-shadow: rgba(0, 0, 0, 0.05) 0px 1px 2px 0px;
display: flex;
align-items: center;
transition: transform 0.2s linear;
background: #fff;
}
.post:hover {
@@ -150,5 +150,15 @@ posts.map((post) => {
}
}
:global(.dark) .post {
background: #1f2937;
}
:global(.dark) .published-at {
color: #ffffff;
}
:global(.dark) .category-name {
background: #374151;
}
</style>

View File

@@ -0,0 +1,70 @@
---
---
<div class="theme-toggle" id="themeToggle" aria-label="Toggle theme">
<svg aria-hidden="true" width="20px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path class="sun" fill-rule="evenodd"
d="M12 17.5a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zm0 1.5a7 7 0 1 0 0-14 7 7 0 0 0 0 14zm12-7a.8.8 0 0 1-.8.8h-2.4a.8.8 0 0 1 0-1.6h2.4a.8.8 0 0 1 .8.8zM4 12a.8.8 0 0 1-.8.8H.8a.8.8 0 0 1 0-1.6h2.5a.8.8 0 0 1 .8.8zm16.5-8.5a.8.8 0 0 1 0 1l-1.8 1.8a.8.8 0 0 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM6.3 17.7a.8.8 0 0 1 0 1l-1.7 1.8a.8.8 0 1 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM12 0a.8.8 0 0 1 .8.8v2.5a.8.8 0 0 1-1.6 0V.8A.8.8 0 0 1 12 0zm0 20a.8.8 0 0 1 .8.8v2.4a.8.8 0 0 1-1.6 0v-2.4a.8.8 0 0 1 .8-.8zM3.5 3.5a.8.8 0 0 1 1 0l1.8 1.8a.8.8 0 1 1-1 1L3.5 4.6a.8.8 0 0 1 0-1zm14.2 14.2a.8.8 0 0 1 1 0l1.8 1.7a.8.8 0 0 1-1 1l-1.8-1.7a.8.8 0 0 1 0-1z"/>
<path class="moon" fill-rule="evenodd"
d="M16.5 6A10.5 10.5 0 0 1 4.7 16.4 8.5 8.5 0 1 0 16.4 4.7l.1 1.3zm-1.7-2a9 9 0 0 1 .2 2 9 9 0 0 1-11 8.8 9.4 9.4 0 0 1-.8-.3c-.4 0-.8.3-.7.7a10 10 0 0 0 .3.8 10 10 0 0 0 9.2 6 10 10 0 0 0 4-19.2 9.7 9.7 0 0 0-.9-.3c-.3-.1-.7.3-.6.7a9 9 0 0 1 .3.8z"/>
</svg>
</div>
<style>
#themeToggle {
border: 0;
background: none;
user-select: none;
}
.theme-toggle {
display: flex;
align-items: center;
}
.sun {
fill: black;
}
.moon {
fill: transparent;
}
:global(.dark) .sun {
fill: transparent;
}
:global(.dark) .moon {
fill: white;
}
</style>
<script is:inline>
const theme = (() => {
const localStorageTheme = localStorage?.getItem("theme") ?? '';
if (['dark', 'light'].includes(localStorageTheme)) {
return localStorageTheme;
}
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
return 'dark';
}
return 'light';
})();
if (theme === 'light') {
document.documentElement.classList.remove('dark');
} else {
document.documentElement.classList.add('dark');
}
window.localStorage.setItem('theme', theme);
const handleToggleClick = () => {
const element = document.documentElement;
element.classList.toggle("dark");
const isDark = element.classList.contains("dark");
localStorage.setItem("theme", isDark ? "dark" : "light");
}
document.getElementById("themeToggle")?.addEventListener("click", handleToggleClick);
</script>