Skip to content

Commit e23478c

Browse files
committed
Dark and Light switch
With the current modification the theme selected is stored locally with an eventListener that stores that information and asks for it. By default the website will be on a white theme, unless the user changes it, in theory the selection should stay fixed even when the user leave the site and comes back later. For the icons I used a the default windows emojis of the Sun and moon, which I think work great and show it off to its best abilities. Additionally I left the Border on the icon so indicate that it can be selected or changed. Doesn't seem to have a performance issues. Does have an issue that page by default is white themed, so upon changing it flickers with the lights, occasionally on slow internets it may show behind the scenes of the website which could be an issue but that is up for debate. See files changed config -> Selects the default color scheme Aux_nav -> adds a new nav list item to the top right panel where " Contribute to .. " is located as a label button Head.html -> Modifies the stylesheets and adds a script of the default " just the docs " theme .js Essentially it has been fixated to the original code Default.html -> Adds our local script " colorThemeSelection.js " this is what controls the theme button and selection and load. ColorThemeSelection.js -> As described
1 parent 3c52543 commit e23478c

5 files changed

Lines changed: 92 additions & 2 deletions

File tree

_config.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ plugins:
1414

1515
# Optional. The default date format, used if none is specified in the tag.
1616
last-modified-at:
17-
date-format: '%d/%m/%Y'
17+
date-format: '%d/%m/%Y'
18+
19+
# Color Theme
20+
color_scheme: light

_includes/components/aux_nav.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<nav aria-label="Auxiliary" class="aux-nav">
2+
<ul class="aux-nav-list">
3+
{% for link in site.aux_links %}
4+
<li class="aux-nav-list-item">
5+
<a href="{{ link.last }}" class="site-button"
6+
{% if site.aux_links_new_tab %}
7+
target="_blank" rel="noopener noreferrer"
8+
{% endif %}
9+
>
10+
{{ link.first }}
11+
</a>
12+
</li>
13+
{% endfor %}
14+
<li class="aux-nav-list-item"></li>
15+
<button class="btn js-toggle-dark-mode"aria-label="Switch to dark mode">🌞</button>
16+
17+
</li>
18+
</ul>
19+
</nav>

_includes/components/head.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{%- comment -%}
2+
Include as: {%- include head.html -%}
3+
Depends on: site.ga_tracking, site.ga_tracking_anonymize_ip,
4+
site.search_enabled, site.static_files, site.favicon_ico.
5+
Results in: HTML for the head element.
6+
Includes:
7+
head_nav.html, head_custom.html.
8+
Overwrites:
9+
ga_tracking_ids, ga_property, file, favicon.
10+
Should not be cached, because included files depend on page.
11+
{%- endcomment -%}
12+
13+
<head>
14+
<meta charset="UTF-8">
15+
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
16+
17+
<link rel="stylesheet" href="{{ '/assets/css/just-the-docs-default.css' | relative_url }}">
18+
19+
{% include head_nav.html %}
20+
21+
{% if site.search_enabled != false %}
22+
<script src="{{ '/assets/js/vendor/lunr.min.js' | relative_url }}"></script>
23+
{% endif %}
24+
25+
<script src="{{ '/assets/js/just-the-docs.js' | relative_url }}"></script>
26+
27+
28+
<meta name="viewport" content="width=device-width, initial-scale=1">
29+
30+
{% include_cached favicon.html %}
31+
32+
{% seo %}
33+
34+
{% include head_custom.html %}
35+
36+
</head>

_layouts/default.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
---
66

77
<!DOCTYPE html>
8-
98
<html lang="{{ site.lang | default: 'en-US' }}">
109
{% include head.html %}
1110
<body>
@@ -61,5 +60,7 @@
6160
{% if page.include_programming_language_switch_script %}
6261
<script src="{{ '/assets/js/programmingLanguageSwitch.js' | relative_url }}" defer></script>
6362
{% endif %}
63+
64+
<script src="{{ '/assets/js/colorThemeSelection.js' | relative_url }}" defer></script>
6465
</body>
6566
</html>

assets/js/colorThemeSelection.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Get the stored theme when the page loads
2+
document.addEventListener('DOMContentLoaded', (event) => {
3+
const storedTheme = localStorage.getItem('theme');
4+
if (storedTheme) {
5+
jtd.setTheme(storedTheme);
6+
// Set the button icon and aria-label based on the stored theme
7+
toggleDarkMode.textContent = storedTheme === 'dark' ? '🌞' : '🌜';
8+
toggleDarkMode.setAttribute('aria-label', storedTheme === 'dark' ? 'Switch to light mode' : 'Switch to dark mode');
9+
}
10+
});
11+
12+
const toggleDarkMode = document.querySelector('.js-toggle-dark-mode');
13+
14+
// Set the initial font size
15+
toggleDarkMode.style.fontSize = '1.5em';
16+
17+
toggleDarkMode.addEventListener('click', function() {
18+
if (jtd.getTheme() === 'dark') {
19+
jtd.setTheme('light');
20+
toggleDarkMode.textContent = '🌜';
21+
toggleDarkMode.setAttribute('aria-label', 'Switch to dark mode');
22+
// Store the theme in localStorage
23+
localStorage.setItem('theme', 'light');
24+
} else {
25+
jtd.setTheme('dark');
26+
toggleDarkMode.textContent = '🌞';
27+
toggleDarkMode.setAttribute('aria-label', 'Switch to light mode');
28+
// Store the theme in localStorage
29+
localStorage.setItem('theme', 'dark');
30+
}
31+
});

0 commit comments

Comments
 (0)