-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add toggle button for switching between light and dark theme
- Loading branch information
1 parent
cf4d9e5
commit 4b733db
Showing
9 changed files
with
225 additions
and
30 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,8 +1,38 @@ | ||
$(document).ready(function() { | ||
'use strict'; | ||
|
||
const enableDarkTheme = {% if INDIGO_ENABLE_DARK_THEME %}true{% else %}false{% endif %}; | ||
if (enableDarkTheme){ | ||
$('body').addClass("indigo-dark-theme"); | ||
const themeCookie = 'indigo-toggle-dark'; | ||
|
||
function applyThemeOnPage(){ | ||
const theme = $.cookie(themeCookie); | ||
{% if INDIGO_ENABLE_DARK_TOGGLE %} | ||
$('body').toggleClass("indigo-dark-theme", theme === 'dark'); // append or remove dark-class based on cookie-value | ||
// update expiry | ||
$.cookie(themeCookie, theme, { domain: window.location.hostname, expires: 90, path: '/' }); | ||
{% endif %} | ||
} | ||
|
||
function setThemeToggleBtnState(){ | ||
const theme = $.cookie(themeCookie); | ||
$("#toggle-switch-input").prop("checked", theme === 'dark'); | ||
} | ||
|
||
function toggleTheme(){ | ||
const themeValue = $.cookie(themeCookie) === 'dark' ? 'light' : 'dark'; | ||
$.cookie(themeCookie, themeValue, { domain: window.location.hostname, expires: 90, path: '/' }); | ||
|
||
applyThemeOnPage(); | ||
} | ||
}); | ||
|
||
// Listener for updating the theme inside an iframe | ||
window.addEventListener("message", function(e){ | ||
if (e.data && e.data["indigo-toggle-dark"]){ | ||
applyThemeOnPage(); | ||
} | ||
}); | ||
|
||
applyThemeOnPage(); // loading theme on page load | ||
setThemeToggleBtnState(); // check/uncheck toggle btn based on theme | ||
|
||
$('#toggle-switch').on('change', toggleTheme); | ||
}); |
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
Oops, something went wrong.