Skip to content

Commit ea2c704

Browse files
authored
Dark and Light switch (#78)
The current modification implements local storage for the selected theme using an event listener. This ensures that the chosen theme persists even when users leave and return to the site. By default, the website adopts a white theme, but users can change it. The selected theme should remain fixed upon subsequent visits. For the theme selection, I've utilized default Windows emojis for the Sun and moon icons, enhancing the visual appeal. Notably, I've maintained a visible border on the icon to signify its selectability. There don't seem to be any performance issues, but there's a minor inconvenience where the page initially loads with a white theme before flickering to the selected theme. This may be more noticeable on slower internet connections, potentially revealing the behind-the-scenes of the website. Whether this is a significant issue is open to discussion. Key changes can be found in the following files: 1. config: Sets the default color scheme. 2. Aux_nav: Adds a new navigation list item to the top-right panel, labeled "Contribute to.." 3 Head.html: Modifies stylesheets and introduces a script for the default "just the docs" theme.js, essentially fixing it to the original code. 4. Default.html: Incorporates the local script "colorThemeSelection.js," responsible for controlling the theme button, selection, and loading. 5. ColorThemeSelection.js: Enhances readability and functionality as described. Issues --- The following issues are as listed: - Theme by default is forced to be " light " themed, which means that upon page change it has to be overwritten which introduces flickering to the website when it is changed. - May have issues on very slow internet where the user could see the backsite, but shouldn't be too worried about it performance seem to be valid for the site still. Fixes --- It would be possible to force load stylesheets globally so that the theme changes it for the entire site, but it has underlying issues with the way ruby website is setup and might need internal code changes.
2 parents b19bdbf + e23478c commit ea2c704

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)