-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add light/dark mode switch button
- Loading branch information
1 parent
4846a19
commit 3fd5227
Showing
5 changed files
with
111 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Setup darkreader for CORS | ||
DarkReader.setFetchMethod(url => { | ||
let headers = new Headers(); | ||
headers.append('Access-Control-Allow-Origin', '*'); | ||
|
||
return window.fetch(url, { | ||
headers, | ||
mode: 'no-cors', | ||
}); | ||
}); | ||
|
||
function darkModeSelected() { | ||
let darkModeSelected = localStorage.getItem('darkMode'); | ||
|
||
if (darkModeSelected === 'false') { | ||
return false; | ||
} | ||
if (darkModeSelected === 'true') { | ||
return true; | ||
} | ||
return darkModeSelected; | ||
} | ||
|
||
function darkModeEnabled() { | ||
if (darkModeSelected() === true) { | ||
return true; | ||
} | ||
if (darkModeSelected() === null && darkModePreferred === true) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
// set color mode and icons on page load | ||
function checkDarkMode() { | ||
if (darkModeEnabled()) { | ||
DarkReader.enable(); | ||
} else { | ||
DarkReader.disable(); | ||
} | ||
toggleIcon(); | ||
} | ||
|
||
function toggleDarkMode() { | ||
if (darkModeEnabled()) { | ||
disableDarkMode(); | ||
} else { | ||
enableDarkMode(); | ||
} | ||
} | ||
|
||
function enableDarkMode() { | ||
DarkReader.enable(); | ||
localStorage.setItem('darkMode', 'true'); | ||
toggleIcon(); | ||
} | ||
|
||
function disableDarkMode() { | ||
DarkReader.disable(); | ||
localStorage.setItem('darkMode', 'false'); | ||
toggleIcon(); | ||
} | ||
|
||
function toggleIcon() { | ||
if (iconDarkMode.classList.contains('fa-sun')) { | ||
// Change to fa-moon | ||
iconDarkMode.classList.remove('fa-sun'); | ||
iconDarkMode.classList.add('fa-moon'); | ||
iconDarkMode.style.color = 'blue'; | ||
} else { | ||
// Change to fa-sun | ||
iconDarkMode.classList.remove('fa-moon'); | ||
iconDarkMode.classList.add('fa-sun'); | ||
iconDarkMode.style.color = 'yellow'; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -21,6 +21,13 @@ | |
<!-- Styles --> | ||
<link href=" {{ site.baseurl }}/assets/style.css?v={{ 'now' | date:'%s' }}" rel="stylesheet"/> | ||
|
||
<!-- FontAwesome --> | ||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.6.0/css/all.css"> | ||
|
||
<!-- Light/Dark Mode--> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/darkreader.min.js"></script> | ||
<script src="/assets/js/color-scheme.js"></script> | ||
|
||
<!-- prettier-ignore --> | ||
<title> | ||
{% if page.title %} | ||
|
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