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

[FEATURE] Add switch theme drop down menu #26

Merged
merged 4 commits into from
Apr 28, 2021
Merged
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
21 changes: 21 additions & 0 deletions assets/css/_partial/_header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ header {
}
}

.color-theme-select {
position: absolute;
opacity: 0;
left: 0;
top: 0;
width: 100%;
height: 100%;

&:hover {
cursor: pointer;
}
}

.search {
position: relative;

Expand Down Expand Up @@ -162,6 +175,10 @@ header {
@include transform(rotate(225deg));
}

.theme-select i {
@include transform(rotate(225deg));
}

#header-desktop {
display: block;
position: fixed;
Expand Down Expand Up @@ -205,6 +222,10 @@ header {
margin-right: 0;
}

&.theme-select {
margin-right: 0;
}

&.search {
margin: 0 -.5rem 0 0;
}
Expand Down
2 changes: 1 addition & 1 deletion assets/js/theme.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/theme.min.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,9 @@ enableEmoji = true
# mobile header mode ("fixed", "normal", "auto")
# 移动端导航栏模式 ("fixed", "normal", "auto")
mobileMode = "auto"
# theme change mode ("switch", "select")
# 主题切换模式 ("switch", "select")
themeChangeMode = "select"
# Header title config
# 页面头部导航栏标题配置
[params.header.title]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ Please open the code block below to view the complete sample configuration :(far
desktopMode = "fixed"
# mobile header mode ("fixed", "normal", "auto")
mobileMode = "auto"
# {{< version 0.2.11 >}} Theme change mode
# theme change mode ("switch", "select")
themeChangeMode = "select"
# {{< version 0.2.0 >}} Header title config
[params.header.title]
# URL of the LOGO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ hugo
desktopMode = "fixed"
# 移动端导航栏模式 ("fixed", "normal", "auto")
mobileMode = "auto"
# {{< version 0.2.11 >}} 主题切换模式
# 主题切换模式 ("switch", "select")
themeChangeMode = "select"
# {{< version 0.2.0 >}} 页面头部导航栏标题配置
[params.header.title]
# LOGO 的 URL
Expand Down
12 changes: 12 additions & 0 deletions i18n/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ other = "Select Language"

[switchTheme]
other = "Switch Theme"

[Light]
other = "Light"

[Dark]
other = "Dark"

[Black]
other = "Black"

[Auto]
other = "Auto"
# === partials/header.html ===

# === partials/footer.html ===
Expand Down
12 changes: 12 additions & 0 deletions i18n/zh-CN.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ other = "选择语言"

[switchTheme]
other = "切换主题"

[Light]
other = "浅色"

[Dark]
other = "深色"

[Black]
other = "黑色"

[Auto]
other = "跟随系统"
# === partials/header.html ===

# === partials/footer.html ===
Expand Down
37 changes: 18 additions & 19 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,30 @@
{{- /* Check theme isDark before body rendering */ -}}
{{- $theme := .Site.Params.defaulttheme -}}
<script type="text/javascript">
if (window.localStorage && localStorage.getItem('theme'))
{
let localStorageTheme = localStorage.getItem('theme');
if (localStorageTheme === 'light' || localStorageTheme === 'dark' || localStorageTheme === 'black')
document.body.setAttribute('theme', localStorageTheme);
else if (localStorage.getItem('theme') === 'auto')
{
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches)
if (window.localStorage && localStorage.getItem('theme')) {
let theme = localStorage.getItem('theme');
if (theme === 'light' || theme === 'dark' || theme === 'black') {
document.body.setAttribute('theme', theme);
} else {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.body.setAttribute('theme', 'dark');
else
}
else {
document.body.setAttribute('theme', 'white');
}
}
}
else
{
if ('{{ $theme }}' === 'light' || '{{ $theme }}' === 'dark' || '{{ $theme }}' === 'black')
{
} else {
if ('{{ $theme }}' === 'light' || '{{ $theme }}' === 'dark' || '{{ $theme }}' === 'black') {
document.body.setAttribute('{{ $theme }}');
window.localStorage && localStorage.setItem('theme', '{{ $theme }}');
}
else
{
} else {
window.localStorage && localStorage.setItem('theme', 'auto');
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) document.body.setAttribute('theme', 'dark');
else document.body.setAttribute('theme', 'white');
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.body.setAttribute('theme', 'dark');
}
else {
document.body.setAttribute('theme', 'white');
}
}
}
</script>
Expand Down
32 changes: 24 additions & 8 deletions layouts/partials/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,21 @@
</span>
</span>
{{- end -}}
{{- if eq .Site.Params.header.themeChangeMode "select" -}}
<a href="javascript:void(0);" class="menu-item theme-select" title="{{ T "switchTheme" }}">
<i class="fas fa-adjust fa-fw"></i>
<select class="color-theme-select" id="theme-select-desktop" title="{{ T "switchTheme" }}">
<option value="light">{{ T "Light" }}</option>
<option value="dark">{{ T "Dark" }}</option>
<option value="black">{{ T "Black" }}</option>
<option value="auto">{{ T "Auto" }}</option>
</select>
</a>
{{- else -}}
<a href="javascript:void(0);" class="menu-item theme-switch" title="{{ T "switchTheme" }}">
<i class="fas fa-adjust fa-fw"></i>
</a>
<!-- <a href="javascript:void(0);" class="menu-item theme-switch" title="{{ T "switchTheme" }}">
<select class="theme-select" id="theme-select-desktop" title="{{ T "switchTheme" }}">
<option value="Light">Light</option>
<option value="Dark">Dark</option>
<option value="Black">Black</option>
<option value="Auto">Auto</option>
</select>
</a> -->
{{- end -}}
</div>
</div>
</div>
Expand Down Expand Up @@ -154,9 +158,21 @@
{{- .Pre | safeHTML }}{{ .Name }}{{ .Post | safeHTML -}}
</a>
{{- end -}}
{{- if eq .Site.Params.header.themeChangeMode "select" -}}
<a href="javascript:void(0);" class="menu-item theme-select" title="{{ T "switchTheme" }}">
<i class="fas fa-adjust fa-fw"></i>
<select class="color-theme-select" id="theme-select-mobile" title="{{ T "switchTheme" }}">
<option value="light">{{ T "Light" }}</option>
<option value="dark">{{ T "Dark" }}</option>
<option value="black">{{ T "Black" }}</option>
<option value="auto">{{ T "Auto" }}</option>
</select>
</a>
{{- else -}}
<a href="javascript:void(0);" class="menu-item theme-switch" title="{{ T "switchTheme" }}">
<i class="fas fa-adjust fa-fw"></i>
</a>
{{- end -}}
{{- if .Site.IsMultiLingual -}}
<a href="javascript:void(0);" class="menu-item" title="{{ T "selectLanguage" }}">
{{- .Language.LanguageName -}}
Expand Down
51 changes: 48 additions & 3 deletions src/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,53 @@ class Theme {
initSwitchTheme() {
this.util.forEach(document.getElementsByClassName('theme-switch'), $themeSwitch => {
$themeSwitch.addEventListener('click', () => {
if (document.body.getAttribute('theme') === 'dark') document.body.setAttribute('theme', 'black'), window.localStorage && localStorage.setItem('theme', 'black'), this.isDark = true;
else if (document.body.getAttribute('theme') === 'black') document.body.setAttribute('theme', 'light'), window.localStorage && localStorage.setItem('theme', 'light'), this.isDark = false;
else document.body.setAttribute('theme', 'dark'), window.localStorage && localStorage.setItem('theme', 'dark'), this.isDark = true;
let currentTheme = document.body.getAttribute('theme');
if (currentTheme === 'dark') {
document.body.setAttribute('theme', 'black');
window.localStorage && localStorage.setItem('theme', 'black');
this.isDark = true;
} else if (currentTheme === 'black') {
document.body.setAttribute('theme', 'light');
window.localStorage && localStorage.setItem('theme', 'light');
this.isDark = false;
} else {
document.body.setAttribute('theme', 'dark');
window.localStorage && localStorage.setItem('theme', 'dark');
this.isDark = true;
}
for (let event of this.switchThemeEventSet) event();
}, false);
});
}

initSelectTheme() {
this.util.forEach(document.getElementsByClassName('color-theme-select'), $themeSelect => {
let currentTheme = document.body.getAttribute('theme');
for (let i, j = 0; i = $themeSelect.options[j]; j++) {
if (i.value == currentTheme) {
$themeSelect.selectedIndex = j;
break;
}
}
$themeSelect.addEventListener('change', () => {
let theme = $themeSelect.value;
window.localStorage && localStorage.setItem('theme', theme);
if (theme != 'auto') {
document.body.setAttribute('theme', theme);
if (theme == 'light') {
this.isDark = false;
} else {
this.isDark = true;
}
} else {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.body.setAttribute('theme', 'dark');
this.isDark = true;
} else {
document.body.setAttribute('theme', 'white');
this.isDark = false;
}
}
for (let event of this.switchThemeEventSet) event();
}, false);
});
Expand Down Expand Up @@ -697,6 +741,7 @@ class Theme {
this.initTwemoji();
this.initMenuMobile();
this.initSwitchTheme();
this.initSelectTheme();
this.initSearch();
this.initDetails();
this.initLightGallery();
Expand Down