Skip to content

Commit 9e3b210

Browse files
committed
Hack element toggler into Quarto theme switching code
1 parent 4b495b5 commit 9e3b210

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

_quarto.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ format:
6767
dark: darkly
6868
css: styles.css
6969
toc: true
70+
include-in-header:
71+
- toggle.js
7072

7173

7274

index.qmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ format:
44
page-layout: full
55
---
66

7-
<center><a>
8-
<picture>
9-
<source srcset="images/stanford-line2-8-dark.png" media="(prefers-color-scheme: dark)" />
10-
<img src="images/stanford-line2-8.png" alt="Poldracklab logo" width="50%" />
11-
</picture></a>
7+
<center>
8+
<a>
9+
<img src="images/stanford-line2-8.png" alt="Poldracklab logo" width="50%" class="only-light" />
10+
<img src="images/stanford-line2-8-dark.png" alt="Poldracklab logo" width="50%" class="only-dark" style="display: none" />
11+
</a>
1212
</center>
1313

1414
<br>

toggle.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<script id="toggle-light-dark-elements" type="application/javascript">
2+
// Extend Quarto theme toggling to allow elements to be displayed/hidden in light/dark themes
3+
4+
// Replicated from https://github.com/quarto-dev/quarto-cli/blob/84d4659/src/resources/formats/html/templates/quarto-html.ejs
5+
const getColorSchemeSentinel = () => {
6+
const localAlternateSentinel = 'alternate';
7+
if (window.location.protocol !== 'file:') {
8+
const storageValue = window.localStorage.getItem('quarto-color-scheme');
9+
return storageValue != null ? storageValue : localAlternateSentinel;
10+
} else {
11+
return localAlternateSentinel;
12+
}
13+
};
14+
15+
// Function to toggle light and dark elements based on color scheme
16+
const toggleColorSchemeElements = () => {
17+
const scheme = getColorSchemeSentinel();
18+
const lightElements = document.getElementsByClassName('only-light');
19+
const darkElements = document.getElementsByClassName('only-dark');
20+
21+
for (var i = 0; i < lightElements.length; i++) {
22+
lightElements[i].style.display = scheme == 'default' ? 'block' : 'none';
23+
}
24+
for (var i = 0; i < darkElements.length; i++) {
25+
darkElements[i].style.display = scheme == 'default' ? 'none' : 'block';
26+
}
27+
};
28+
29+
// Add event listener for when the readyState is complete (and default toggler is set)
30+
document.addEventListener('readystatechange', function() {
31+
if (document.readyState === 'complete') {
32+
// Toggle scheme once
33+
toggleColorSchemeElements();
34+
// Append our toggle function to the old one
35+
const oldToggle = window.quartoToggleColorScheme;
36+
window.quartoToggleColorScheme = () => {
37+
oldToggle();
38+
toggleColorSchemeElements();
39+
}
40+
}
41+
});
42+
</script>

0 commit comments

Comments
 (0)