generated from kevinlin1/just-the-class
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reasonably happy with the light/dark mode toggle
* button in the bottom of the sidebar * respects the browser pref is avaiable * saves setting to localStorage * provides an unset button to clear localStorage
- Loading branch information
1 parent
59b1556
commit a426563
Showing
5 changed files
with
87 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
<!-- This file comes from Just The Docs. | ||
This footer should be included on all EECS/DS sites. | ||
--> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
<!-- This file adds links at the bottom of the sidebar on the left. --> | ||
|
||
{% if site.class_archive_path %} | ||
<section class="site-footer" aria-label="Footer: site archive"> | ||
<section class="site-footer py-1" aria-label="Footer: site archive"> | ||
<a href="{{ site.class_archive_path }}" | ||
target="_blank" rel="noopener nofollow">View all course offerings | ||
<i class="fa-solid fa-external-link" aria-hidden="true"></i></a> | ||
</section> | ||
{% endif %} | ||
|
||
{% if site.hide_color_scheme_toggle != 'true' %} | ||
<div class="site-footer m-1"> | ||
{% include toggle-color-scheme.html %} | ||
</div> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
{% capture btn_aria_label %} | ||
{%- if site.color_scheme == 'dark' -%} | ||
Switch to Light Mode | ||
{%- else -%} | ||
Switch to Dark Mode | ||
{%- endif -%} | ||
{% endcapture %} | ||
|
||
<span class="fs-3"> | ||
<button type="button" | ||
class="btn js-toggle-dark-mode btn-outline" | ||
style="margin-right: -2px; border-top-right-radius: 0; border-bottom-right-radius: 0;" | ||
aria-label="{{ btn_aria_label }}"> | ||
{%- if site.color_scheme == 'dark' -%} | ||
🔆 Light Mode | ||
{%- else -%} | ||
🌙 Dark Mode | ||
{%- endif -%} | ||
</button> | ||
<button type="button" | ||
class="btn js-unset-color-scheme btn-outline px-1" | ||
style="margin-left: -2px; border-top-left-radius: 0; border-bottom-left-radius: 0;" | ||
aria-label="Reset color scheme" | ||
>⎌</button> | ||
</span> | ||
|
||
<script> | ||
document.addEventListener("DOMContentLoaded", function() { | ||
if (!jtd) { return; } | ||
|
||
const toggleSchemeBtn = document.querySelector('.js-toggle-dark-mode'); | ||
const unsetColorScheme = document.querySelector('.js-unset-color-scheme'); | ||
|
||
// If the browser suggests a color scheme, and user hasn't saved/switch one yet.. | ||
if (window.matchMedia && !localStorage['jtd-theme']) { | ||
if (window.matchMedia('(prefers-color-scheme: dark)').matches) { | ||
jtd.setTheme('dark'); | ||
toggleSchemeBtn.textContent = '🔆 Light Mode'; | ||
toggleSchemeBtn.ariaLabel = 'Switch to Light Mode'; | ||
} else if (window.matchMedia('(prefers-color-scheme: light)').matches) { | ||
jtd.setTheme('light'); | ||
toggleSchemeBtn.textContent = '🌙 Dark Mode'; | ||
toggleSchemeBtn.ariaLabel = 'Switch to Dark Mode'; | ||
} | ||
} else if (localStorage['jtd-theme']) { | ||
jtd.setTheme(localStorage['jtd-theme']); | ||
} | ||
|
||
jtd.addEvent(toggleSchemeBtn, 'click', function() { | ||
if (jtd.getTheme() === 'dark') { | ||
localStorage['jtd-theme'] = 'light'; | ||
jtd.setTheme('light'); | ||
toggleSchemeBtn.textContent = '🌙 Dark Mode'; | ||
toggleSchemeBtn.ariaLabel = 'Switch to Dark Mode'; | ||
} else { | ||
jtd.setTheme('dark'); | ||
localStorage['jtd-theme'] = 'dark'; | ||
toggleSchemeBtn.textContent = '🔆 Light Mode'; | ||
toggleSchemeBtn.ariaLabel = 'Switch to Light Mode'; | ||
} | ||
}); | ||
|
||
// Add a way to unset the saved theme. | ||
// "default" respect's the site config setting, but not the user browser pref | ||
// So, check the pref and set the theme to the preferred one, if possible. | ||
jtd.addEvent(unsetColorScheme, 'click', function() { | ||
delete localStorage['jtd-theme']; | ||
|
||
if (window.matchMedia) { | ||
if (window.matchMedia('(prefers-color-scheme: dark)').matches) { | ||
jtd.setTheme('dark'); | ||
} else if (window.matchMedia('(prefers-color-scheme: light)').matches) { | ||
jtd.setTheme('light'); | ||
} | ||
} else { | ||
jtd.setTheme('default'); | ||
} | ||
}); | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters