Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add option to set default theme in _config.vivia.yml #65

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example_config.vivia.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ banner:
onAllPages: true # Display banner on all pages instead of only the home page

# Appearance
color_scheme: dark # dark, light, auto
hue: 250 # The hue of the theme color (e.g. red: 0, orange: 60, blue: 260, purple: 300, pink: 345)
# Visit `saicaca.github.io/vivia-preview` to preview the theme color

Expand Down
1 change: 1 addition & 0 deletions example_zh_CN_config.vivia.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ banner:
onAllPages: true # 在所有页面上显示横幅,而不是只在首页显示

# 外观
color_scheme: dark # dark, light, auto
hue: 250 # 主题色调 (例如:红: 0,橙: 60,蓝:260,紫:300,粉:345)
# 可访问 `saicaca.github.io/vivia-preview` 预览颜色设置

Expand Down
17 changes: 16 additions & 1 deletion layout/layout.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@
hasBanner = "false";
}
%>
<%
// targetTheme: set theme to dark if not being configured to light or auto
let targetTheme;
if (theme.color_scheme === "light") {
targetTheme = "light";
} else if (theme.color_scheme === "auto") {
targetTheme = "auto";
} else {
targetTheme = "dark";
}
%>

<% %>
<html theme="dark" showBanner="true" hasBanner="<%- hasBanner %>" >
<html theme="<%- targetTheme %>" showBanner="true" hasBanner="<%- hasBanner %>" >
<link href="https://cdn.staticfile.org/font-awesome/6.4.2/css/fontawesome.min.css" rel="stylesheet">
<link href="https://cdn.staticfile.org/font-awesome/6.4.2/css/brands.min.css" rel="stylesheet">
<link href="https://cdn.staticfile.org/font-awesome/6.4.2/css/solid.min.css" rel="stylesheet">
<script src="<%- url_for("/js/color.global.min.js") %>" ></script>
<script src="<%- url_for("/js/load-settings.js") %>" ></script>
<% if (targetTheme === "auto") { %>
<script src="<%- url_for("/js/auto-theme-switch.js") %>"></script>
<% } %>
<%- partial('_partial/head') %>
<body>
<% if (theme.previewMode) { %>
Expand Down
10 changes: 10 additions & 0 deletions source/js/auto-theme-switch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function autoTheme() {
let root = document.documentElement;
let theme = localStorage.getItem('theme');
if (theme) {
root.setAttribute('theme', theme);
} else {
window.matchMedia('(prefers-color-scheme: light)').matches ? root.setAttribute('theme', 'light') : root.setAttribute('theme', 'dark');
}
};
autoTheme();