Skip to content
Go back

自定义 AstroPaper 主题配色方案

Updated:
Edit page

本文将解释如何为网站启用/禁用浅色和深色模式。此外,您还将学习如何自定义整个网站的配色方案。

目录

启用/禁用浅色和深色模式

AstroPaper 主题默认包含浅色和深色模式。换句话说,将有两种配色方案_一种用于浅色模式,另一种用于深色模式。可以在 SITE 配置对象中禁用此默认行为。

export const SITE = {
  website: "https://astro-paper.pages.dev/", // 将其替换为您部署的域名
  author: "Sat Naing",
  profile: "https://satnaing.dev/",
  desc: "A minimal, responsive and SEO-friendly Astro blog theme.",
  title: "AstroPaper",
  ogImage: "astropaper-og.jpg",
  lightAndDarkMode: true,
  postPerIndex: 4,
  postPerPage: 4,
  scheduledPostMargin: 15 * 60 * 1000, // 15 分钟
  showArchives: true,
  showBackButton: true, // 在文章详情页显示返回按钮
  editPost: {
    enabled: true,
    text: "Suggest Changes",
    url: "https://github.com/satnaing/astro-paper/edit/main/",
  },
  dynamicOgImage: true,
  lang: "en", // html 语言代码。将此设置为空,默认值将为 "en"
  timezone: "Asia/Bangkok", // 默认全局时区(IANA 格式)https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
} as const;src/config.ts

要禁用浅色和深色模式,请将 SITE.lightAndDarkMode 设置为 false

选择主要配色方案

默认情况下,如果我们禁用 SITE.lightAndDarkMode,我们将只获得系统的 prefers-color-scheme。

因此,要选择主要配色方案而不是 prefers-color-scheme,我们必须在 toggle-theme.js 中的 primaryColorScheme 变量中设置配色方案。

const primaryColorScheme = ""; // "light" | "dark"

// 从本地存储获取主题数据
const currentTheme = localStorage.getItem("theme");

// ...public/toggle-theme.js

primaryColorScheme 变量可以保存两个值_"light""dark"。如果您不想指定主要配色方案,可以留空字符串(默认)。

为什么 primaryColorScheme 不在 config.ts 中? 为了避免页面重新加载时的颜色闪烁,我们必须在页面加载时尽早放置切换开关 JavaScript 代码。它解决了闪烁的问题,但作为权衡,我们不能再使用 ESM 导入了。

自定义配色方案

AstroPaper 主题的浅色和深色配色方案都可以在 global.css 文件中自定义。

@import "tailwindcss";
@import "./typography.css";

@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));

:root,
html[data-theme="light"] {
  --background: #fdfdfd;
  --foreground: #282728;
  --accent: #006cac;
  --muted: #e6e6e6;
  --border: #ece9e9;
}

html[data-theme="dark"] {
  --background: #212737;
  --foreground: #eaedf3;
  --accent: #ff6b01;
  --muted: #343f60bf;
  --border: #ab4b08;
}
/* ... */src/styles/global.css

在 AstroPaper 主题中,:roothtml[data-theme="light"] 选择器定义浅色配色方案,而 html[data-theme="dark"] 定义深色配色方案。

要自定义您自己的配色方案,请在 :root, html[data-theme="light"] 中指定您的浅色,在 html[data-theme="dark"] 中指定您的深色。

以下是颜色属性的详细说明。

颜色属性定义和用途
--background网站的主要颜色。通常是主背景。
--foreground网站的次要颜色。通常是文本颜色。
--accent网站的强调色。链接颜色、悬停颜色等。
--muted悬停状态等的卡片和滚动条背景颜色。
--border边框颜色。特别用于水平线(hr)

以下是更改浅色配色方案的示例。

/* ... */
:root,
html[data-theme="light"] {
  --background: #f6eee1;
  --foreground: #012c56;
  --accent: #e14a39;
  --muted: #efd8b0;
  --border: #dc9891;
}
/* ... */src/styles/global.css

查看 AstroPaper 已经为您精心设计的一些预定义配色方案


Edit page
Share this post on:

Previous Post
在 AstroPaper 主题中添加新文章
Next Post
如何在 Astro 博客文章中添加 LaTeX 公式