|
| 1 | +--- |
| 2 | +// Same behavior as website/index.html theme toggle: persists `iii_theme`, |
| 3 | +// swaps CSS variables + icon, updates theme-color meta, dispatches iii:theme-change. |
| 4 | +--- |
| 5 | + |
| 6 | +<button type="button" id="theme-btn" class="theme-btn" aria-label="Toggle theme"> |
| 7 | + <svg id="theme-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> |
| 8 | + <!-- moon when current theme is light (click → dark) --> |
| 9 | + <path d="M12.5 9.5 A 5 5 0 1 1 6.5 3.5 A 4 4 0 0 0 12.5 9.5 Z"></path> |
| 10 | + </svg> |
| 11 | +</button> |
| 12 | + |
| 13 | +<script is:inline> |
| 14 | + (function () { |
| 15 | + var themeBtn = document.getElementById('theme-btn') |
| 16 | + var themeIcon = document.getElementById('theme-icon') |
| 17 | + if (!themeBtn || !themeIcon) return |
| 18 | + var THEME_KEY = 'iii_theme' |
| 19 | + var isDark = (window.__iiiThemeInit && window.__iiiThemeInit.dark) || false |
| 20 | + var THEME_ACCENT = { light: '#ff5a1f', dark: '#3ea8ff' } |
| 21 | + function setAria() { |
| 22 | + themeBtn.setAttribute('aria-label', isDark ? 'Switch to light mode' : 'Switch to dark mode') |
| 23 | + } |
| 24 | + function syncIcon() { |
| 25 | + if (isDark) { |
| 26 | + themeIcon.innerHTML = |
| 27 | + '<circle cx="8" cy="8" r="3"/><path d="M8 1 L8 3 M8 13 L8 15 M1 8 L3 8 M13 8 L15 8 M3 3 L4.5 4.5 M11.5 11.5 L13 13 M3 13 L4.5 11.5 M11.5 4.5 L13 3"/>' |
| 28 | + } else { |
| 29 | + themeIcon.innerHTML = '<path d="M12.5 9.5 A 5 5 0 1 1 6.5 3.5 A 4 4 0 0 0 12.5 9.5 Z"/>' |
| 30 | + } |
| 31 | + } |
| 32 | + if (isDark) syncIcon() |
| 33 | + setAria() |
| 34 | + themeBtn.addEventListener('click', function () { |
| 35 | + isDark = !isDark |
| 36 | + try { |
| 37 | + localStorage.setItem(THEME_KEY, isDark ? 'dark' : 'light') |
| 38 | + } catch (e) {} |
| 39 | + document.documentElement.dataset.theme = isDark ? 'dark' : 'light' |
| 40 | + var r = document.documentElement.style |
| 41 | + if (isDark) { |
| 42 | + r.setProperty('--bg', '#111110') |
| 43 | + r.setProperty('--panel', '#1a1916') |
| 44 | + r.setProperty('--paper', '#111110') |
| 45 | + r.setProperty('--paper-2', '#222220') |
| 46 | + r.setProperty('--ink', '#f2f0ed') |
| 47 | + r.setProperty('--ink-2', '#e2dfd9') |
| 48 | + r.setProperty('--ink-soft', '#e2dfd9') |
| 49 | + r.setProperty('--ink-faint', '#9c9893') |
| 50 | + r.setProperty('--ink-ghost', '#5d5a55') |
| 51 | + r.setProperty('--mute', '#9c9893') |
| 52 | + r.setProperty('--mute-2', '#5d5a55') |
| 53 | + r.setProperty('--rule', '#2a2926') |
| 54 | + r.setProperty('--rule-2', '#1f1e1c') |
| 55 | + r.setProperty('--accent', THEME_ACCENT.dark) |
| 56 | + } else { |
| 57 | + r.setProperty('--bg', '#f5f3ee') |
| 58 | + r.setProperty('--panel', '#e9e6e2') |
| 59 | + r.setProperty('--paper', '#f5f3ee') |
| 60 | + r.setProperty('--paper-2', '#ebe8e3') |
| 61 | + r.setProperty('--ink', '#0a0a0a') |
| 62 | + r.setProperty('--ink-2', '#1a1a1a') |
| 63 | + r.setProperty('--ink-soft', '#1a1a1a') |
| 64 | + r.setProperty('--ink-faint', '#6b6865') |
| 65 | + r.setProperty('--ink-ghost', '#a3a09c') |
| 66 | + r.setProperty('--mute', '#6b6865') |
| 67 | + r.setProperty('--mute-2', '#a3a09c') |
| 68 | + r.setProperty('--rule', '#d8d4cb') |
| 69 | + r.setProperty('--rule-2', '#e6e3df') |
| 70 | + r.setProperty('--accent', THEME_ACCENT.light) |
| 71 | + } |
| 72 | + syncIcon() |
| 73 | + setAria() |
| 74 | + var meta = document.querySelector('meta[name="theme-color"]') |
| 75 | + if (meta) meta.content = isDark ? '#111110' : '#f5f3ee' |
| 76 | + window.dispatchEvent( |
| 77 | + new CustomEvent('iii:theme-change', { |
| 78 | + detail: { |
| 79 | + theme: isDark ? 'dark' : 'light', |
| 80 | + accent: isDark ? THEME_ACCENT.dark : THEME_ACCENT.light, |
| 81 | + }, |
| 82 | + }), |
| 83 | + ) |
| 84 | + }) |
| 85 | + })() |
| 86 | +</script> |
0 commit comments