fix(ui): ensure theme-toggle button is visible on first visit

Signed-off-by: ernolf <raphael.gradenwitz@googlemail.com>
This commit is contained in:
ernolf 2024-10-17 17:18:58 +02:00
parent 2b6e81c074
commit a5b25ab56b
No known key found for this signature in database
GPG key ID: 0B145139A170715C
2 changed files with 13 additions and 11 deletions

View file

@ -1,7 +1,7 @@
// Function to toggle theme
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = (currentTheme === 'dark') ? 'light' : 'dark';
const newTheme = (currentTheme === 'dark') ? '' : 'dark'; // Toggle between no theme and dark theme
document.documentElement.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
@ -13,14 +13,14 @@ function toggleTheme() {
// Function to apply saved theme from localStorage
function applySavedTheme() {
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
document.documentElement.setAttribute('data-theme', savedTheme);
// Ensure the icon is set correctly based on the saved theme
const themeIcon = document.getElementById('theme-icon');
themeIcon.textContent = savedTheme === 'dark' ? '☀️' : '🌙';
if (savedTheme === 'dark') {
document.documentElement.setAttribute('data-theme', 'dark');
document.getElementById('theme-icon').textContent = '☀️'; // Sun icon for dark mode
} else {
document.documentElement.removeAttribute('data-theme'); // Default to light theme (no data-theme)
document.getElementById('theme-icon').textContent = '🌙'; // Moon icon for light mode
}
}
// Apply theme when the page loads
document.addEventListener('DOMContentLoaded', applySavedTheme);
// Immediately apply the saved theme
applySavedTheme();

View file

@ -4,16 +4,18 @@
<link rel="stylesheet" href="/style.css?v3" media="all" />
<link rel="icon" href="/img/favicon.png">
<script type="text/javascript" src="forms.js"></script>
<script type="text/javascript" src="toggle-dark-mode.js"></script>
</head>
<body>
<div class="wrapper">
{% block body %}{% endblock %}
</div>
<button id="theme-toggle" onclick="toggleTheme()"><span id="theme-icon"/></button>
<script type="text/javascript" src="toggle-dark-mode.js"></script>
<div id="overlay">
<div class="loader"></div>
</div>
<button id="theme-toggle" onclick="toggleTheme()">
<span id="theme-icon">🌙</span>
</button>
</body>
</html>