|
| 1 | +<button id='theme-toggle' class='theme-toggle' aria-label='Toggle theme'> |
| 2 | + <svg class='sun-and-moon' aria-hidden='true' width='24' height='24' viewBox='0 0 24 24'> |
| 3 | + <mask class='moon' id='moon-mask'> |
| 4 | + <rect x='0' y='0' width='100%' height='100%' fill='white'></rect> |
| 5 | + <circle cx='24' cy='10' r='6' fill='black'></circle> |
| 6 | + </mask> |
| 7 | + <circle class='sun' cx='12' cy='12' r='6' fill='currentColor' mask='url(#moon-mask)'></circle> |
| 8 | + <g class='sun-beams' stroke='currentColor'> |
| 9 | + <line x1='12' y1='1' x2='12' y2='3'></line> |
| 10 | + <line x1='12' y1='21' x2='12' y2='23'></line> |
| 11 | + <line x1='4.22' y1='4.22' x2='5.64' y2='5.64'></line> |
| 12 | + <line x1='18.36' y1='18.36' x2='19.78' y2='19.78'></line> |
| 13 | + <line x1='1' y1='12' x2='3' y2='12'></line> |
| 14 | + <line x1='21' y1='12' x2='23' y2='12'></line> |
| 15 | + <line x1='4.22' y1='19.78' x2='5.64' y2='18.36'></line> |
| 16 | + <line x1='18.36' y1='5.64' x2='19.78' y2='4.22'></line> |
| 17 | + </g> |
| 18 | + </svg> |
| 19 | +</button> |
| 20 | + |
| 21 | +<style is:global> |
| 22 | + .theme-toggle { |
| 23 | + position: fixed; |
| 24 | + bottom: 24px; |
| 25 | + right: 24px; |
| 26 | + background: none; |
| 27 | + border: none; |
| 28 | + cursor: pointer; |
| 29 | + color: var(--theme-text); |
| 30 | + padding: 0; |
| 31 | + } |
| 32 | + |
| 33 | + .sun-and-moon > :is(.moon, .sun, .sun-beams, .moon-circle, .moon-crater) { |
| 34 | + transform-origin: center center; |
| 35 | + transition: |
| 36 | + transform 0.5s ease, |
| 37 | + opacity 0.5s ease; |
| 38 | + } |
| 39 | + |
| 40 | + .sun-and-moon > :is(.moon, .sun) { |
| 41 | + fill: currentColor; |
| 42 | + } |
| 43 | + |
| 44 | + .theme-toggle:is(:hover, :focus-visible) { |
| 45 | + color: var(--theme-accent); |
| 46 | + } |
| 47 | + |
| 48 | + .sun-and-moon > .sun-beams { |
| 49 | + stroke: currentColor; |
| 50 | + stroke-width: 2px; |
| 51 | + } |
| 52 | + |
| 53 | + .moon-circle { |
| 54 | + transition: transform 0.5s ease; |
| 55 | + } |
| 56 | + |
| 57 | + [data-theme='dark'] .sun-and-moon > .sun { |
| 58 | + transform: scale(1.75); |
| 59 | + } |
| 60 | + |
| 61 | + [data-theme='dark'] .sun-and-moon > .sun-beams { |
| 62 | + opacity: 0; |
| 63 | + } |
| 64 | + |
| 65 | + [data-theme='dark'] .sun-and-moon > .moon > circle { |
| 66 | + transform: translateX(-7px); |
| 67 | + transition: transform 0.5s ease; |
| 68 | + } |
| 69 | + |
| 70 | + [data-theme='dark'] .moon-circle { |
| 71 | + transform: translateX(-12px) scale(0.92); |
| 72 | + } |
| 73 | + |
| 74 | + [data-theme='dark'] .moon-crater { |
| 75 | + transform: translateX(-7px); |
| 76 | + } |
| 77 | + |
| 78 | + [data-theme='light'] .moon-circle, |
| 79 | + [data-theme='light'] .moon-crater { |
| 80 | + opacity: 0; |
| 81 | + } |
| 82 | + |
| 83 | + @media (prefers-reduced-motion: reduce) { |
| 84 | + .sun-and-moon > :is(.moon, .sun, .sun-beams, .moon-circle, .moon-crater) { |
| 85 | + transition: none; |
| 86 | + } |
| 87 | + } |
| 88 | +</style> |
| 89 | +<script> |
| 90 | + const themeToggle = document.getElementById('theme-toggle'); |
| 91 | + const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)'); |
| 92 | + |
| 93 | + function changeGiscusTheme(newTheme) { |
| 94 | + const giscusScript = document.getElementById('giscus-script'); |
| 95 | + if (giscusScript) { |
| 96 | + giscusScript.setAttribute('data-theme', newTheme); |
| 97 | + |
| 98 | + // Reload giscus |
| 99 | + const iframe = document.querySelector('iframe.giscus-frame') as HTMLIFrameElement; |
| 100 | + if (iframe) { |
| 101 | + iframe.contentWindow.postMessage({ giscus: { setConfig: { theme: newTheme } } }, 'https://giscus.app'); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + function setTheme(theme) { |
| 107 | + if (theme === 'dark') { |
| 108 | + document.body.classList.add('dark'); |
| 109 | + changeGiscusTheme('https://gw.alipayobjects.com/os/k/weekly1/comment-dark.css'); |
| 110 | + } else { |
| 111 | + document.body.classList.remove('dark'); |
| 112 | + changeGiscusTheme('https://gw.alipayobjects.com/os/k/weekly/comment.css'); |
| 113 | + } |
| 114 | + document.body.setAttribute('data-theme', theme); |
| 115 | + |
| 116 | + localStorage.setItem('theme', theme); |
| 117 | + } |
| 118 | + |
| 119 | + function toggleTheme() { |
| 120 | + const currentTheme = document.body.getAttribute('data-theme'); |
| 121 | + const newTheme = currentTheme === 'dark' ? 'light' : 'dark'; |
| 122 | + setTheme(newTheme); |
| 123 | + } |
| 124 | + |
| 125 | + const savedTheme = localStorage.getItem('theme'); |
| 126 | + if (savedTheme) { |
| 127 | + setTheme(savedTheme); |
| 128 | + } else { |
| 129 | + setTheme(prefersDarkScheme.matches ? 'dark' : 'light'); |
| 130 | + } |
| 131 | + |
| 132 | + themeToggle.addEventListener('click', toggleTheme); |
| 133 | + |
| 134 | + prefersDarkScheme.addEventListener('change', (e) => { |
| 135 | + const newTheme = e.matches ? 'dark' : 'light'; |
| 136 | + setTheme(newTheme); |
| 137 | + }); |
| 138 | +</script> |
0 commit comments