Skip to content

Namespace docs.rs theme setting #2464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions gui-tests/theme.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// setting local-storage emulates how we detect rustdoc changing the theme, we
// only run this detection on rustdoc pages so must change there before visiting
// other pages

// on rustdoc pages we only control the .nav-container and its descendants, on
// the crate page we control the whole page

go-to: |DOC_PATH| + "/sysinfo"
set-local-storage: { "rustdoc-theme": null }
wait-for-css: (".nav-container", { "background-color": "rgb(255, 255, 255)" })
go-to: |DOC_PATH| + "/crate/sysinfo"
wait-for-css: ("body", { "background-color": "rgb(255, 255, 255)" })

go-to: |DOC_PATH| + "/sysinfo"
set-local-storage: { "rustdoc-theme": "ayu" }
wait-for-css: (".nav-container", { "background-color": "rgb(15, 20, 25)" })
go-to: |DOC_PATH| + "/crate/sysinfo"
wait-for-css: ("body", { "background-color": "rgb(15, 20, 25)" })
2 changes: 1 addition & 1 deletion templates/rustdoc/body.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script async src="/-/static/menu.js?{{ docsrs_version() | slugify }}"></script>
<script async src="/-/static/index.js?{{ docsrs_version() | slugify }}"></script>
{# see comment in ../storage-change-detection.html for details #}
<iframe loading="lazy" src="/-/storage-change-detection.html" width="0" height="0" style="display: none"></iframe>
<iframe src="/-/storage-change-detection.html" width="0" height="0" style="display: none"></iframe>
6 changes: 3 additions & 3 deletions templates/style/_syntax-themes.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ html {
--color-syntax-string: #718c00;
}

// To add a new theme, copy the above theme into a new `html[data-theme="name"]`
// To add a new theme, copy the above theme into a new `html[data-docs-rs-theme="name"]`
// block below and change the colors

html[data-theme="dark"] {
html[data-docs-rs-theme="dark"] {
--color-syntax-foreground: inherit;
--color-syntax-attribute: #ee6868;
--color-syntax-background: #2a2a2a;
Expand All @@ -39,7 +39,7 @@ html[data-theme="dark"] {
--color-syntax-string: #83a300;
}

html[data-theme="ayu"] {
html[data-docs-rs-theme="ayu"] {
--color-syntax-foreground: #e6e1cf;
--color-syntax-attribute: #e6e1cf;
--color-syntax-background: #191f26;
Expand Down
6 changes: 3 additions & 3 deletions templates/style/_themes.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ html {
--chart-grid: #ddd;
}

// To add a new theme, copy the above theme into a new `html[data-theme="name"]`
// To add a new theme, copy the above theme into a new `html[data-docs-rs-theme="name"]`
// block below and change the colors

html[data-theme="dark"] {
html[data-docs-rs-theme="dark"] {
color-scheme: dark;
--color-background-code: #2a2a2a;
--color-background: #353535;
Expand Down Expand Up @@ -68,7 +68,7 @@ html[data-theme="dark"] {
--chart-grid: #4e4e4e;
}

html[data-theme="ayu"] {
html[data-docs-rs-theme="ayu"] {
color-scheme: dark;
--color-background-code: #191f26;
--color-background: #0f1419;
Expand Down
34 changes: 19 additions & 15 deletions templates/theme.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
function applyTheme(theme) {
document.documentElement.dataset.theme = theme;
}
(function() {
function applyTheme(theme) {
if (theme) {
document.documentElement.dataset.docsRsTheme = theme;
}
}

window.addEventListener('storage', function (ev) {
if (ev.key === 'rustdoc-theme') {
applyTheme(ev.newValue);
}
});
window.addEventListener('storage', function (ev) {
if (ev.key === 'rustdoc-theme') {
applyTheme(ev.newValue);
}
});

// see ./storage-change-detection.html for details
window.addEventListener('message', function (ev) {
if (ev.data && ev.data.storage && ev.data.storage.key === 'rustdoc-theme') {
applyTheme(ev.data.storage.value);
}
});
// see ./storage-change-detection.html for details
window.addEventListener('message', function (ev) {
if (ev.data && ev.data.storage && ev.data.storage.key === 'rustdoc-theme') {
applyTheme(ev.data.storage.value);
}
});

applyTheme(window.localStorage.getItem('rustdoc-theme'));
applyTheme(window.localStorage.getItem('rustdoc-theme'));
})()