Detect Dark Mode Preference Using JavaScript
Description
This snippet detects whether the user prefers dark mode using the window.matchMedia API, helping you deliver a customized experience.
Code Snippet
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (isDarkMode) {
document.body.classList.add('dark-theme');
} else {
document.body.classList.remove('dark-theme');
}