Skip to content

Commit edcf05e

Browse files
Improve theme support when JS is disabled
1 parent 684bb78 commit edcf05e

File tree

9 files changed

+37
-16
lines changed

9 files changed

+37
-16
lines changed

src/book/init.rs

+3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ impl BookBuilder {
136136
let mut chrome_css = File::create(cssdir.join("chrome.css"))?;
137137
chrome_css.write_all(theme::CHROME_CSS)?;
138138

139+
let mut noscript_css = File::create(cssdir.join("noscript.css"))?;
140+
noscript_css.write_all(theme::NOSCRIPT_CSS)?;
141+
139142
if html_config.print.enable {
140143
let mut print_css = File::create(cssdir.join("print.css"))?;
141144
print_css.write_all(theme::PRINT_CSS)?;

src/renderer/html_handlebars/hbs_renderer.rs

+1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ impl HtmlHandlebars {
243243
write_file(destination, "book.js", &theme.js)?;
244244
write_file(destination, "css/general.css", &theme.general_css)?;
245245
write_file(destination, "css/chrome.css", &theme.chrome_css)?;
246+
write_file(destination, "css/noscript.css", &theme.noscript_css)?;
246247
if html_config.print.enable {
247248
write_file(destination, "css/print.css", &theme.print_css)?;
248249
}

src/theme/css/chrome.css

+6-10
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ a > .hljs {
4040
border-block-end-style: solid;
4141
}
4242
#menu-bar.sticky,
43-
.js #menu-bar-hover-placeholder:hover + #menu-bar,
44-
.js #menu-bar:hover,
45-
.js.sidebar-visible #menu-bar {
43+
#menu-bar-hover-placeholder:hover + #menu-bar,
44+
#menu-bar:hover,
45+
body.sidebar-visible #menu-bar {
4646
position: -webkit-sticky;
4747
position: sticky;
4848
top: 0 !important;
@@ -91,9 +91,6 @@ a > .hljs {
9191
display: flex;
9292
margin: 0 5px;
9393
}
94-
.no-js .left-buttons button {
95-
display: none;
96-
}
9794

9895
.menu-title {
9996
display: inline-block;
@@ -107,7 +104,7 @@ a > .hljs {
107104
overflow: hidden;
108105
text-overflow: ellipsis;
109106
}
110-
.js .menu-title {
107+
.menu-title {
111108
cursor: pointer;
112109
}
113110

@@ -427,8 +424,7 @@ ul#searchresults span.teaser em {
427424
-ms-user-select: none;
428425
user-select: none;
429426
}
430-
.no-js .sidebar,
431-
.js:not(.sidebar-resizing) .sidebar {
427+
body:not(.sidebar-resizing) .sidebar {
432428
transition: transform 0.3s; /* Animation: slide away */
433429
}
434430
.sidebar code {
@@ -465,7 +461,7 @@ ul#searchresults span.teaser em {
465461
left: calc(var(--sidebar-resize-indicator-width) * -1);
466462
right: unset;
467463
}
468-
.js .sidebar .sidebar-resize-handle {
464+
.sidebar .sidebar-resize-handle {
469465
cursor: col-resize;
470466
width: calc(var(--sidebar-resize-indicator-width) - var(--sidebar-resize-indicator-space));
471467
}

src/theme/css/general.css

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ html {
1414
-webkit-text-size-adjust: none;
1515
}
1616

17+
* {
18+
color: var(--fg);
19+
}
20+
1721
body {
1822
margin: 0;
1923
font-size: 1.6rem;

src/theme/css/noscript.css

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.left-buttons button {
2+
display: none;
3+
}
4+
5+
.sidebar {
6+
transition: transform 0.3s; /* Animation: slide away */
7+
}

src/theme/css/variables.css

+4-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
--copy-button-filter-hover: invert(36%) sepia(70%) saturate(503%) hue-rotate(167deg) brightness(98%) contrast(89%);
113113
}
114114

115-
.light {
115+
.light, body:not(.js) {
116116
--bg: hsl(0, 0%, 100%);
117117
--fg: hsl(0, 0%, 0%);
118118

@@ -258,7 +258,7 @@
258258
}
259259

260260
@media (prefers-color-scheme: dark) {
261-
.light.no-js {
261+
body:not(.js) {
262262
--bg: hsl(200, 7%, 8%);
263263
--fg: #98a3ad;
264264

@@ -299,6 +299,8 @@
299299
--searchresults-li-bg: #2b2b2f;
300300
--search-mark-bg: #355c7d;
301301

302+
--color-scheme: dark;
303+
302304
/* Same as `--icons` */
303305
--copy-button-filter: invert(26%) sepia(8%) saturate(575%) hue-rotate(169deg) brightness(87%) contrast(82%);
304306
/* Same as `--sidebar-active` */

src/theme/index.hbs

+5-4
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@
5252
<!-- MathJax -->
5353
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
5454
{{/if}}
55+
<noscript>
56+
<link rel="stylesheet" href="{{ path_to_root }}css/noscript.css">
57+
</noscript>
5558
</head>
56-
<body class="sidebar-visible no-js">
59+
<body class="sidebar-visible">
5760
<div id="body-container">
5861
<!-- Provide site root to javascript -->
5962
<script>
@@ -85,9 +88,7 @@
8588
var html = document.querySelector('html');
8689
html.classList.remove('{{ default_theme }}')
8790
html.classList.add(theme);
88-
var body = document.querySelector('body');
89-
body.classList.remove('no-js')
90-
body.classList.add('js');
91+
document.body.classList.add("js");
9192
</script>
9293

9394
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">

src/theme/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub static HEAD: &[u8] = include_bytes!("head.hbs");
1818
pub static REDIRECT: &[u8] = include_bytes!("redirect.hbs");
1919
pub static HEADER: &[u8] = include_bytes!("header.hbs");
2020
pub static CHROME_CSS: &[u8] = include_bytes!("css/chrome.css");
21+
pub static NOSCRIPT_CSS: &[u8] = include_bytes!("css/noscript.css");
2122
pub static GENERAL_CSS: &[u8] = include_bytes!("css/general.css");
2223
pub static PRINT_CSS: &[u8] = include_bytes!("css/print.css");
2324
pub static VARIABLES_CSS: &[u8] = include_bytes!("css/variables.css");
@@ -51,6 +52,7 @@ pub struct Theme {
5152
pub redirect: Vec<u8>,
5253
pub header: Vec<u8>,
5354
pub chrome_css: Vec<u8>,
55+
pub noscript_css: Vec<u8>,
5456
pub general_css: Vec<u8>,
5557
pub print_css: Vec<u8>,
5658
pub variables_css: Vec<u8>,
@@ -87,6 +89,7 @@ impl Theme {
8789
(theme_dir.join("header.hbs"), &mut theme.header),
8890
(theme_dir.join("book.js"), &mut theme.js),
8991
(theme_dir.join("css/chrome.css"), &mut theme.chrome_css),
92+
(theme_dir.join("css/noscript.css"), &mut theme.noscript_css),
9093
(theme_dir.join("css/general.css"), &mut theme.general_css),
9194
(theme_dir.join("css/print.css"), &mut theme.print_css),
9295
(
@@ -175,6 +178,7 @@ impl Default for Theme {
175178
redirect: REDIRECT.to_owned(),
176179
header: HEADER.to_owned(),
177180
chrome_css: CHROME_CSS.to_owned(),
181+
noscript_css: NOSCRIPT_CSS.to_owned(),
178182
general_css: GENERAL_CSS.to_owned(),
179183
print_css: PRINT_CSS.to_owned(),
180184
variables_css: VARIABLES_CSS.to_owned(),
@@ -235,6 +239,7 @@ mod tests {
235239
"favicon.png",
236240
"favicon.svg",
237241
"css/chrome.css",
242+
"css/noscript.css",
238243
"css/general.css",
239244
"css/print.css",
240245
"css/variables.css",
@@ -264,6 +269,7 @@ mod tests {
264269
redirect: Vec::new(),
265270
header: Vec::new(),
266271
chrome_css: Vec::new(),
272+
noscript_css: Vec::new(),
267273
general_css: Vec::new(),
268274
print_css: Vec::new(),
269275
variables_css: Vec::new(),

tests/init.rs

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ fn copy_theme() {
116116
"book.js",
117117
"css/chrome.css",
118118
"css/general.css",
119+
"css/noscript.css",
119120
"css/print.css",
120121
"css/variables.css",
121122
"favicon.png",

0 commit comments

Comments
 (0)