Skip to content
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

Better automatic dark mode #1069

Merged
merged 3 commits into from
Oct 23, 2019
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
9 changes: 5 additions & 4 deletions src/theme/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,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 @@ -356,9 +356,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 @@ -368,7 +369,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