|
| 1 | +--- |
| 2 | +// Theme Toggle Component |
| 3 | +--- |
| 4 | + |
| 5 | +<button |
| 6 | + id="theme-toggle" |
| 7 | + class="theme-toggle" |
| 8 | + aria-label="Toggle dark mode" |
| 9 | + title="Toggle dark mode (Cmd/Ctrl + Shift + L)" |
| 10 | +> |
| 11 | + <span class="theme-toggle-icon theme-toggle-sun" aria-hidden="true"> |
| 12 | + <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> |
| 13 | + <circle cx="12" cy="12" r="5"/> |
| 14 | + <line x1="12" y1="1" x2="12" y2="3"/> |
| 15 | + <line x1="12" y1="21" x2="12" y2="23"/> |
| 16 | + <line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/> |
| 17 | + <line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/> |
| 18 | + <line x1="1" y1="12" x2="3" y2="12"/> |
| 19 | + <line x1="21" y1="12" x2="23" y2="12"/> |
| 20 | + <line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/> |
| 21 | + <line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/> |
| 22 | + </svg> |
| 23 | + </span> |
| 24 | + <span class="theme-toggle-icon theme-toggle-moon" aria-hidden="true"> |
| 25 | + <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> |
| 26 | + <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/> |
| 27 | + </svg> |
| 28 | + </span> |
| 29 | +</button> |
| 30 | + |
| 31 | +<script> |
| 32 | + (function() { |
| 33 | + const STORAGE_KEY = 'awesome-copilot-theme'; |
| 34 | + const toggle = document.getElementById('theme-toggle'); |
| 35 | + const html = document.documentElement; |
| 36 | + |
| 37 | + // Get initial theme |
| 38 | + function getTheme() { |
| 39 | + const stored = localStorage.getItem(STORAGE_KEY); |
| 40 | + if (stored === 'dark' || stored === 'light') return stored; |
| 41 | + return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; |
| 42 | + } |
| 43 | + |
| 44 | + // Apply theme |
| 45 | + function applyTheme(theme: string) { |
| 46 | + if (theme === 'dark') { |
| 47 | + html.setAttribute('data-theme', 'dark'); |
| 48 | + document.body.classList.remove('light'); |
| 49 | + document.body.classList.add('dark'); |
| 50 | + } else { |
| 51 | + html.setAttribute('data-theme', 'light'); |
| 52 | + document.body.classList.remove('dark'); |
| 53 | + document.body.classList.add('light'); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Toggle theme |
| 58 | + function toggleTheme() { |
| 59 | + const current = getTheme(); |
| 60 | + const next = current === 'dark' ? 'light' : 'dark'; |
| 61 | + localStorage.setItem(STORAGE_KEY, next); |
| 62 | + applyTheme(next); |
| 63 | + } |
| 64 | + |
| 65 | + // Initialize |
| 66 | + applyTheme(getTheme()); |
| 67 | + |
| 68 | + // Click handler |
| 69 | + toggle?.addEventListener('click', toggleTheme); |
| 70 | + |
| 71 | + // Keyboard shortcut: Cmd/Ctrl + Shift + L |
| 72 | + document.addEventListener('keydown', (e) => { |
| 73 | + if ((e.metaKey || e.ctrlKey) && e.shiftKey && e.key === 'L') { |
| 74 | + e.preventDefault(); |
| 75 | + toggleTheme(); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + // Listen for system theme changes |
| 80 | + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => { |
| 81 | + if (!localStorage.getItem(STORAGE_KEY)) { |
| 82 | + applyTheme(e.matches ? 'dark' : 'light'); |
| 83 | + } |
| 84 | + }); |
| 85 | + })(); |
| 86 | +</script> |
| 87 | + |
| 88 | +<style> |
| 89 | + .theme-toggle { |
| 90 | + display: flex; |
| 91 | + align-items: center; |
| 92 | + justify-content: center; |
| 93 | + width: 40px; |
| 94 | + height: 40px; |
| 95 | + padding: 0; |
| 96 | + border: 1px solid var(--color-glass-border); |
| 97 | + border-radius: var(--border-radius); |
| 98 | + background: var(--color-glass); |
| 99 | + color: var(--color-text); |
| 100 | + cursor: pointer; |
| 101 | + transition: all 0.2s ease; |
| 102 | + position: relative; |
| 103 | + overflow: hidden; |
| 104 | + } |
| 105 | + |
| 106 | + .theme-toggle:hover { |
| 107 | + background: var(--color-bg-tertiary); |
| 108 | + border-color: var(--color-accent); |
| 109 | + transform: translateY(-1px); |
| 110 | + } |
| 111 | + |
| 112 | + .theme-toggle:active { |
| 113 | + transform: translateY(0); |
| 114 | + } |
| 115 | + |
| 116 | + .theme-toggle:focus-visible { |
| 117 | + outline: 2px solid var(--color-accent); |
| 118 | + outline-offset: 2px; |
| 119 | + } |
| 120 | + |
| 121 | + .theme-toggle-icon { |
| 122 | + position: absolute; |
| 123 | + transition: all 0.3s ease; |
| 124 | + display: flex; |
| 125 | + align-items: center; |
| 126 | + justify-content: center; |
| 127 | + } |
| 128 | + |
| 129 | + /* Sun icon - shown in dark mode */ |
| 130 | + :root[data-theme="dark"] .theme-toggle-sun, |
| 131 | + body.dark .theme-toggle-sun { |
| 132 | + opacity: 1; |
| 133 | + transform: rotate(0deg) scale(1); |
| 134 | + } |
| 135 | + |
| 136 | + :root[data-theme="dark"] .theme-toggle-moon, |
| 137 | + body.dark .theme-toggle-moon { |
| 138 | + opacity: 0; |
| 139 | + transform: rotate(90deg) scale(0.5); |
| 140 | + } |
| 141 | + |
| 142 | + /* Moon icon - shown in light mode */ |
| 143 | + :root[data-theme="light"] .theme-toggle-sun, |
| 144 | + body.light .theme-toggle-sun, |
| 145 | + :root:not([data-theme]) .theme-toggle-sun, |
| 146 | + body:not(.dark):not(.light) .theme-toggle-sun { |
| 147 | + opacity: 0; |
| 148 | + transform: rotate(-90deg) scale(0.5); |
| 149 | + } |
| 150 | + |
| 151 | + :root[data-theme="light"] .theme-toggle-moon, |
| 152 | + body.light .theme-toggle-moon, |
| 153 | + :root:not([data-theme]) .theme-toggle-moon, |
| 154 | + body:not(.dark):not(.light) .theme-toggle-moon { |
| 155 | + opacity: 1; |
| 156 | + transform: rotate(0deg) scale(1); |
| 157 | + } |
| 158 | +</style> |
0 commit comments