Skip to content

Commit 175c065

Browse files
committed
fix: linting issue
1 parent c4ec2bd commit 175c065

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/js/07-night-mode.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1+
/* eslint-env browser */
12
;(function () {
2-
'use strict'
3+
'use strict';
34

4-
var currentTheme = localStorage.getItem('theme') || 'light'
5-
document.documentElement.setAttribute('data-theme', currentTheme)
5+
const storage = (typeof window !== 'undefined' && window.localStorage) ? window.localStorage : null;
6+
const currentTheme = storage ? (storage.getItem('theme') || 'light') : 'light';
7+
document.documentElement.setAttribute('data-theme', currentTheme);
68

7-
function bind() {
8-
var toggle = document.querySelector('.night-mode-toggle')
9-
if (!toggle) return
10-
toggle.addEventListener('click', function () {
11-
var next = document.documentElement.getAttribute('data-theme') === 'dark' ? 'light' : 'dark'
12-
document.documentElement.setAttribute('data-theme', next)
13-
localStorage.setItem('theme', next)
14-
})
9+
function bind () {
10+
const toggle = document.querySelector('.night-mode-toggle');
11+
if (!toggle) return;
12+
toggle.addEventListener('click', function (e) {
13+
e.preventDefault();
14+
const next = document.documentElement.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
15+
document.documentElement.setAttribute('data-theme', next);
16+
if (storage) storage.setItem('theme', next);
17+
});
1518
}
1619

17-
// Ensure the button exists before binding
1820
if (document.readyState === 'loading') {
19-
document.addEventListener('DOMContentLoaded', bind)
21+
document.addEventListener('DOMContentLoaded', bind);
2022
} else {
21-
bind()
23+
bind();
2224
}
23-
})()
25+
})();

0 commit comments

Comments
 (0)