|
| 1 | +function setTheme(mode) { |
| 2 | + localStorage.setItem("theme-storage", mode); |
| 3 | +} |
| 4 | + |
| 5 | +function changeGiscusTheme() { |
| 6 | + const theme = getSavedTheme(); |
| 7 | + function sendMessage(message) { |
| 8 | + const iframe = document.querySelector("iframe.giscus-frame"); |
| 9 | + if (!iframe) return; |
| 10 | + iframe.contentWindow.postMessage({ giscus: message }, "https://giscus.app"); |
| 11 | + } |
| 12 | + sendMessage({ |
| 13 | + setConfig: { |
| 14 | + theme, |
| 15 | + }, |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +function toggleTheme() { |
| 20 | + if (localStorage.getItem("theme-storage") === "light") { |
| 21 | + setTheme("dark"); |
| 22 | + updateItemToggleTheme(); |
| 23 | + changeGiscusTheme(); |
| 24 | + } else if (localStorage.getItem("theme-storage") === "dark") { |
| 25 | + setTheme("light"); |
| 26 | + updateItemToggleTheme(); |
| 27 | + changeGiscusTheme(); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function updateItemToggleTheme() { |
| 32 | + let mode = getSavedTheme(); |
| 33 | + |
| 34 | + const darkModeStyle = document.getElementById("darkModeStyle"); |
| 35 | + if (darkModeStyle) { |
| 36 | + darkModeStyle.disabled = mode === "light"; |
| 37 | + } |
| 38 | + |
| 39 | + const sunIcon = document.getElementById("sun-icon"); |
| 40 | + const moonIcon = document.getElementById("moon-icon"); |
| 41 | + if (sunIcon && moonIcon) { |
| 42 | + sunIcon.style.display = (mode === "dark") ? "block" : "none"; |
| 43 | + moonIcon.style.display = (mode === "light") ? "block" : "none"; |
| 44 | + } |
| 45 | + |
| 46 | + let htmlElement = document.querySelector("html"); |
| 47 | + if (mode === "dark") { |
| 48 | + htmlElement.classList.remove("light"); |
| 49 | + htmlElement.classList.add("dark"); |
| 50 | + } else if (mode === "light") { |
| 51 | + htmlElement.classList.remove("dark"); |
| 52 | + htmlElement.classList.add("light"); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function getSavedTheme() { |
| 57 | + let currentTheme = localStorage.getItem("theme-storage"); |
| 58 | + if (!currentTheme) { |
| 59 | + if ( |
| 60 | + window.matchMedia && |
| 61 | + window.matchMedia("(prefers-color-scheme: dark)").matches |
| 62 | + ) { |
| 63 | + currentTheme = "dark"; |
| 64 | + } else { |
| 65 | + currentTheme = "light"; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return currentTheme; |
| 70 | +} |
| 71 | + |
| 72 | +// Update the toggle theme on page load |
| 73 | +updateItemToggleTheme(); |
0 commit comments