feat: Add theme settings
This commit is contained in:
parent
dd1ede93d9
commit
17deb772cf
6 changed files with 37 additions and 18 deletions
|
@ -1,8 +1,10 @@
|
||||||
const PERSISTENCE_KEY = 'lightmode:color-scheme'
|
import * as params from '@params';
|
||||||
|
const PERSISTENCE_KEY = 'theme-scheme'
|
||||||
|
|
||||||
const THEME_DARK = typeof process.env.FEATURE_THEME_DARK === 'undefined' ? false : process.env.FEATURE_THEME_DARK;
|
const themeOptions = params.theme || {}
|
||||||
const THEME_LIGHT = typeof process.env.FEATURE_THEME_LIGHT === 'undefined' ? false : process.env.FEATURE_THEME_LIGHT;
|
const THEME_DARK = typeof themeOptions.dark === 'undefined' ? false : themeOptions.dark;
|
||||||
const THEME_DEFAULT = typeof process.env.FEATURE_THEME_DEFAULT === 'system' ? false : process.env.FEATURE_THEME_DEFAULT;
|
const THEME_LIGHT = typeof themeOptions.light === 'undefined' ? false : themeOptions.light;
|
||||||
|
const THEME_DEFAULT = typeof themeOptions.default === 'undefined' ? "system" : themeOptions.default;
|
||||||
|
|
||||||
window.addEventListener('load', async () => {
|
window.addEventListener('load', async () => {
|
||||||
const menu = document.getElementById('themeMenu')
|
const menu = document.getElementById('themeMenu')
|
||||||
|
@ -18,13 +20,9 @@ window.addEventListener('load', async () => {
|
||||||
|
|
||||||
|
|
||||||
function checkScheme(scheme) {
|
function checkScheme(scheme) {
|
||||||
if (scheme === "light" && THEME_LIGHT) {
|
if (THEME_LIGHT == false) return "dark"
|
||||||
return "light"
|
if (THEME_DARK == false) return "light"
|
||||||
}
|
return scheme
|
||||||
if (scheme === "dark" && THEME_DARK) {
|
|
||||||
return "dark"
|
|
||||||
}
|
|
||||||
return "system"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadScheme() {
|
function loadScheme() {
|
||||||
|
@ -61,7 +59,8 @@ window.addEventListener('load', async () => {
|
||||||
setImages(theme)
|
setImages(theme)
|
||||||
}
|
}
|
||||||
|
|
||||||
setScheme(loadScheme())
|
const checkedScheme = checkScheme(loadScheme())
|
||||||
|
setScheme(checkedScheme)
|
||||||
|
|
||||||
Array.from(menu.getElementsByTagName('a')).forEach((btn) => {
|
Array.from(menu.getElementsByTagName('a')).forEach((btn) => {
|
||||||
btn.addEventListener('click', () => {
|
btn.addEventListener('click', () => {
|
||||||
|
@ -77,8 +76,6 @@ function setImages(newScheme) {
|
||||||
const light = el.querySelector('.light-logo');
|
const light = el.querySelector('.light-logo');
|
||||||
const dark = el.querySelector('.dark-logo');
|
const dark = el.querySelector('.dark-logo');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (newScheme === "dark" && dark !== null) {
|
if (newScheme === "dark" && dark !== null) {
|
||||||
if (light !== null) light.style.display = 'none'
|
if (light !== null) light.style.display = 'none'
|
||||||
dark.style.display = 'inline'
|
dark.style.display = 'inline'
|
||||||
|
|
|
@ -91,10 +91,20 @@ params:
|
||||||
# Configure various features of this theme
|
# Configure various features of this theme
|
||||||
features:
|
features:
|
||||||
|
|
||||||
# Enable dark theme
|
# [Deprecated] Enable dark theme
|
||||||
|
# This is a deprecated setting, but has not been removed to maintain backward compatibility
|
||||||
|
# If `theme` is set, the `darkMode` setting will be discarded.
|
||||||
darkMode:
|
darkMode:
|
||||||
enable: true
|
enable: true
|
||||||
|
|
||||||
|
# Configure theme color settings
|
||||||
|
theme:
|
||||||
|
enable: true
|
||||||
|
services:
|
||||||
|
light: true # enable light theme. default "true"
|
||||||
|
dark: true # enable dark theme. default "true"
|
||||||
|
default: system # can be either light, dark or system. default "system"
|
||||||
|
|
||||||
# Enable and configure portfolio
|
# Enable and configure portfolio
|
||||||
portfolio:
|
portfolio:
|
||||||
enable: true
|
enable: true
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
<!--================= add analytics if enabled =========================-->
|
<!--================= add analytics if enabled =========================-->
|
||||||
{{- partial "analytics.html" . -}}
|
{{- partial "analytics.html" . -}}
|
||||||
<script>
|
<script>
|
||||||
theme = localStorage.getItem('darkmode:color-scheme') || 'system';
|
theme = localStorage.getItem('theme-scheme') || localStorage.getItem('darkmode:color-scheme') || 'system';
|
||||||
if (theme == 'system') {
|
if (theme == 'system') {
|
||||||
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||||
theme = 'dark';
|
theme = 'dark';
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
<!--================= add analytics if enabled =========================-->
|
<!--================= add analytics if enabled =========================-->
|
||||||
{{- partial "analytics.html" . -}}
|
{{- partial "analytics.html" . -}}
|
||||||
<script>
|
<script>
|
||||||
theme = localStorage.getItem('darkmode:color-scheme') || 'system';
|
theme = localStorage.getItem('theme-scheme') || localStorage.getItem('darkmode:color-scheme') || 'system';
|
||||||
if (theme == 'system') {
|
if (theme == 'system') {
|
||||||
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||||
theme = 'dark';
|
theme = 'dark';
|
||||||
|
|
|
@ -141,7 +141,7 @@
|
||||||
{{ if .IsTranslated }}
|
{{ if .IsTranslated }}
|
||||||
{{ partial "navigators/lang-selector.html" . }}
|
{{ partial "navigators/lang-selector.html" . }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ if site.Params.features.darkMode.enable }}
|
{{ if or site.Params.features.darkMode.enable site.Params.features.theme.enable }}
|
||||||
{{ partial "navigators/theme-selector.html" . }}
|
{{ partial "navigators/theme-selector.html" . }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
@ -1,15 +1,27 @@
|
||||||
|
{{/* variables for enabling/disabling various features */}}
|
||||||
|
{{ $darkEnabled := true }}
|
||||||
|
{{ $lightEnabled := true }}
|
||||||
|
{{ if site.Params.features.theme.enable }}
|
||||||
|
{{ $darkEnabled = site.Params.features.theme.services.dark | default true }}
|
||||||
|
{{ $lightEnabled = site.Params.features.theme.services.light | default true }}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
<li class="nav-item dropdown">
|
<li class="nav-item dropdown">
|
||||||
<a class="nav-link dropdown-toggle" href="#" id="themeSelector" role="button"
|
<a class="nav-link dropdown-toggle" href="#" id="themeSelector" role="button"
|
||||||
data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
||||||
<img id="navbar-theme-icon-svg" class="theme-icon" src="{{ "icons/moon-svgrepo-com.svg" | relURL }}" width=20 alt="Dark Theme">
|
<img id="navbar-theme-icon-svg" class="theme-icon" src="{{ "icons/moon-svgrepo-com.svg" | relURL }}" width=20 alt="Dark Theme">
|
||||||
</a>
|
</a>
|
||||||
<div id="themeMenu" class="dropdown-menu dropdown-menu-icons-only" aria-labelledby="themeSelector">
|
<div id="themeMenu" class="dropdown-menu dropdown-menu-icons-only" aria-labelledby="themeSelector">
|
||||||
|
{{ if $lightEnabled }}
|
||||||
<a class="dropdown-item nav-link" href="#" data-scheme="light">
|
<a class="dropdown-item nav-link" href="#" data-scheme="light">
|
||||||
<img class="theme-icon" src="{{ "icons/sun-svgrepo-com.svg" | relURL }}" width=20 alt="Light Theme">
|
<img class="theme-icon" src="{{ "icons/sun-svgrepo-com.svg" | relURL }}" width=20 alt="Light Theme">
|
||||||
</a>
|
</a>
|
||||||
|
{{ end }}
|
||||||
|
{{ if $darkEnabled }}
|
||||||
<a class="dropdown-item nav-link" href="#" data-scheme="dark">
|
<a class="dropdown-item nav-link" href="#" data-scheme="dark">
|
||||||
<img class="theme-icon" src="{{ "icons/moon-svgrepo-com.svg" | relURL }}" width=20 alt="Dark Theme">
|
<img class="theme-icon" src="{{ "icons/moon-svgrepo-com.svg" | relURL }}" width=20 alt="Dark Theme">
|
||||||
</a>
|
</a>
|
||||||
|
{{ end }}
|
||||||
<a class="dropdown-item nav-link" href="#" data-scheme="system">
|
<a class="dropdown-item nav-link" href="#" data-scheme="system">
|
||||||
<img class="theme-icon" src="{{ "icons/computer-svgrepo-com.svg" | relURL }}" width=20 alt="System Theme">
|
<img class="theme-icon" src="{{ "icons/computer-svgrepo-com.svg" | relURL }}" width=20 alt="System Theme">
|
||||||
</a>
|
</a>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue