Skip to content

Commit d93fbc0

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

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

src/theme/css/chrome.css

+6-7
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+
html.sidebar-visible #menu-bar {
4646
position: -webkit-sticky;
4747
position: sticky;
4848
top: 0 !important;
@@ -91,7 +91,7 @@ a > .hljs {
9191
display: flex;
9292
margin: 0 5px;
9393
}
94-
.no-js .left-buttons button {
94+
html:not(.js) .left-buttons button {
9595
display: none;
9696
}
9797

@@ -107,7 +107,7 @@ a > .hljs {
107107
overflow: hidden;
108108
text-overflow: ellipsis;
109109
}
110-
.js .menu-title {
110+
.menu-title {
111111
cursor: pointer;
112112
}
113113

@@ -427,8 +427,7 @@ ul#searchresults span.teaser em {
427427
-ms-user-select: none;
428428
user-select: none;
429429
}
430-
.no-js .sidebar,
431-
.js:not(.sidebar-resizing) .sidebar {
430+
html:not(.sidebar-resizing) .sidebar {
432431
transition: transform 0.3s; /* Animation: slide away */
433432
}
434433
.sidebar code {

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, html: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+
html: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

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE HTML>
2-
<html lang="{{ language }}" class="{{ default_theme }}" dir="{{ text_direction }}">
2+
<html lang="{{ language }}" class="{{ default_theme }} sidebar-visible" dir="{{ text_direction }}">
33
<head>
44
<!-- Book generated using mdBook -->
55
<meta charset="UTF-8">
@@ -53,7 +53,7 @@
5353
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
5454
{{/if}}
5555
</head>
56-
<body class="sidebar-visible no-js">
56+
<body>
5757
<div id="body-container">
5858
<!-- Provide site root to javascript -->
5959
<script>
@@ -82,19 +82,16 @@
8282
var theme;
8383
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
8484
if (theme === null || theme === undefined) { theme = default_theme; }
85-
var html = document.querySelector('html');
85+
const html = document.documentElement;
8686
html.classList.remove('{{ default_theme }}')
8787
html.classList.add(theme);
88-
var body = document.querySelector('body');
89-
body.classList.remove('no-js')
90-
body.classList.add('js');
88+
html.classList.add("js");
9189
</script>
9290

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

9593
<!-- Hide / unhide sidebar before it is displayed -->
9694
<script>
97-
var body = document.querySelector('body');
9895
var sidebar = null;
9996
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
10097
if (document.body.clientWidth >= 1080) {
@@ -104,8 +101,8 @@
104101
sidebar = 'hidden';
105102
}
106103
sidebar_toggle.checked = sidebar === 'visible';
107-
body.classList.remove('sidebar-visible');
108-
body.classList.add("sidebar-" + sidebar);
104+
html.classList.remove('sidebar-visible');
105+
html.classList.add("sidebar-" + sidebar);
109106
</script>
110107

111108
<nav id="sidebar" class="sidebar" aria-label="Table of contents">

0 commit comments

Comments
 (0)