forked from PrisonBreak8/my-module-dark-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
18 lines (18 loc) · 847 Bytes
/
index.js
File metadata and controls
18 lines (18 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Module Dark and Light Theme
export function modeInit() {
//============ DARK LIGHT THEME ============================================================================================================================================
// Switch function
const switchTheme = () => {
// Get root element and data-theme value
const rootElem = document.documentElement;
let dataTheme = rootElem.getAttribute('data-theme');
let newTheme = rootElem.getAttribute('data-theme');
newTheme = (dataTheme === 'light') ? 'dark' : 'light';
// Set the new HTML attribute
rootElem.setAttribute('data-theme', newTheme);
// Set the new local storage item
localStorage.setItem('theme', newTheme);
}
//Add event listener for the theme switcher
document.querySelector('#theme-switcher').addEventListener('click', switchTheme);
}