Skip to content

Commit 9d47023

Browse files
Improve the the local storage handling of the UI theme (#111)
* extra logic for theme in local storage. Not sure why there are multiple spots for it * use prefers theme as well
1 parent 5810273 commit 9d47023

File tree

5 files changed

+47
-25
lines changed

5 files changed

+47
-25
lines changed

gcx/algoliaSearch/index.html

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,29 @@ <h3>Filter by product</h3>
198198
</footer>
199199
<script type="text/javascript" src="/en/home/assets/js/site404.js"></script>
200200
<script>
201+
function getTheme() {
202+
var localStorage = window.localStorage.getItem("theme");
203+
var prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
204+
if (localStorage) return localStorage;
205+
if (prefersDark) return "dark";
206+
return "light";
207+
}
201208
!function () {
202-
var theme = window.localStorage.getItem('theme');
203209
var root = document.getElementsByTagName('html')[0];
204-
if (theme) {
210+
if (getTheme() === 'dark') {
205211
root.classList.add('dark-mode');
206212
}
207213
}();
208214
window.addEventListener('DOMContentLoaded', (event) => {
209-
var theme = window.localStorage.getItem('theme')
215+
var theme = getTheme()
210216

211217
var path = window.location.pathname
212218
var page = path.split('/').pop()
213219
var name = page.replace('.html', '')
214-
console.log(name)
215220
const sidebar = document.querySelector('.sidebar')
216221
const col = document.getElementById('collapse')
217222
var checkbox = document.getElementById('switch')
218-
if (theme) {
223+
if (theme === 'dark') {
219224
checkbox.checked = true
220225
} else {
221226
checkbox.checked = false
@@ -242,7 +247,7 @@ <h3>Filter by product</h3>
242247
window.localStorage.setItem('theme', 'dark')
243248
root.classList.add('dark-mode')
244249
} else {
245-
window.localStorage.removeItem('theme')
250+
window.localStorage.setItem('theme', 'light')
246251
root.classList.remove('dark-mode')
247252
}
248253
}

gcx/stencil/gcx-schema/src/components/gcx-schema/gcx-schema.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class MyComponent {
1010
const theme = window.localStorage.getItem("theme");
1111
let checkbox: any = document.getElementById("switch");
1212
let root = document.getElementsByTagName("html")[0];
13-
if (theme) {
13+
if (theme === 'dark') {
1414
root.classList.add("dark-mode");
1515
checkbox.checked = true;
1616
} else {

gcx/styles/src/js/08-gcx-helios.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
/*Dark theme verification*/
2+
function getTheme () {
3+
var localStorage = window.localStorage.getItem('theme')
4+
var prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
5+
if (localStorage) return localStorage
6+
if (prefersDark) return 'dark'
7+
return 'light'
8+
}
29

310
function isMobile () {
411
// eslint-disable-next-line no-undef
@@ -12,16 +19,15 @@ function isMobile () {
1219
}
1320

1421
window.addEventListener('DOMContentLoaded', (event) => {
15-
var theme = window.localStorage.getItem('theme')
22+
var theme = getTheme()
1623

1724
var path = window.location.pathname
1825
var page = path.split('/').pop()
1926
var name = page.replace('.html', '')
20-
console.log(name)
2127
const sidebar = document.querySelector('.sidebar')
2228
const col = document.getElementById('collapse')
2329
var checkbox = document.getElementById('switch')
24-
if (theme) {
30+
if (theme === 'dark') {
2531
checkbox.checked = true
2632
} else {
2733
checkbox.checked = false
@@ -64,7 +70,7 @@ window.addEventListener('DOMContentLoaded', (event) => {
6470
window.localStorage.setItem('theme', 'dark')
6571
root.classList.add('dark-mode')
6672
} else {
67-
window.localStorage.removeItem('theme')
73+
window.localStorage.setItem('theme', 'light')
6874
root.classList.remove('dark-mode')
6975
}
7076
}
@@ -195,7 +201,7 @@ sliderBlock.forEach( (s,i) => {
195201
})
196202
restart.addEventListener('click', resetSlider)
197203
function resetSlider() {
198-
swiper.slideTo(0)
204+
swiper.slideTo(0)
199205
}
200206
})
201207

gcx/styles/src/layouts/404.hbs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,16 @@
119119
</article>
120120
<script type="text/javascript" src="/en/home/assets/js/site404.js"></script>
121121
<script>
122+
function getTheme() {
123+
var localStorage = window.localStorage.getItem("theme");
124+
var prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
125+
if (localStorage) return localStorage;
126+
if (prefersDark) return "dark";
127+
return "light";
128+
}
122129
!function () {
123-
var theme = window.localStorage.getItem('theme');
124130
var root = document.getElementsByTagName('html')[0];
125-
if (theme) {
131+
if (getTheme() === 'dark') {
126132
root.classList.add('dark-mode');
127133
}
128134
}();
@@ -139,16 +145,15 @@
139145
}
140146
141147
window.addEventListener('DOMContentLoaded', (event) => {
142-
var theme = window.localStorage.getItem('theme')
148+
var theme = getTheme()
143149
144150
var path = window.location.pathname
145151
var page = path.split('/').pop()
146152
var name = page.replace('.html', '')
147-
console.log(name)
148153
const sidebar = document.querySelector('.sidebar')
149154
const col = document.getElementById('collapse')
150155
var checkbox = document.getElementById('switch')
151-
if (theme) {
156+
if (theme === 'dark') {
152157
checkbox.checked = true
153158
} else {
154159
checkbox.checked = false
@@ -175,7 +180,7 @@ window.addEventListener('DOMContentLoaded', (event) => {
175180
window.localStorage.setItem('theme', 'dark')
176181
root.classList.add('dark-mode')
177182
} else {
178-
window.localStorage.removeItem('theme')
183+
window.localStorage.setItem('theme', 'light')
179184
root.classList.remove('dark-mode')
180185
}
181186
}

gcx/styles/src/partials/header-scripts.hbs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
<!-- End Google Tag Manager (noscript) -->
66
{{/with}}
77
<script>
8-
!function(){
9-
var theme = window.localStorage.getItem('theme');
10-
var root = document.getElementsByTagName('html')[0];
11-
if (theme) {
12-
root.classList.add('dark-mode');
13-
}
14-
}();
8+
function getTheme() {
9+
var localStorage = window.localStorage.getItem("theme");
10+
var prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
11+
if (localStorage) return localStorage;
12+
if (prefersDark) return "dark";
13+
return "light";
14+
}
15+
!function () {
16+
var root = document.getElementsByTagName('html')[0];
17+
if (getTheme() === 'dark') {
18+
root.classList.add('dark-mode');
19+
}
20+
}();
1521
</script>
1622

0 commit comments

Comments
 (0)