mirror of
https://github.com/rickbarrette/redmine_dark_mode.git
synced 2026-11-17 12:50:36 -05:00
A dark mode for Redmine 7 in Gitea's dark theme colours. It follows the device by default, with a per-user choice in My account (Automatic, Off, On) and a sun/moon toggle in the top bar. The mode is set before the page paints, and pictures are never inverted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
56 lines
2.4 KiB
JavaScript
56 lines
2.4 KiB
JavaScript
// The top menu's dark mode toggle: switches between light and dark right away and saves the choice (with the
|
|
// user's preferences when logged in, else in this browser). "Automatic" is chosen in My account.
|
|
(function () {
|
|
var MOON = '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8z" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
|
|
var SUN = '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"/></g></svg>';
|
|
|
|
function isDark() {
|
|
return document.documentElement.classList.contains('dark-mode');
|
|
}
|
|
|
|
function save(mode) {
|
|
var dm = window.RedmineDarkMode;
|
|
if (!dm.loggedIn) {
|
|
try { localStorage.setItem('redmine_dark_mode', mode); } catch (e) {}
|
|
return;
|
|
}
|
|
var token = document.querySelector('meta[name="csrf-token"]');
|
|
var body = new URLSearchParams({ mode: mode });
|
|
fetch((window.relativeUrlRoot || '') + '/dark_mode', {
|
|
method: 'POST',
|
|
credentials: 'same-origin',
|
|
headers: { 'X-CSRF-Token': token ? token.content : '', 'X-Requested-With': 'XMLHttpRequest' },
|
|
body: body
|
|
});
|
|
}
|
|
|
|
function render(button) {
|
|
var dm = window.RedmineDarkMode;
|
|
var label = isDark() ? dm.labels.light : dm.labels.dark;
|
|
button.innerHTML = isDark() ? SUN : MOON;
|
|
button.title = label;
|
|
button.setAttribute('aria-label', label);
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
var menu = document.querySelector('#top-menu .profile-menu');
|
|
if (!menu || !window.RedmineDarkMode) return;
|
|
|
|
var button = document.createElement('button');
|
|
button.type = 'button';
|
|
button.className = 'dark-mode-toggle';
|
|
render(button);
|
|
button.addEventListener('click', function () {
|
|
var mode = isDark() ? 'light' : 'dark';
|
|
window.RedmineDarkMode.set(mode);
|
|
render(button);
|
|
save(mode);
|
|
});
|
|
menu.insertBefore(button, menu.firstChild);
|
|
|
|
// The device switching light/dark (Automatic) changes the icon too
|
|
var media = window.matchMedia('(prefers-color-scheme: dark)');
|
|
if (media.addEventListener) media.addEventListener('change', function () { render(button); });
|
|
});
|
|
})();
|