| | --- |
| | import sunIconUrl from "../assets/icones/sun.svg?url"; |
| | import moonIconUrl from "../assets/icones/moon.svg?url"; |
| | --- |
| | <button id="theme-toggle" aria-label="Toggle color theme"> |
| | <img class="icon light" src={sunIconUrl} alt="light" width="20" height="20" /> |
| | <img class="icon dark" src={moonIconUrl} alt="dark" width="20" height="20" /> |
| | <script> |
| | const btn = document.getElementById('theme-toggle'); |
| | const media = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)'); |
| | const prefersDark = media && media.matches; |
| | const saved = localStorage.getItem('theme'); |
| |
|
| | const apply = (mode) => { |
| | document.documentElement.dataset.theme = mode; |
| | }; |
| |
|
| | |
| | apply(saved || (prefersDark ? 'dark' : 'light')); |
| |
|
| | |
| | if (!saved && media) { |
| | const syncWithSystem = (e) => apply(e.matches ? 'dark' : 'light'); |
| | if (media.addEventListener) media.addEventListener('change', syncWithSystem); |
| | else if (media.addListener) media.addListener(syncWithSystem); |
| | } |
| |
|
| | btn.addEventListener('click', () => { |
| | const next = (document.documentElement.dataset.theme === 'dark') ? 'light' : 'dark'; |
| | localStorage.setItem('theme', next); |
| | apply(next); |
| | }); |
| | </script> |
| | </button> |
| |
|
| |
|
| |
|