Skip to content

Commit

Permalink
add theme mode switch
Browse files Browse the repository at this point in the history
  • Loading branch information
kritzl committed Jul 12, 2024
1 parent 63fad2e commit 7df8ee1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/vinywaji/gui/static_src/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const colors = require('tailwindcss/colors')
const theme_color = process.env.VW_THEME_COLOR || 'teal';

module.exports = {
darkMode: 'selector',
content: [
/**
* HTML. Paths to Django template files that will contain Tailwind CSS classes.
Expand Down
36 changes: 36 additions & 0 deletions src/vinywaji/gui/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,42 @@
{% include "components/footer.html" %}
{% block scripts %}
<script type="application/javascript" src="{% static 'base.js' %}"></script>
<script>
let mode = localStorage.theme ?? 'system';

function updateMode() {
if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark')
} else {
document.documentElement.classList.remove('dark')
}

document.querySelector('#modeSwitch').dataset.mode = mode;
}
updateMode();

function switchMode() {
switch (mode) {
case 'light':
localStorage.theme = 'dark';
mode = 'dark';
break;
case 'dark':
localStorage.removeItem('theme')
mode = 'system';
break;
case 'system':
localStorage.theme = 'light';
mode = 'light';
break;
}
updateMode();
}

document.querySelector('#modeSwitch').addEventListener('click', (e) => {
switchMode();
})
</script>
{% block scripts-extra %}{% endblock %}
{% endblock %}
{% endblock %}
Expand Down
11 changes: 8 additions & 3 deletions src/vinywaji/gui/templates/components/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
<div class="bg-primary-400 dark:bg-primary-600 text-black dark:text-white flex justify-between px-2 items-stretch">
<a href="/" class="text-3xl font-bold place-self-center">{{ title }}</a>
<ul class="flex items-stretch">
<li><a class="block px-2 leading-[4rem] hover:bg-primary-700 transition-colors" href="#"
onclick="switchMode()" id="modeSwitch">Mode</a></li>
{% if request.user.is_anonymous %}
<li><a class="block px-2 leading-[4rem] hover:bg-primary-700 transition-colors" href="{% url 'simple_openid_connect:login' %}">Login</a></li>
<li><a class="block px-2 leading-[4rem] hover:bg-primary-700 transition-colors"
href="{% url 'simple_openid_connect:login' %}">Login</a></li>
{% else %}
<li><a class="block px-2 leading-[4rem] hover:bg-primary-700 transition-colors" href="{% url 'profile' %}">Profile</a></li>
<li><a class="block px-2 leading-[4rem] hover:bg-primary-700 transition-colors" href="{% url 'simple_openid_connect:logout' %}">Logout</a></li>
<li><a class="block px-2 leading-[4rem] hover:bg-primary-700 transition-colors"
href="{% url 'profile' %}">Profile</a></li>
<li><a class="block px-2 leading-[4rem] hover:bg-primary-700 transition-colors"
href="{% url 'simple_openid_connect:logout' %}">Logout</a></li>
{% endif %}
</ul>
</div>
Expand Down

0 comments on commit 7df8ee1

Please sign in to comment.