-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: light mode with optional dark mode toggle (#999)
- Loading branch information
Showing
9 changed files
with
376 additions
and
15 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 |
---|---|---|
@@ -1,4 +1 @@ | ||
/* You can override SASS variables here. */ | ||
|
||
@import "plugins/dark"; | ||
|
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,17 @@ | ||
@import "defaults"; | ||
@import "variables"; | ||
|
||
// Import the dark overrides. This file is otherwise identical to dark.scss | ||
@import "plugins/dark.scss"; | ||
|
||
@import "normalize"; | ||
@import "utils"; | ||
@import "main"; | ||
@import "fonts"; | ||
@import "print"; | ||
|
||
@import "markdown"; | ||
@import "shortcodes"; | ||
|
||
// Custom defined styles | ||
@import "custom"; |
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,52 @@ | ||
// iife to avoid polluting the global. | ||
(function () { | ||
// Run me as soon as possible after the the css links are in the dom. | ||
// This assumes this js file is added to the page after the css links. | ||
const lightMode = document.getElementById('light-mode-link') | ||
const darkMode = document.getElementById('dark-mode-link') | ||
const btn = document.querySelector('.dark-mode-toggle') | ||
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)').matches | ||
let theme = prefersDarkScheme ? 'dark' : 'light' | ||
|
||
function enableLightMode () { | ||
lightMode.disabled = false | ||
darkMode.disabled = true | ||
btn.setAttribute('aria-pressed', theme === 'dark') | ||
} | ||
|
||
function enableDarkMode () { | ||
darkMode.disabled = false | ||
lightMode.disabled = true | ||
btn.setAttribute('aria-pressed', theme === 'dark') | ||
} | ||
|
||
// enable dark theme optimistically on OS with dark theme enabled to reduce flashing of white theme. | ||
if (prefersDarkScheme) { | ||
enableDarkMode() | ||
} | ||
|
||
// wait for localstorage... | ||
const previousChoice = localStorage.getItem('theme') | ||
theme = previousChoice || theme | ||
|
||
// Light is default, so enable dark if user previously chose it but their OS pref is light. | ||
if (theme === 'dark') { | ||
enableDarkMode() | ||
} | ||
|
||
// set up the toggle once the DOM is ready. | ||
document.addEventListener("DOMContentLoaded", function(event) { | ||
|
||
btn.addEventListener('click', function () { | ||
theme = (theme === 'light' ? 'dark' : 'light') | ||
if (theme === 'dark') { | ||
enableDarkMode() | ||
} else { | ||
enableLightMode() | ||
} | ||
localStorage.setItem('theme', theme) | ||
}) | ||
// init the button state to match the currently selected theme. | ||
btn.setAttribute('aria-pressed', theme === 'dark') | ||
}); | ||
})() |
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
Oops, something went wrong.