Mercurial
annotate mrjunejune/src/index.js @ 109:1c446ab6f945
[MrJuneJune] New Blog about writing Seobeo library.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Sat, 03 Jan 2026 19:37:51 -0800 |
| parents | bcc76a156aea |
| children |
| rev | line source |
|---|---|
| 82 | 1 const THEME_STORAGE_KEY = 'theme-preference'; |
|
76
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2 // Get stored preference or detect system preference |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
3 function getThemePreference() |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
4 { |
| 82 | 5 const stored = localStorage.getItem(THEME_STORAGE_KEY); |
|
76
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
6 if (stored) { |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
7 return stored; |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
8 } |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
9 return 'auto'; |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
10 } |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
11 |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
12 function applyTheme(preference) |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
13 { |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
14 const root = document.documentElement; |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
15 |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
16 if (preference === 'light') { |
|
80
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
17 root.classList.remove('dark', 'auto'); |
|
76
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
18 root.classList.add('light-mode'); |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
19 } else if (preference === 'dark') { |
|
80
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
20 root.classList.remove('light-mode', 'auto'); |
|
76
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
21 root.classList.add('dark'); |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
22 } else { |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
23 // Auto - remove manual overrides and let CSS media query handle it |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
24 root.classList.remove('light-mode', 'dark'); |
|
80
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
25 root.classList.add('auto'); |
|
76
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
26 } |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
27 } |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
28 |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
29 (function() { |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
30 const currentPreference = getThemePreference(); |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
31 applyTheme(currentPreference); |
|
35b1abc37969
Updating my website to use seobeo library.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
32 })(); |
|
80
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
33 |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
34 /* Toggle lights auto and dark */ |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
35 const toggle = document.querySelector('#themeToggle'); |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
36 toggle.addEventListener('click', function() { |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
37 let preference = getThemePreference(); |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
38 |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
39 // Cycle through: auto -> light -> dark -> auto |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
40 if (preference === 'auto') { |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
41 preference = 'light'; |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
42 } else if (preference === 'light') { |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
43 preference = 'dark'; |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
44 } else { |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
45 preference = 'auto'; |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
46 } |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
47 |
| 82 | 48 localStorage.setItem(THEME_STORAGE_KEY, preference); |
|
80
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
49 applyTheme(preference); |
|
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
76
diff
changeset
|
50 }); |