Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add theme toggle. #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions static/js/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// From https://albertoroura.com/building-a-theme-switcher-for-bootstrap/

function setTheme (mode = 'auto') {
const userMode = localStorage.getItem('bs-theme');
const sysMode = window.matchMedia('(prefers-color-scheme: light)').matches;
const useSystem = mode === 'system' || (!userMode && mode === 'auto');
const modeChosen = useSystem ? 'system' : mode === 'dark' || mode === 'light' ? mode : userMode;

if (useSystem) {
localStorage.removeItem('bs-theme');
} else {
localStorage.setItem('bs-theme', modeChosen);
}

document.documentElement.setAttribute('data-bs-theme', useSystem ? (sysMode ? 'light' : 'dark') : modeChosen);
document.querySelectorAll('.mode-switch .btn').forEach(e => e.classList.remove('text-body'));
document.getElementById(modeChosen).classList.add('text-body');
}

setTheme();
document.querySelectorAll('.mode-switch .btn').forEach(e => e.addEventListener('click', () => setTheme(e.id)));
window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', () => setTheme());
11 changes: 9 additions & 2 deletions templates/_base.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% load static %}
<!DOCTYPE html>
<html lang="en">
<html lang="en" data-bs-theme="auto">

<head>
<meta charset="utf-8">
Expand All @@ -15,6 +15,7 @@
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css" rel="stylesheet">

<link rel="stylesheet" href="{% static 'css/base.css' %}">
{% endblock %}
Expand Down Expand Up @@ -56,7 +57,13 @@
</li>
</ul>
</div>
{% else %}
{% endif %}
<div class="mode-switch me-3">
<button id="dark" class="btn p-0 m-1" title="Dark mode."><i class="bi bi-lightbulb-fill"></i></button>
<button id="light" class="btn p-0 m-1" title="Light mode."><i class="bi bi-lightbulb"></i></button>
<button id="system" class="btn p-0 m-1" title="System preferred mode."><i class="bi bi-sliders"></i></button>
</div>
{% if not user.is_authenticated %}
<div class="mr-auto">
<form class="form d-flex">
<a href="{% url 'account_login' %}" class="btn btn-outline-secondary">Log in</a>
Expand Down