svelteplot 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,6 +2,27 @@
|
|
|
2
2
|
import { getContext } from 'svelte';
|
|
3
3
|
import type { PlotContext } from '../../types';
|
|
4
4
|
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
|
+
};
|
|
5
26
|
|
|
6
27
|
let restProps: {} = $props();
|
|
7
28
|
|
|
@@ -14,14 +35,15 @@
|
|
|
14
35
|
canvas element inside a foreignObject for use in a plot and takes care of
|
|
15
36
|
scaling it to the device pixel ratio.
|
|
16
37
|
-->
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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}
|
|
25
47
|
</foreignObject>
|
|
26
48
|
|
|
27
49
|
<style>
|