Skip to content

Commit

Permalink
Merge pull request sujaykundu777#190 from Apezdr/theme-toggle
Browse files Browse the repository at this point in the history
Fix Theme Light/Dark Toggling
  • Loading branch information
sujaykundu777 authored Dec 12, 2022
2 parents 41fb346 + 8ab6556 commit e8c8d43
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
1 change: 1 addition & 0 deletions _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
crossorigin="anonymous"
></script>
<!-- <script src="{{site.url}}{{site.baseurl}}/assets/bower_components/jquery/dist/jquery.min.js"></script> -->
<script src="{{site.url}}{{site.baseurl}}/assets/js/mode-switcher.js"></script>
<script src="{{site.url}}{{site.baseurl}}/assets/bower_components/jquery.easing/jquery.easing.min.js"></script>
<script src="{{site.url}}{{site.baseurl}}/assets/bower_components/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="{{site.url}}{{site.baseurl}}/assets/bower_components/jquery-mousewheel/jquery.mousewheel.min.js"></script>
Expand Down
8 changes: 3 additions & 5 deletions _includes/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@
</a>
</li>

<!-- <li class="nav-item">
<li class="nav-item">
<input
class="nav-link switch"
id="theme-toggle"
onclick="modeSwitcher()"
class="nav-link switch theme-toggle"
type="checkbox"
name="checkbox"
/>
</li> -->
</li>
</ul>
</nav>
<div id="progress-bar"></div>
Expand Down
1 change: 0 additions & 1 deletion _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

<div hidden id="snipcart" data-api-key="Y2I1NTAyNWYtMTNkMy00ODg0LWE4NDItNTZhYzUxNzJkZTI5NjM3MDI4NTUzNzYyMjQ4NzU0"></div>
<script src="https://cdn.snipcart.com/themes/v3.0.0-beta.3/default/snipcart.js" defer></script>
<script src="{{site.url}}{{site.baseurl}}/assets/js/mode-switcher.js"></script>
<script>
if (window.netlifyIdentity) {
window.netlifyIdentity.on("init", user => {
Expand Down
53 changes: 38 additions & 15 deletions assets/js/mode-switcher.js
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');
}

});
}

0 comments on commit e8c8d43

Please sign in to comment.