Skip to content

Commit

Permalink
Added "follow system" option to theme toggle (alshedivat#2261)
Browse files Browse the repository at this point in the history
The theme toggle button now has a third option, which follows the user's
system preferences.

- In the code there's now a distinction between the theme setting (which
can be "dark", "light" or "system") and the computed theme.
- The theme setting is now stored as the "theme-setting" local storage
variable. Since this is different from the old variable ("theme"), this
will effectively reset all current user themes to the default "system".
Maybe this is not what you want.
- The "system" theme icon is currently a half circle symbol.
- The toggle button now displays the current theme setting, rather than
the next theme setting (as far as I know this is consistent with other
sites which have three theme options).
- `theme.js` is now loaded regardless of `site.enable_darkmode`. This is
because other scripts which were always loaded relied on being able to
determine the theme. `theme.js` no longer initialises the theme itself
though; this only happens when `site.enable_darkmode`.
- When the theme setting is "system", the theme will change immediately
whenever the user changes their system theme.

alshedivat#2261

---------

Signed-off-by: George Araujo <[email protected]>
Co-authored-by: George Araujo <[email protected]>
  • Loading branch information
SamAdamDay and george-gca authored Mar 14, 2024
1 parent 6cf110a commit c788a30
Show file tree
Hide file tree
Showing 25 changed files with 42,162 additions and 67 deletions.
2 changes: 1 addition & 1 deletion _includes/giscus.liquid
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div id="giscus_thread" style="max-width: {{ site.max_width }}; margin: 0 auto;">
{% if site.giscus.repo %}
<script>
let giscusTheme = localStorage.getItem('theme');
let giscusTheme = determineComputedTheme();
let giscusAttributes = {
src: 'https://giscus.app/client.js',
'data-repo': '{{ site.giscus.repo }}',
Expand Down
5 changes: 4 additions & 1 deletion _includes/head.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<link rel="canonical" href="{{ page.url | replace:'index.html','' | absolute_url }}">

<!-- Dark Mode -->
<script src="{{ '/assets/js/theme.js' | relative_url | bust_file_cache }}"></script>
{% if site.enable_darkmode %}
<link
defer
Expand All @@ -60,7 +61,9 @@
media="none"
id="highlight_theme_dark"
>
<script src="{{ '/assets/js/theme.js' | relative_url | bust_file_cache }}"></script>
<script>
initTheme();
</script>
{% endif %}

<!-- GeoJSON support via Leaflet -->
Expand Down
5 changes: 3 additions & 2 deletions _includes/header.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@
<!-- Toogle theme mode -->
<li class="toggle-container">
<button id="light-toggle" title="Change theme">
<i class="fa-solid fa-moon"></i>
<i class="fa-solid fa-sun"></i>
<i class="ti ti-sun-moon" id="light-toggle-system"></i>
<i class="ti ti-moon-filled" id="light-toggle-dark"></i>
<i class="ti ti-sun-filled" id="light-toggle-light"></i>
</button>
</li>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion _includes/scripts/diff2html.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
crossorigin="anonymous"
></script>
<script>
let theme = localStorage.getItem('theme');
let theme = determineComputedTheme();
/* Create echarts chart as another node and hide the code block, appending the echarts node after it
this is done to enable retrieving the code again when changing theme between light/dark */
Expand Down
2 changes: 1 addition & 1 deletion _includes/scripts/echarts.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
></script>
{% endif %}
<script>
let theme = localStorage.getItem('theme');
let theme = determineComputedTheme();
/* Create echarts chart as another node and hide the code block, appending the echarts node after it
this is done to enable retrieving the code again when changing theme between light/dark */
Expand Down
2 changes: 1 addition & 1 deletion _includes/scripts/mermaid.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
></script>
{% endif %}
<script>
let theme = localStorage.getItem('theme');
let theme = determineComputedTheme();
/* Create mermaid diagram as another node and hide the code block, appending the mermaid node after it
this is done to enable retrieving the code again when changing theme between light/dark */
Expand Down
2 changes: 1 addition & 1 deletion _includes/scripts/vega.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
></script>

<script>
let theme = localStorage.getItem('theme');
let theme = determineComputedTheme();
/* Create vega lite chart as another node and hide the code block, appending the vega lite node after it
this is done to enable retrieving the code again when changing theme between light/dark */
Expand Down
44 changes: 36 additions & 8 deletions _sass/_themes.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@
--global-danger-block-text: #600;
--global-danger-block-title: #c00;

.fa-sun {
display: none;
}
.fa-moon {
#light-toggle-system {
padding-left: 10px;
padding-top: 12px;
display: block;
}

#light-toggle-dark {
display: none;
}

#light-toggle-light {
display: none;
}

.repo-img-light {
display: block;
}
Expand Down Expand Up @@ -75,19 +80,42 @@ html[data-theme="dark"] {
--global-danger-block-text: #600;
--global-danger-block-title: #c00;

.fa-sun {
.repo-img-light {
display: none;
}
.repo-img-dark {
display: block;
}
}

html[data-theme-setting="dark"] {
#light-toggle-system {
display: none;
}

#light-toggle-dark {
padding-left: 10px;
padding-top: 12px;
display: block;
}
.fa-moon {

#light-toggle-light {
display: none;
}
}

.repo-img-light {
html[data-theme-setting="light"] {
#light-toggle-system {
display: none;
}
.repo-img-dark {

#light-toggle-dark {
display: none;
}

#light-toggle-light {
padding-left: 10px;
padding-top: 12px;
display: block;
}
}
3 changes: 3 additions & 0 deletions _sass/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ $code-bg-color-dark: #2c3237 !default;

// Font awesome location
$fa-font-path: "../webfonts";

// Tabler icons location
$ti-font-path: "../fonts";
Loading

0 comments on commit c788a30

Please sign in to comment.