Skip to content

Commit a1db13e

Browse files
committed
fix(canvas): React to theme changes (#9283)
The Sparkline canvas widget does not update its colors when the global application theme changes (e.g., to dark mode) because the theme config is only passed on initial registration, but updateConfig in the worker does not handle it dynamically. Updated updateConfig in src/canvas/Sparkline.mjs to apply theme changes and trigger a redraw. Evaluated theme inline inside afterSetOffscreenRegistered in src/component/Sparkline.mjs to ensure the correct string is passed.
1 parent 2c5c596 commit a1db13e

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

src/canvas/Sparkline.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ class Sparkline extends Base {
307307
* Updates the configuration for a specific canvas.
308308
* @param {Object} data
309309
* @param {String} data.canvasId
310+
* @param {String} [data.theme]
310311
* @param {Boolean} [data.usePulse]
311312
* @param {Boolean} [data.useTransition]
312313
*/
@@ -315,6 +316,10 @@ class Sparkline extends Base {
315316
item = me.items.get(data.canvasId);
316317

317318
if (item) {
319+
if (data.theme !== undefined) {
320+
item.theme = data.theme;
321+
me.draw(item)
322+
}
318323
if (data.usePulse !== undefined) {
319324
item.usePulse = data.usePulse
320325
}
@@ -359,7 +364,7 @@ class Sparkline extends Base {
359364
if (!item.isTransitioning) {
360365
item.isTransitioning = true;
361366
me.activeItems.add(item);
362-
367+
363368
if (!me.animationId) {
364369
me.renderLoop()
365370
}

src/component/Sparkline.mjs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ class Sparkline extends Canvas {
7878
* @param {Boolean} oldValue
7979
*/
8080
afterSetUsePulse(value, oldValue) {
81-
if (this.offscreenRegistered && value !== undefined) {
82-
this.renderer?.updateConfig({
83-
canvasId: this.id,
81+
let me = this;
82+
83+
if (me.offscreenRegistered && value !== undefined) {
84+
me.renderer?.updateConfig({
85+
canvasId: me.id,
8486
usePulse: value
8587
})
8688
}
@@ -92,14 +94,37 @@ class Sparkline extends Canvas {
9294
* @param {Boolean} oldValue
9395
*/
9496
afterSetUseTransition(value, oldValue) {
95-
if (this.offscreenRegistered && value !== undefined) {
96-
this.renderer?.updateConfig({
97-
canvasId : this.id,
97+
let me = this;
98+
99+
if (me.offscreenRegistered && value !== undefined) {
100+
me.renderer?.updateConfig({
101+
canvasId : me.id,
98102
useTransition: value
99103
})
100104
}
101105
}
102106

107+
/**
108+
* Triggered after the theme config got changed
109+
* @param {String|null} value
110+
* @param {String|null} oldValue
111+
* @protected
112+
*/
113+
afterSetTheme(value, oldValue) {
114+
super.afterSetTheme(value, oldValue);
115+
116+
let me = this;
117+
118+
if (me.offscreenRegistered && value) {
119+
let theme = value.includes('dark') ? 'dark' : 'light';
120+
121+
me.renderer?.updateConfig({
122+
canvasId: me.id,
123+
theme
124+
})
125+
}
126+
}
127+
103128
/**
104129
* @param {Number[]|null} value
105130
* @param {Number[]|null} oldValue
@@ -119,9 +144,11 @@ class Sparkline extends Canvas {
119144
* @param {...*} args
120145
*/
121146
destroy(...args) {
122-
if (this.offscreenRegistered) {
123-
this.renderer?.unregister({
124-
canvasId: this.id
147+
let me = this;
148+
149+
if (me.offscreenRegistered) {
150+
me.renderer?.unregister({
151+
canvasId: me.id
125152
})
126153
}
127154

@@ -160,7 +187,7 @@ class Sparkline extends Canvas {
160187
await me.renderer?.register({
161188
canvasId : me.id,
162189
devicePixelRatio: Neo.config.devicePixelRatio,
163-
theme : me.theme || 'light',
190+
theme : me.theme?.includes('dark') ? 'dark' : 'light',
164191
usePulse : me.usePulse,
165192
useTransition : me.useTransition,
166193
windowId : me.windowId

0 commit comments

Comments
 (0)