-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.js
52 lines (45 loc) · 1.49 KB
/
theme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*!
* Color mode toggler for Bootstrap's docs (https://getbootstrap.com/)
* Copyright 2011-2024 The Bootstrap Authors
* Licensed under the Creative Commons Attribution 3.0 Unported License.
*/
const getStoredTheme = () => localStorage.getItem('theme')
const setStoredTheme = theme => localStorage.setItem('theme', theme)
function getPreferredTheme() {
const storedTheme = getStoredTheme()
if (storedTheme) {
return storedTheme;
}
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark'
: 'light'
}
function setTheme(theme) {
if (theme === 'auto') {
theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark'
: 'light';
}
document.documentElement.setAttribute('data-bs-theme', theme);
if (theme === 'light') {
document.body.classList.remove('bg-dark');
} else {
document.body.classList.add('bg-dark');
}
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change',
() => {
const storedTheme = getStoredTheme()
if (storedTheme !== 'light' && storedTheme !== 'dark') {
setTheme(getPreferredTheme())
}
});
/**
* Switches between light and dark mode color scheme
*/
function toggleColorScheme() {
// Mirrors the pizza icon when toggling color mode scheme
document.getElementById("pizza-image").classList.toggle("flip-x");
const theme = document.documentElement.getAttribute(
'data-bs-theme') === 'light' ? 'dark' : 'light';
setStoredTheme(theme);
setTheme(theme);
}