PageSourceSearch

https://www.ashtonbradley.nz/js/darkmode.js

js ashtonbradley.nz collected 2026-10-06 13:29:02 UTC 2,078 bytes, 35 lines download raw bytes

1document.addEventListener('DOMContentLoaded', function () {
2    const darkModeToggle = document.getElementById('darkModeToggle');
3    const body = document.body;
4    const darkModeIconContainer = document.getElementById('darkModeIconContainer');
5
6    // Check if dark mode is stored in localStorage, or use system preference
7    const isDarkModeStored = localStorage.getItem('darkMode');
8    const isLightModePreferred = window.matchMedia('(prefers-color-scheme: light)').matches;
9
10
11    // Default to dark mode unless the user explicitly prefers light mode
12    if (isLightModePreferred) {
13        updateDarkModeIcon('sun');
14    } else {
15        body.classList.add('dark-mode');
16        updateDarkModeIcon('moon');   
17    }
18
19    darkModeToggle.addEventListener('click', function () {
20        body.classList.toggle('dark-mode');
21        const isDarkMode = body.classList.contains('dark-mode');
22        updateDarkModeIcon(isDarkMode ? 'moon' : 'sun');
23        localStorage.setItem('darkMode', isDarkMode.toString());
24    });
25
26    function updateDarkModeIcon(icon) {
27        darkModeIconContainer.innerHTML = getIconSvgContent(icon);
28    }
29
30    function getIconSvgContent(icon) {
31        return icon === 'moon'
32            ? '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"><path fill="currentColor" fill-rule="evenodd" d="M10 2a1 1 0 0 1 1 1v1a1 1 0 1 1-2 0V3a1 1 0 0 1 1-1m4 8a4 4 0 1 1-8 0a4 4 0 0 1 8 0m-.464 4.95l.707.707a1 1 0 0 0 1.414-1.414l-.707-.707a1 1 0 0 0-1.414 1.414m2.12-10.607a1 1 0 0 1 0 1.414l-.706.707a1 1 0 1 1-1.414-1.414l.707-.707a1 1 0 0 1 1.414 0M17 11a1 1 0 1 0 0-2h-1a1 1 0 1 0 0 2zm-7 4a1 1 0 0 1 1 1v1a1 1 0 1 1-2 0v-1a1 1 0 0 1 1-1M5.05 6.464A1 1 0 1 0 6.465 5.05l-.708-.707a1 1 0 0 0-1.414 1.414zm1.414 8.486l-.707.707a1 1 0 0 1-1.414-1.414l.707-.707a1 1 0 0 1 1.414 1.414M4 11a1 1 0 1 0 0-2H3a1 1 0 0 0 0 2z" clip-rule="evenodd"/></svg>'
33            : '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"><path fill="currentColor" d="M17.293 13.293A8 8 0 0 1 6.707 2.707a8.001 8.001 0 1 0 10.586 10.586"/></svg>';
34    }
35});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.