Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move theme storing to local storage #4902

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions javascripts/dark-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ if (typeof define !== 'function') {
}

define([], () => {
const storedValue = window.sessionStorage.getItem('mode');
// default to light mode if no stored value found, or stored value is anything else
let lightModeEnabled = !(storedValue && storedValue === 'dark');
const storedValue = window.localStorage.getItem('mode');

let lightModeEnabled = true;

if (storedValue) {
lightModeEnabled = storedValue === 'light';
} else {
// no stored theme - let's check user's preference
lightModeEnabled = !window.matchMedia('(prefers-color-scheme: dark)').matches;
window.localStorage.setItem('mode', lightModeEnabled ? 'light' : 'dark');
}

const root = document.documentElement;

Expand Down Expand Up @@ -64,7 +72,7 @@ define([], () => {
* @param {"dark" | "light"} value
*/
function updateValue(value) {
window.sessionStorage.setItem('mode', value);
window.localStorage.setItem('mode', value);
lightModeEnabled = value === 'light';
}

Expand Down