Skip to content

Commit

Permalink
feat: Use the theme flavor configured in the browser (#226)
Browse files Browse the repository at this point in the history
* Use the theme flavor configured in the browser
* refactor header-theme.js file

Cover
#128
  • Loading branch information
benjaminParisel authored Jul 11, 2024
1 parent 257c24d commit f203feb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 111 deletions.
100 changes: 35 additions & 65 deletions src/js/header-01-theme.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,44 @@
/* eslint-disable no-undef */
/* eslint-disable no-unused-vars */
// Need to be on top of the file to avoid the flash after the content loaded
document.querySelector('html').setAttribute('data-theme', isDarkTheme() ? 'dark' : 'light')

function toDarkTheme (resolve) {
localStorage.setItem('theme', 'dark')
document.querySelector('html').setAttribute('data-theme', 'dark')

// Update highlight js theme
enableHightLightDarkTheme(true)
// Promise.resolve if existing
resolve && resolve()
}

function toLightTheme (resolve) {
localStorage.setItem('theme', 'light')
document.querySelector('html').setAttribute('data-theme', 'light')

// Update highlight js theme
enableHightLightDarkTheme(false)
// Promise.resolve if existing
resolve && resolve()
}

// Update highlight js to dark theme if dark = true
function enableHightLightDarkTheme (dark) {
var hljsCssLink = document.getElementById('highlight-style-lnk')
if (hljsCssLink) {
var currentHref = hljsCssLink.getAttribute('href')
var cssHref = currentHref.replace('-dark', '-light')
if (dark) {
cssHref = currentHref.replace('-light', '-dark')
}
hljsCssLink.setAttribute('href', cssHref)
} else {
console.log('Failed to find highlight-style-lnk css link element in page, can not swap theme')
}
// Check if user has set a theme preference in localStorage or in browser preferences
function isDarkTheme () {
const localThemeSetting = localStorage.getItem('theme')
return !localThemeSetting
? window.matchMedia('(prefers-color-scheme: dark)').matches
: localThemeSetting === 'dark'
}

function performThemeSwitch (checkbox, switchBall) {
setTimeout(function () {
const themeSwitchPromise = new Promise((resolve) => {
if (checkbox.checked) {
toDarkTheme(resolve)
document.addEventListener('DOMContentLoaded', () => {
function updateTheme (isDarkTheme) {
// Switch HighlightJs to theme
function enableHighLightJsTheme (isDarkTheme) {
const hljsCssLink = document.getElementById('highlight-style-lnk')
if (hljsCssLink) {
const currentHref = hljsCssLink.getAttribute('href')
let cssHref = currentHref.replace('-dark', '-light')
if (isDarkTheme) {
cssHref = currentHref.replace('-light', '-dark')
}
hljsCssLink.setAttribute('href', cssHref)
} else {
toLightTheme(resolve)
console.log('Failed to find highlight-style-lnk css link element in page, can not swap theme')
}
})

themeSwitchPromise.finally(function () {
switchBall.innerHTML = ''
})
}, 100)
}
}
document.querySelector('html').setAttribute('data-theme', isDarkTheme ? 'dark' : 'light')
enableHighLightJsTheme(isDarkTheme)
}

// create the loader div
const loader = document.createElement('div')
loader.classList.add('lds-dual-ring')
const checkboxTheme = document.getElementById('theme-switch')

function toggleDarkThemeMode (checkbox) {
const switchBall = document.querySelector('.theme-switch-wrapper .toggle-content .label .ball')
switchBall.appendChild(loader)
performThemeSwitch(checkbox, switchBall)
}
checkboxTheme.addEventListener('change', (event) => {
const isDarkTheme = event.currentTarget.checked
localStorage.setItem('theme', isDarkTheme ? 'dark' : 'light')
updateTheme(isDarkTheme)
})

function isDarkTheme () {
return localStorage.getItem('theme') === 'dark' ? 'checked' : 'unchecked'
}

// init
if (localStorage.getItem('theme') === 'dark') {
toDarkTheme()
} else {
toLightTheme()
}
const darkThemeChecked = isDarkTheme()
updateTheme(darkThemeChecked)
checkboxTheme.checked = darkThemeChecked
})
13 changes: 0 additions & 13 deletions src/partials/footer-scripts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,3 @@
});
</script>
<script>
// init toggle state
const isDarkThemeEnable = localStorage.getItem('theme') === 'dark';
initializeThemeToggleIfAvailable('check', isDarkThemeEnable);
initializeThemeToggleIfAvailable('check-mobile', isDarkThemeEnable);
function initializeThemeToggleIfAvailable(elementId, checked) {
const checkElement = document.getElementById(elementId);
if(checkElement) {
checkElement.checked = checked;
}
}
</script>
4 changes: 2 additions & 2 deletions src/partials/header-content.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
{{/if}}

<div class="theme-switch-wrapper">
<input type="checkbox" class="checkbox" onclick="toggleDarkThemeMode(this)" id="check">
<input type="checkbox" class="checkbox" id="theme-switch">
<div class="toggle-content">
<label class="label" for="check">
<label class="label" for="theme-switch">
<span>🌜</span>
<span>🌞</span>
<div class="ball"></div>
Expand Down
30 changes: 0 additions & 30 deletions src/stylesheets/loader.scss

This file was deleted.

1 change: 0 additions & 1 deletion src/stylesheets/site.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@
@import "print.scss";
@import "components/cards";
@import "search.scss";
@import "loader.scss";
@import "components/troubleshooting.scss";
@import "components/colors.scss";

0 comments on commit f203feb

Please sign in to comment.