Skip to content

Commit

Permalink
Better automatic dark mode (#1069)
Browse files Browse the repository at this point in the history
* Don't save default theme to localStorage

* Auto enable dark mode on no-js

* Fix light theme with no-js
  • Loading branch information
benediktwerner authored and Dylan-DPC committed Oct 23, 2019
1 parent fcf2d7a commit c9dae17
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/theme/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ function playpen_text(playpen) {
themeToggleButton.focus();
}

function set_theme(theme) {
function set_theme(theme, store = true) {
let ace_theme;

if (theme == 'coal' || theme == 'navy') {
Expand Down Expand Up @@ -331,9 +331,10 @@ function playpen_text(playpen) {
try { previousTheme = localStorage.getItem('mdbook-theme'); } catch (e) { }
if (previousTheme === null || previousTheme === undefined) { previousTheme = default_theme; }

try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
if (store) {
try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
}

document.body.className = theme;
html.classList.remove(previousTheme);
html.classList.add(theme);
}
Expand All @@ -343,7 +344,7 @@ function playpen_text(playpen) {
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }

set_theme(theme);
set_theme(theme, false);

themeToggleButton.addEventListener('click', function () {
if (themePopup.style.display === 'block') {
Expand Down
42 changes: 42 additions & 0 deletions src/theme/css/variables.css
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,45 @@
--searchresults-li-bg: #dec2a2;
--search-mark-bg: #e69f67;
}

@media (prefers-color-scheme: dark) {
.light.no-js {
--bg: hsl(200, 7%, 8%);
--fg: #98a3ad;

--sidebar-bg: #292c2f;
--sidebar-fg: #a1adb8;
--sidebar-non-existant: #505254;
--sidebar-active: #3473ad;
--sidebar-spacer: #393939;

--scrollbar: var(--sidebar-fg);

--icons: #43484d;
--icons-hover: #b3c0cc;

--links: #2b79a2;

--inline-code-color: #c5c8c6;;

--theme-popup-bg: #141617;
--theme-popup-border: #43484d;
--theme-hover: #1f2124;

--quote-bg: hsl(234, 21%, 18%);
--quote-border: hsl(234, 21%, 23%);

--table-border-color: hsl(200, 7%, 13%);
--table-header-bg: hsl(200, 7%, 28%);
--table-alternate-bg: hsl(200, 7%, 11%);

--searchbar-border-color: #aaa;
--searchbar-bg: #b7b7b7;
--searchbar-fg: #000;
--searchbar-shadow-color: #aaa;
--searchresults-header-fg: #666;
--searchresults-border-color: #98a3ad;
--searchresults-li-bg: #2b2b2f;
--search-mark-bg: #355c7d;
}
}
11 changes: 7 additions & 4 deletions src/theme/index.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE HTML>
<html lang="{{ language }}" class="sidebar-visible no-js">
<html lang="{{ language }}" class="sidebar-visible no-js {{ default_theme }}">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
Expand Down Expand Up @@ -39,7 +39,7 @@
<script async type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
{{/if}}
</head>
<body class="{{ default_theme }}">
<body>
<!-- Provide site root to javascript -->
<script type="text/javascript">
var path_to_root = "{{ path_to_root }}";
Expand Down Expand Up @@ -67,8 +67,11 @@
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
document.body.className = theme;
document.querySelector('html').className = theme + ' js';
var html = document.querySelector('html');
html.classList.remove('no-js')
html.classList.remove('{{ default_theme }}')
html.classList.add(theme);
html.classList.add('js');
</script>

<!-- Hide / unhide sidebar before it is displayed -->
Expand Down

0 comments on commit c9dae17

Please sign in to comment.