File tree 2 files changed +24
-9
lines changed
2 files changed +24
-9
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ export default [
18
18
resolve ( ) ,
19
19
terser ( )
20
20
] ,
21
- external : [ "@carbon/charts" ]
21
+ external : [ "svelte" , " @carbon/charts"]
22
22
} ,
23
23
{
24
24
input : "./src/index.js" ,
@@ -28,9 +28,8 @@ export default [
28
28
} ,
29
29
plugins : [
30
30
svelte ( ) ,
31
- resolve ( ) ,
32
- terser ( )
31
+ resolve ( )
33
32
] ,
34
- external : [ "@carbon/charts" ]
33
+ external : [ "svelte" , " @carbon/charts"]
35
34
}
36
35
] ;
Original file line number Diff line number Diff line change 6
6
import { onMount , createEventDispatcher } from " svelte" ;
7
7
8
8
const dispatch = createEventDispatcher ();
9
+ const id = " chart-" + Math .random ().toString (36 );
9
10
10
11
let ref = undefined ;
11
12
let chart = undefined ;
12
13
13
14
onMount (() => {
14
- chart = new Chart (ref, { data, options });
15
- dispatch (" load" , chart);
15
+ /**
16
+ * CodeSandbox does not resolve Svelte components from the `svelte` package.json entry.
17
+ * This causes `bind:ref` to be `undefined`; the chart can't mount to the element.
18
+ *
19
+ * We fallback to manually querying the DOM for the chart holder element because
20
+ * CodeSandbox does not use uncompiled Svelte source code.
21
+ *
22
+ * See https://github.com/sveltejs/svelte/issues/2937
23
+ */
24
+ const element = ref || document .getElementById (id);
25
+
26
+ if (element) {
27
+ chart = new Chart (element, { data, options });
28
+ dispatch (" load" , chart);
29
+ }
16
30
17
31
return () => {
18
- chart .destroy ();
19
- dispatch (" destroy" );
32
+ if (chart) {
33
+ chart .destroy ();
34
+ dispatch (" destroy" );
35
+ }
20
36
};
21
37
});
22
38
27
43
}
28
44
</script >
29
45
30
- <div {...$$restProps } bind:this ={ref } on:load on:update on:destroy />
46
+ <div {...$$restProps } bind:this ={ref } { id } />
You can’t perform that action at this time.
0 commit comments