Skip to content

Commit

Permalink
feat: Add dark mode toggle functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Feb 11, 2024
1 parent 2f56733 commit a9af577
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions wiki/darkmode-toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const themeToggleButton = document.getElementById('theme-toggle');

Check warning on line 1 in wiki/darkmode-toggle.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

Use the global form of 'use strict'
const themePreferenceKey = 'userThemePreference';
const darkThemePath = 'wiki/darkmode.css';

function applyTheme(theme) {
let link = document.querySelector('link[href="' + darkThemePath + '"]');
if (theme === 'dark') {
if (!link) {
link = document.createElement('link');
link.rel = 'stylesheet';
link.href = darkThemePath;
document.head.appendChild(link);
}
} else {
if (link) {
document.head.removeChild(link);
}
}
}

function loadTheme() {
const savedTheme = localStorage.getItem(themePreferenceKey);
applyTheme(savedTheme);
}

function toggleTheme() {
const currentTheme = localStorage.getItem(themePreferenceKey) === 'dark' ? 'light' : 'dark';
localStorage.setItem(themePreferenceKey, currentTheme);
applyTheme(currentTheme);
}

themeToggleButton.addEventListener('click', toggleTheme);

loadTheme();

0 comments on commit a9af577

Please sign in to comment.