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

Prefers color scheme #966

Closed
Closed
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
3 changes: 2 additions & 1 deletion _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">

<link rel="stylesheet" href="{{ '/assets/css/just-the-docs-default.css' | relative_url }}">
<link rel="stylesheet" href="{{ '/assets/css/just-the-docs-light.css' | relative_url }}" media="(prefers-color-scheme: light)">
<link rel="stylesheet" href="{{ '/assets/css/just-the-docs-dark.css' | relative_url }}" media="(prefers-color-scheme: dark)">

{% if site.ga_tracking != nil %}
{% assign ga_tracking_ids = site.ga_tracking | split: "," %}
Expand Down
13 changes: 11 additions & 2 deletions assets/js/just-the-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,21 @@ function searchLoaded(index, docs) {
// Switch theme

jtd.getTheme = function() {
var cssFileHref = document.querySelector('[rel="stylesheet"]').getAttribute('href');
var cssFile = document.querySelector('[rel="stylesheet"]');
if(cssFile.hasAttribute('media')) return 'auto';

var cssFileHref = cssFile.getAttribute('href');
return cssFileHref.substring(cssFileHref.lastIndexOf('-') + 1, cssFileHref.length - 4);
}

jtd.setTheme = function(theme) {
var cssFile = document.querySelector('[rel="stylesheet"]');
var cssFiles = [...document.querySelectorAll('[rel="stylesheet"]')];
var cssFile = cssFiles[0];
if(cssFiles.length >= 1) {
cssFiles.shift();
cssFiles.forEach(it => it.remove());
cssFile.removeAttribute('media');
}
cssFile.setAttribute('href', '{{ "assets/css/just-the-docs-" | relative_url }}' + theme + '.css');
}

Expand Down