Skip to content

Commit 2933062

Browse files
committed
fix: repaint canvas layers on colorMode changes
1 parent f37635e commit 2933062

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

.vscode/settings.json

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,13 @@
1313
"[svelte]": {
1414
"editor.defaultFormatter": "esbenp.prettier-vscode"
1515
},
16-
"eslint.validate": [
17-
"javascript",
18-
"javascriptreact",
19-
"typescript",
20-
"typescriptreact",
21-
"svelte"
22-
],
16+
"eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact", "svelte"],
2317
"prettier.useTabs": false,
2418
"prettier.singleQuote": true,
2519
"prettier.tabWidth": 4,
2620
"prettier.trailingComma": "none",
2721
"prettier.printWidth": 100,
2822
"svelte.plugin.svelte.format.enable": false,
29-
"eslint.probe": [
30-
"javascript",
31-
"typescript",
32-
"svelte"
33-
],
23+
"eslint.probe": ["javascript", "typescript", "svelte"],
3424
"eslint.experimental.useFlatConfig": true
3525
}

src/lib/marks/helpers/CanvasLayer.svelte

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22
import { getContext } from 'svelte';
33
import type { PlotContext } from 'svelteplot/types';
44
import { devicePixelRatio } from 'svelte/reactivity/window';
5+
import { MediaQuery } from 'svelte/reactivity';
6+
import type { Attachment } from 'svelte/attachments';
7+
8+
const darkMode = new MediaQuery('prefers-color-scheme: dark');
9+
let colorScheme = $state();
10+
11+
/**
12+
* we need to repaint on dark mode changes in case the user
13+
* is using any css variables or currentColor that changes
14+
* with the color scheme
15+
*/
16+
const watchColorScheme: Attachment = (element: Element) => {
17+
const htmlElement = element.ownerDocument.documentElement;
18+
const observer = new MutationObserver(() => {
19+
colorScheme = getComputedStyle(htmlElement).colorScheme;
20+
});
21+
observer.observe(htmlElement, { attributes: true });
22+
return () => {
23+
observer.disconnect();
24+
};
25+
};
526
627
let restProps: {} = $props();
728
@@ -14,14 +35,15 @@
1435
canvas element inside a foreignObject for use in a plot and takes care of
1536
scaling it to the device pixel ratio.
1637
-->
17-
18-
<foreignObject x="0" y="0" width={plot.width} height={plot.height}>
19-
<canvas
20-
xmlns="http://www.w3.org/1999/xhtml"
21-
{...restProps}
22-
width={plot.width * (devicePixelRatio.current ?? 1)}
23-
height={plot.height * (devicePixelRatio.current ?? 1)}
24-
style="width: {plot.width}px; height: {plot.height}px;"></canvas>
38+
<foreignObject x="0" y="0" {@attach watchColorScheme} width={plot.width} height={plot.height}>
39+
{#key [colorScheme, darkMode.current]}
40+
<canvas
41+
xmlns="http://www.w3.org/1999/xhtml"
42+
{...restProps}
43+
width={plot.width * (devicePixelRatio.current ?? 1)}
44+
height={plot.height * (devicePixelRatio.current ?? 1)}
45+
style="width: {plot.width}px; height: {plot.height}px;"></canvas>
46+
{/key}
2547
</foreignObject>
2648

2749
<style>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy