-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hack element toggler into Quarto theme switching code
- Loading branch information
Showing
3 changed files
with
49 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,8 @@ format: | |
dark: darkly | ||
css: styles.css | ||
toc: true | ||
include-in-header: | ||
- toggle.js | ||
|
||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<script id="toggle-light-dark-elements" type="application/javascript"> | ||
/* Extend Quarto theme toggling to allow elements to be displayed/hidden in light/dark themes */ | ||
|
||
// Replicated from https://github.com/quarto-dev/quarto-cli/blob/84d4659/src/resources/formats/html/templates/quarto-html.ejs | ||
const getColorSchemeSentinel = () => { | ||
const localAlternateSentinel = 'alternate'; | ||
if (window.location.protocol !== 'file:') { | ||
const storageValue = window.localStorage.getItem('quarto-color-scheme'); | ||
return storageValue != null ? storageValue : localAlternateSentinel; | ||
} else { | ||
return localAlternateSentinel; | ||
} | ||
}; | ||
|
||
// Function to toggle light and dark elements based on color scheme | ||
const toggleColorSchemeElements = () => { | ||
const scheme = getColorSchemeSentinel(); | ||
const lightElements = document.getElementsByClassName('only-light'); | ||
const darkElements = document.getElementsByClassName('only-dark'); | ||
|
||
for (var i = 0; i < lightElements.length; i++) { | ||
lightElements[i].style.display = scheme == 'default' ? 'block' : 'none'; | ||
} | ||
for (var i = 0; i < darkElements.length; i++) { | ||
darkElements[i].style.display = scheme == 'default' ? 'none' : 'block'; | ||
} | ||
}; | ||
|
||
// Add event listener for when the readyState is complete (and default toggler is set) | ||
document.addEventListener('readystatechange', function() { | ||
if (document.readyState === 'complete') { | ||
// Toggle scheme once | ||
toggleColorSchemeElements(); | ||
// Append our toggle function to the old one | ||
const oldToggle = window.quartoToggleColorScheme; | ||
window.quartoToggleColorScheme = () => { | ||
oldToggle(); | ||
toggleColorSchemeElements(); | ||
} | ||
} | ||
}); | ||
</script> |