|
| 1 | +<style> |
| 2 | +.theme-toggle { |
| 3 | + position: fixed; |
| 4 | + top: 1rem; |
| 5 | + right: 1rem; |
| 6 | + z-index: 1000; |
| 7 | + width: 2.25rem; |
| 8 | + height: 2.25rem; |
| 9 | + border-radius: 50%; |
| 10 | + border: none; |
| 11 | + cursor: pointer; |
| 12 | + padding: 0; |
| 13 | + display: flex; |
| 14 | + align-items: center; |
| 15 | + justify-content: center; |
| 16 | + background-color: rgba(128, 128, 128, 0.15); |
| 17 | + color: inherit; |
| 18 | + transition: background-color 0.2s, transform 0.2s; |
| 19 | +} |
| 20 | +.theme-toggle svg { |
| 21 | + width: 1.1rem; |
| 22 | + height: 1.1rem; |
| 23 | + fill: currentColor; |
| 24 | +} |
| 25 | +.theme-toggle:hover { |
| 26 | + background-color: rgba(128, 128, 128, 0.3); |
| 27 | + transform: scale(1.1); |
| 28 | +} |
| 29 | +.theme-toggle:active { |
| 30 | + transform: scale(0.95); |
| 31 | +} |
| 32 | +</style> |
| 33 | +<script> |
| 34 | +(function() { |
| 35 | + var saved = localStorage.getItem('theme'); |
| 36 | + var prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches; |
| 37 | + if (saved === 'codeday-dark' || (!saved && prefersDark)) { |
| 38 | + var link = document.querySelector('link[rel="stylesheet"]'); |
| 39 | + if (link) { |
| 40 | + link.href = link.href.replace(/just-the-docs-[^.]+/, 'just-the-docs-codeday-dark'); |
| 41 | + window.scrollTo(0, 0); |
| 42 | + document.addEventListener('DOMContentLoaded', function() { window.scrollTo(0, 0); }); |
| 43 | + } |
| 44 | + } |
| 45 | +})(); |
| 46 | + |
| 47 | +document.addEventListener('DOMContentLoaded', function() { |
| 48 | + var sunSvg = '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><line x1="12" y1="21" x2="12" y2="23" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><line x1="1" y1="12" x2="3" y2="12" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><line x1="21" y1="12" x2="23" y2="12" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>'; |
| 49 | + var moonSvg = '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>'; |
| 50 | + |
| 51 | + var btn = document.createElement('button'); |
| 52 | + btn.className = 'theme-toggle'; |
| 53 | + btn.setAttribute('aria-label', 'Toggle dark mode'); |
| 54 | + document.body.appendChild(btn); |
| 55 | + |
| 56 | + function setTheme(theme) { |
| 57 | + var link = document.querySelector('link[rel="stylesheet"]'); |
| 58 | + if (link) link.setAttribute('href', link.getAttribute('href').replace(/just-the-docs-[^.]+/, 'just-the-docs-' + theme)); |
| 59 | + } |
| 60 | + function isDark() { |
| 61 | + var saved = localStorage.getItem('theme'); |
| 62 | + return saved === 'codeday-dark' || (!saved && window.matchMedia('(prefers-color-scheme: dark)').matches); |
| 63 | + } |
| 64 | + function updateBtn() { |
| 65 | + var dark = isDark(); |
| 66 | + btn.innerHTML = dark ? sunSvg : moonSvg; |
| 67 | + var logo = document.querySelector('.site-logo'); |
| 68 | + if (logo) logo.style.backgroundImage = dark ? 'url("/assets/logo-text/white.svg")' : 'url("/assets/logo-text/color.svg")'; |
| 69 | + } |
| 70 | + btn.addEventListener('click', function() { |
| 71 | + var toDark = !isDark(); |
| 72 | + var newTheme = toDark ? 'codeday-dark' : 'codeday-light'; |
| 73 | + setTheme(newTheme); |
| 74 | + localStorage.setItem('theme', newTheme); |
| 75 | + updateBtn(); |
| 76 | + }); |
| 77 | + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function(e) { |
| 78 | + if (!localStorage.getItem('theme')) { |
| 79 | + setTheme(e.matches ? 'codeday-dark' : 'codeday-light'); |
| 80 | + updateBtn(); |
| 81 | + } |
| 82 | + }); |
| 83 | + updateBtn(); |
| 84 | +}); |
| 85 | +</script> |
0 commit comments