|
| 1 | +--- |
| 2 | +--- |
| 3 | + |
| 4 | +<button id="themeToggle" aria-label="Toggle theme"> |
| 5 | + <svg width="24px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> |
| 6 | + <path class="sun" fill-rule="evenodd" d="M12 17.5a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zm0 1.5a7 7 0 1 0 0-14 7 7 0 0 0 0 14zm12-7a.8.8 0 0 1-.8.8h-2.4a.8.8 0 0 1 0-1.6h2.4a.8.8 0 0 1 .8.8zM4 12a.8.8 0 0 1-.8.8H.8a.8.8 0 0 1 0-1.6h2.5a.8.8 0 0 1 .8.8zm16.5-8.5a.8.8 0 0 1 0 1l-1.8 1.8a.8.8 0 0 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM6.3 17.7a.8.8 0 0 1 0 1l-1.7 1.8a.8.8 0 1 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM12 0a.8.8 0 0 1 .8.8v2.5a.8.8 0 0 1-1.6 0V.8A.8.8 0 0 1 12 0zm0 20a.8.8 0 0 1 .8.8v2.4a.8.8 0 0 1-1.6 0v-2.4a.8.8 0 0 1 .8-.8zM3.5 3.5a.8.8 0 0 1 1 0l1.8 1.8a.8.8 0 1 1-1 1L3.5 4.6a.8.8 0 0 1 0-1zm14.2 14.2a.8.8 0 0 1 1 0l1.8 1.7a.8.8 0 0 1-1 1l-1.8-1.7a.8.8 0 0 1 0-1z"/> |
| 7 | + <path class="moon" fill-rule="evenodd" d="M16.5 6A10.5 10.5 0 0 1 4.7 16.4 8.5 8.5 0 1 0 16.4 4.7l.1 1.3zm-1.7-2a9 9 0 0 1 .2 2 9 9 0 0 1-11 8.8 9.4 9.4 0 0 1-.8-.3c-.4 0-.8.3-.7.7a10 10 0 0 0 .3.8 10 10 0 0 0 9.2 6 10 10 0 0 0 4-19.2 9.7 9.7 0 0 0-.9-.3c-.3-.1-.7.3-.6.7a9 9 0 0 1 .3.8z"/> |
| 8 | + </svg> |
| 9 | +</button> |
| 10 | + |
| 11 | +<style> |
| 12 | + #themeToggle { |
| 13 | + border: 0; |
| 14 | + background: none; |
| 15 | + cursor: pointer; |
| 16 | + display: flex; |
| 17 | + } |
| 18 | + .sun { fill: white; } |
| 19 | + .moon { fill: transparent; } |
| 20 | + :global([data-theme="dark"]) .sun { fill: transparent; } |
| 21 | + :global([data-theme="dark"]) .moon { fill: white; } |
| 22 | +</style> |
| 23 | + |
| 24 | +<script> |
| 25 | + // Theme switching functionality |
| 26 | + (function() { |
| 27 | + const themeToggle = document.getElementById('themeToggle'); |
| 28 | + const html = document.documentElement; |
| 29 | + const THEME_STORAGE_KEY = 'terragrunt-theme'; |
| 30 | + |
| 31 | + // Function to get user's system preference |
| 32 | + function getSystemTheme() { |
| 33 | + return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; |
| 34 | + } |
| 35 | + |
| 36 | + // Function to get stored theme or system preference |
| 37 | + function getCurrentTheme() { |
| 38 | + const stored = localStorage.getItem(THEME_STORAGE_KEY); |
| 39 | + if (stored === 'light' || stored === 'dark') { |
| 40 | + return stored; |
| 41 | + } |
| 42 | + return getSystemTheme(); |
| 43 | + } |
| 44 | + |
| 45 | + // Function to apply theme |
| 46 | + function applyTheme(theme) { |
| 47 | + html.setAttribute('data-theme', theme); |
| 48 | + } |
| 49 | + |
| 50 | + // Function to set theme and store preference |
| 51 | + function setTheme(theme) { |
| 52 | + applyTheme(theme); |
| 53 | + localStorage.setItem(THEME_STORAGE_KEY, theme); |
| 54 | + } |
| 55 | + |
| 56 | + // Function to toggle theme |
| 57 | + function toggleTheme() { |
| 58 | + const currentTheme = getCurrentTheme(); |
| 59 | + const newTheme = currentTheme === 'light' ? 'dark' : 'light'; |
| 60 | + setTheme(newTheme); |
| 61 | + } |
| 62 | + |
| 63 | + // Initialize theme on page load |
| 64 | + function initializeTheme() { |
| 65 | + const theme = getCurrentTheme(); |
| 66 | + applyTheme(theme); |
| 67 | + } |
| 68 | + |
| 69 | + // Listen for system theme changes |
| 70 | + function handleSystemThemeChange(e) { |
| 71 | + // Only update if user hasn't set a preference |
| 72 | + if (!localStorage.getItem(THEME_STORAGE_KEY)) { |
| 73 | + const newTheme = e.matches ? 'dark' : 'light'; |
| 74 | + applyTheme(newTheme); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Add event listeners |
| 79 | + if (themeToggle) { |
| 80 | + themeToggle.addEventListener('click', toggleTheme); |
| 81 | + } |
| 82 | + |
| 83 | + // Listen for system theme changes |
| 84 | + const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); |
| 85 | + mediaQuery.addEventListener('change', handleSystemThemeChange); |
| 86 | + |
| 87 | + // Initialize theme |
| 88 | + initializeTheme(); |
| 89 | + })(); |
| 90 | +</script> |
0 commit comments