|
| 1 | +const htmlElement = document.documentElement; |
| 2 | +const colorThemeSelector = document.querySelector( |
| 3 | + "[data-color-theme-selector]" |
| 4 | +); |
| 5 | +const colorThemeSelectorInput = colorThemeSelector.querySelector("select"); |
| 6 | +const colorThemeSelectorInputIcon = colorThemeSelector.querySelector('.color-theme-selector-wrapper-icon'); |
| 7 | +const colorThemeSelectorInputIconSrc = colorThemeSelectorInputIcon.querySelector('use'); |
| 8 | +const storedTheme = localStorage.theme; |
| 9 | + |
| 10 | +function setInputIcon(type) { |
| 11 | + if(type === 'dark') { |
| 12 | + colorThemeSelectorInputIconSrc.setAttribute('xlink:href', '#icon-color-theme-dark'); |
| 13 | + } else if(type === 'light') { |
| 14 | + colorThemeSelectorInputIconSrc.setAttribute('xlink:href', '#icon-color-theme-light'); |
| 15 | + } else { |
| 16 | + colorThemeSelectorInputIconSrc.setAttribute('xlink:href', '#icon-color-theme-system'); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +function setInputState(storedTheme) { |
| 21 | + if (!!storedTheme) { |
| 22 | + colorThemeSelectorInput.querySelector( |
| 23 | + `[value="${storedTheme}"]` |
| 24 | + ).selected = true; |
| 25 | + |
| 26 | + setInputIcon(storedTheme); |
| 27 | + |
| 28 | + htmlElement.classList.add(storedTheme); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function displayThemeSelector() { |
| 33 | + colorThemeSelector.style.visibility = 'visible'; |
| 34 | +} |
| 35 | + |
| 36 | +function toggleThemeClass(type) { |
| 37 | + if (type === "dark") { |
| 38 | + if (htmlElement.classList.contains("light")) { |
| 39 | + htmlElement.classList.remove("light"); |
| 40 | + } |
| 41 | + |
| 42 | + htmlElement.classList.add("dark"); |
| 43 | + } else if (type == "light") { |
| 44 | + if (htmlElement.classList.contains("dark")) { |
| 45 | + htmlElement.classList.remove("dark"); |
| 46 | + } |
| 47 | + |
| 48 | + htmlElement.classList.add("light"); |
| 49 | + } else { |
| 50 | + // "System" mode - blanket remove all theme classes from the root <html> element |
| 51 | + if (htmlElement.classList.contains("dark")) { |
| 52 | + htmlElement.classList.remove("dark"); |
| 53 | + } |
| 54 | + |
| 55 | + if (htmlElement.classList.contains("light")) { |
| 56 | + htmlElement.classList.remove("light"); |
| 57 | + } |
| 58 | + |
| 59 | + // If the current system color scheme preference is dark, apply the .dark class |
| 60 | + if (window.matchMedia("(prefers-color-scheme: dark)").matches) { |
| 61 | + htmlElement.classList.add("dark"); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function updateTheme(type) { |
| 67 | + switch (type) { |
| 68 | + case "dark": |
| 69 | + toggleThemeClass("dark"); |
| 70 | + |
| 71 | + setInputIcon('dark'); |
| 72 | + |
| 73 | + localStorage.theme = "dark"; |
| 74 | + break; |
| 75 | + case "light": |
| 76 | + toggleThemeClass("light"); |
| 77 | + |
| 78 | + setInputIcon('light'); |
| 79 | + |
| 80 | + localStorage.theme = "light"; |
| 81 | + break; |
| 82 | + default: |
| 83 | + toggleThemeClass("system"); |
| 84 | + |
| 85 | + setInputIcon('system'); |
| 86 | + |
| 87 | + localStorage.removeItem("theme"); |
| 88 | + break; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +setInputState(storedTheme); |
| 93 | +displayThemeSelector(); |
| 94 | + |
| 95 | +colorThemeSelectorInput.addEventListener("change", (e) => { |
| 96 | + updateTheme(e.target.value); |
| 97 | +}); |
0 commit comments