diff --git a/gui-tests/theme.goml b/gui-tests/theme.goml new file mode 100644 index 000000000..8684cc2cc --- /dev/null +++ b/gui-tests/theme.goml @@ -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)" }) diff --git a/templates/rustdoc/body.html b/templates/rustdoc/body.html index 7fa746bf6..edf35f4ad 100644 --- a/templates/rustdoc/body.html +++ b/templates/rustdoc/body.html @@ -1,4 +1,4 @@ {# see comment in ../storage-change-detection.html for details #} - + diff --git a/templates/style/_syntax-themes.scss b/templates/style/_syntax-themes.scss index ac6dc076e..039769de2 100644 --- a/templates/style/_syntax-themes.scss +++ b/templates/style/_syntax-themes.scss @@ -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; @@ -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; diff --git a/templates/style/_themes.scss b/templates/style/_themes.scss index 2559f1e10..5530ebdca 100644 --- a/templates/style/_themes.scss +++ b/templates/style/_themes.scss @@ -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; @@ -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; diff --git a/templates/theme.js b/templates/theme.js index 6563213ef..ec199fab8 100644 --- a/templates/theme.js +++ b/templates/theme.js @@ -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')); +})()