Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

feat(theme): broadcast theme:change so SVG/canvas consumers can repaint (#1839)

Guillem Arias Fauste272b8acd66ab
diff --git a/app/javascript/controllers/theme_controller.js b/app/javascript/controllers/theme_controller.js
index 361ae95ed9..ff64fd41c4 100644
--- a/app/javascript/controllers/theme_controller.js
+++ b/app/javascript/controllers/theme_controller.js
@@ -39,15 +39,15 @@ export default class extends Controller {
}
}
- // Sets or removes the data-theme attribute
+ // Sets the data-theme attribute and broadcasts a `theme:change` event so
+ // imperative consumers (D3/SVG/canvas) can repaint without polling.
setTheme(isDark) {
- if (isDark) {
- localStorage.theme = "dark";
- document.documentElement.setAttribute("data-theme", "dark");
- } else {
- localStorage.theme = "light";
- document.documentElement.setAttribute("data-theme", "light");
- }
+ const theme = isDark ? "dark" : "light";
+ localStorage.theme = theme;
+ document.documentElement.setAttribute("data-theme", theme);
+ document.documentElement.dispatchEvent(
+ new CustomEvent("theme:change", { detail: { theme } }),
+ );
}
systemPrefersDark() {