Merge pull request #164 from TobiasBerg/docs-auto-theme

Set Dark/Light Mode in Docs Based on Browser Preference
This commit is contained in:
Guillermo Villar 2026-02-16 17:45:18 +01:00 committed by GitHub
commit a335d6835b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 2 deletions

View File

@ -1053,12 +1053,17 @@
const icon = document.getElementById('theme-icon'); const icon = document.getElementById('theme-icon');
icon.textContent = newTheme === 'light' ? '🌙' : '☀️'; icon.textContent = newTheme === 'light' ? '🌙' : '☀️';
} }
function getBrowserThemePreference() {
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
return prefersDarkMode ? 'dark' : 'light';
}
// Load saved theme on page load // Load saved theme on page load
(function() { (function() {
const savedTheme = localStorage.getItem('theme') || 'light'; const savedTheme = localStorage.getItem('theme') || getBrowserThemePreference();
document.documentElement.setAttribute('data-theme', savedTheme); document.documentElement.setAttribute('data-theme', savedTheme);
// Update icon // Update icon
const icon = document.getElementById('theme-icon'); const icon = document.getElementById('theme-icon');
icon.textContent = savedTheme === 'light' ? '🌙' : '☀️'; icon.textContent = savedTheme === 'light' ? '🌙' : '☀️';