Skip to content

Commit 00ba0b2

Browse files
Nemo157Joshua Nelson
authored andcommitted
Detect theme changes on all docs.rs pages
This allows us to change non-rustdoc pages if another open tab is looking at a rustdoc page and changes the theme
1 parent 202599b commit 00ba0b2

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

static/storage-change-detection.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<!--
66
Only other windows get notified when we change local storage, so
77
this is used in an invisible iframe to send a message to JS in
8-
../templates/rustdoc/body.html when local storage changes so we can
9-
detect rustdoc changing the theme
8+
../templates/theme.js when rustdoc in the current window changes the
9+
theme
1010
-->
1111
<script type="text/javascript">
1212
onstorage = function(ev) {

templates/rustdoc/body.html

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,7 @@
2929
window.addEventListener("scroll", maybeFixupViewPortPosition, {"once": true});
3030
}
3131
}
32-
33-
window.addEventListener('message', function (ev) {
34-
if (ev.data && ev.data.storage && ev.data.storage.key === 'rustdoc-theme') {
35-
applyTheme(ev.data.storage.value);
36-
}
37-
});
3832
</script>
39-
<!--
40-
Only other windows get notified when we change local storage, so we have an
41-
invisible iframe that sends us a message when local storage changes so we
42-
can detect rustdoc changing the theme
43-
-->
33+
34+
{# see comment in ../../static/storage-change-detection.html for details #}
4435
<iframe src="/-/static/storage-change-detection.html" width="0" height="0" style="display: none"></iframe>

templates/theme.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
// This is a global function also called from a script in ./rustdoc/body.html
2-
// which detects when the rustdoc theme is changed
31
function applyTheme(theme) {
42
document.documentElement.dataset.theme = theme;
53
}
64

5+
window.addEventListener('storage', function (ev) {
6+
if (ev.key === 'rustdoc-theme') {
7+
applyTheme(ev.newValue);
8+
}
9+
});
10+
11+
// see ../static/storage-change-detection.html for details
12+
window.addEventListener('message', function (ev) {
13+
if (ev.data && ev.data.storage && ev.data.storage.key === 'rustdoc-theme') {
14+
applyTheme(ev.data.storage.value);
15+
}
16+
});
17+
718
applyTheme(window.localStorage.getItem('rustdoc-theme'));

0 commit comments

Comments
 (0)