Created
February 25, 2026 09:57
-
-
Save wplit/e37931a5a9337f30935939fa3e8fbe12 to your computer and use it in GitHub Desktop.
Change colors on <canvas> when core framework theme mode toggle clicked
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Watch for class changes on html tag | |
| if (!window.xClassChangeObserverAdded) { | |
| window.xClassChangeObserverAdded = true; | |
| let previousTheme = document.documentElement.classList.contains('cf-theme-dark') ? 'dark' : 'light'; | |
| const observer = new MutationObserver((mutations) => { | |
| mutations.forEach((mutation) => { | |
| if (mutation.type === 'attributes' && mutation.attributeName === 'class') { | |
| const classList = document.documentElement.classList; | |
| const currentTheme = classList.contains('cf-theme-dark') ? 'dark' : 'light'; | |
| // Only dispatch if theme actually changed | |
| if (currentTheme !== previousTheme) { | |
| previousTheme = currentTheme; | |
| document.dispatchEvent(new Event('x-color-scheme-change')); | |
| } | |
| } | |
| }); | |
| }); | |
| observer.observe(document.documentElement, { | |
| attributes: true, | |
| attributeFilter: ['class'] | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment