File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const DARK_MODE = 'dark' ;
2+ const LIGHT_MODE = 'light' ;
3+ const THEME = 'mode' ;
4+
5+ document . addEventListener (
6+ 'DOMContentLoaded' , ( event ) => {
7+ applyTheme ( ) ;
8+ const toggleSwitch = document . getElementById ( 'toggle-switch' ) ;
9+ toggleSwitch . onclick = function ( ) {
10+ let currentMode = localStorage . getItem ( THEME ) ;
11+ localStorage . setItem (
12+ THEME ,
13+ currentMode === DARK_MODE ? LIGHT_MODE : DARK_MODE
14+ ) ;
15+ applyTheme ( ) ;
16+ }
17+ }
18+ ) ;
19+
20+ function applyTheme ( ) {
21+ let html = document . documentElement ;
22+ let currentMode = localStorage . getItem ( THEME ) ;
23+ if ( currentMode === DARK_MODE ) {
24+ html . classList . add ( DARK_MODE ) ;
25+ document . getElementById ( 'toggle-switch' ) . innerHTML =
26+ '<i class="fas fa-sun fa-3x"></i>' ;
27+ }
28+ else {
29+ html . classList . remove ( DARK_MODE ) ;
30+ document . getElementById ( 'toggle-switch' ) . innerHTML =
31+ '<i class="fas fa-moon fa-3x"></i>' ;
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments