forked from sujaykundu777/devlopr-jekyll
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sujaykundu777#190 from Apezdr/theme-toggle
Fix Theme Light/Dark Toggling
- Loading branch information
Showing
4 changed files
with
42 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,42 @@ | ||
$(document).ready(()=> modeSwitcher() ) | ||
|
||
(()=> { modeSwitcher()})(); | ||
|
||
function modeSwitcher() { | ||
if ( !localStorage.getItem('color-theme') ){ | ||
document.documentElement.setAttribute('data-theme', 'dark'); | ||
sessionStorage.setItem('theme', 'dark'); | ||
} | ||
else{ | ||
document.documentElement.setAttribute('data-theme', localStorage.getItem('color-theme')); | ||
} | ||
|
||
/** | ||
* Page theme switching between *light* and *dark* | ||
* | ||
* Initialize page theme and set event handlers | ||
*/ | ||
function modeSwitcher() { | ||
|
||
switch ( localStorage.getItem('color-theme') ){ | ||
case 'dark': | ||
$('.theme-toggle').removeAttr('checked'); | ||
break; | ||
case 'light': | ||
$('.theme-toggle').attr('checked',''); | ||
break; | ||
} | ||
|
||
// if (currentTheme === "dark") { | ||
// document.documentElement.setAttribute('data-theme', 'light'); | ||
// sessionStorage.setItem('theme', 'light'); | ||
// } else if (currentTheme === "light") { | ||
// document.documentElement.setAttribute('data-theme', 'dark'); | ||
// sessionStorage.setItem('theme', 'dark'); | ||
// }else{ | ||
// document.documentElement.setAttribute('data-theme', 'dark'); | ||
// sessionStorage.setItem('theme', 'dark'); | ||
// } | ||
// } | ||
/* | ||
* dark-light mode-switcher | ||
* Change the icons inside the button based on previous settings | ||
*/ | ||
$('.theme-toggle').off('click').on('click', function() { | ||
|
||
// if exists and set via local storage previously | ||
if ($(document.documentElement).attr('data-theme') === "dark" ) { | ||
document.documentElement.setAttribute('data-theme', 'light'); | ||
localStorage.setItem('color-theme', 'light'); | ||
} else { | ||
document.documentElement.setAttribute('data-theme', 'dark'); | ||
localStorage.setItem('color-theme', 'dark'); | ||
} | ||
|
||
}); | ||
} |