Skip to content

Instantly share code, notes, and snippets.

@wplit
Created February 25, 2026 09:57
Show Gist options
  • Select an option

  • Save wplit/e37931a5a9337f30935939fa3e8fbe12 to your computer and use it in GitHub Desktop.

Select an option

Save wplit/e37931a5a9337f30935939fa3e8fbe12 to your computer and use it in GitHub Desktop.
Change colors on <canvas> when core framework theme mode toggle clicked
// 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