-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Added default theme * Revert "feat: Added default theme" This reverts commit a11a99f. * feat: Add basic theme feature * feat: Add theme settings * fix: Fix default theme value * fix: Fix CR and doc-string * Comment out darkMode on exampleSite/hugo.yaml Signed-off-by: hossainemruz <[email protected]> --------- Signed-off-by: hossainemruz <[email protected]> Co-authored-by: Emruz Hossain <[email protected]>
- Loading branch information
1 parent
3ce670e
commit 7bc37c7
Showing
8 changed files
with
134 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import * as params from '@params'; | ||
const PERSISTENCE_KEY = 'theme-scheme' | ||
|
||
const themeOptions = params.theme || {} | ||
const THEME_DARK = typeof themeOptions.dark === 'undefined' ? true : themeOptions.dark; | ||
const THEME_LIGHT = typeof themeOptions.light === 'undefined' ? true : themeOptions.light; | ||
const THEME_DEFAULT = typeof themeOptions.default === 'undefined' ? "system" : themeOptions.default; | ||
|
||
window.addEventListener('load', async () => { | ||
const menu = document.getElementById('themeMenu') | ||
const $icon = document.getElementById('navbar-theme-icon-svg') | ||
if (menu == null || $icon == null) return | ||
|
||
const btns = menu.getElementsByTagName('a') | ||
const iconMap = Array.from(btns).reduce((map, btn) => { | ||
const $img = btn.getElementsByTagName('img')[0] | ||
map[btn.dataset.scheme] = $img.src | ||
return map | ||
}, {}) | ||
|
||
|
||
function checkScheme(scheme) { | ||
if (THEME_LIGHT === false) return "dark" | ||
if (THEME_DARK === false) return "light" | ||
return scheme | ||
} | ||
|
||
function loadScheme() { | ||
return localStorage.getItem(PERSISTENCE_KEY) || loadDefaultScheme() | ||
} | ||
|
||
function loadDefaultScheme() { | ||
return THEME_DEFAULT || "system" | ||
} | ||
|
||
function saveScheme(scheme) { | ||
localStorage.setItem(PERSISTENCE_KEY, scheme) | ||
} | ||
|
||
function getPreferredColorScheme() { | ||
const isDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches; | ||
return isDarkMode ? "dark" : "light"; | ||
} | ||
|
||
function setScheme(newScheme) { | ||
let theme = newScheme | ||
if (newScheme === 'system') { | ||
theme = getPreferredColorScheme() | ||
} | ||
// set data-theme attribute on html tag | ||
document.querySelector("html").dataset.theme = theme; | ||
|
||
// update icon | ||
$icon.src = iconMap[newScheme] | ||
|
||
// save preference to local storage | ||
saveScheme(newScheme) | ||
|
||
setImages(theme) | ||
} | ||
|
||
const checkedScheme = checkScheme(loadScheme()) | ||
setScheme(checkedScheme) | ||
|
||
Array.from(menu.getElementsByTagName('a')).forEach((btn) => { | ||
btn.addEventListener('click', () => { | ||
const { scheme } = btn.dataset | ||
setScheme(scheme) | ||
}) | ||
}) | ||
}) | ||
|
||
function setImages(newScheme) { | ||
const els = Array.from(document.getElementsByClassName('logo-holder')); | ||
for (const el of els) { | ||
const light = el.querySelector('.light-logo'); | ||
const dark = el.querySelector('.dark-logo'); | ||
|
||
if (newScheme === "dark" && dark !== null) { | ||
if (light !== null) light.style.display = 'none' | ||
dark.style.display = 'inline' | ||
} | ||
else { | ||
if (light !== null) light.style.display = 'inline' | ||
if (dark !== null) dark.style.display = 'none' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
{{/* 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"> | ||
<a class="nav-link dropdown-toggle" href="#" id="themeSelector" role="button" | ||
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"> | ||
</a> | ||
<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"> | ||
<img class="theme-icon" src="{{ "icons/sun-svgrepo-com.svg" | relURL }}" width=20 alt="Light Theme"> | ||
</a> | ||
{{ end }} | ||
{{ if $darkEnabled }} | ||
<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"> | ||
</a> | ||
{{ end }} | ||
{{ if and $lightEnabled $darkEnabled }} | ||
<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"> | ||
</a> | ||
{{ end }} | ||
</div> | ||
</li> |