11import "../color-scale/color-scale.js" ;
2+ import "../channel-picker/channel-picker.js" ;
23import ColorElement from "../common/color-element.js" ;
34
45const Self = class ColorChart extends ColorElement {
56 static tagName = "color-chart" ;
67 static url = import . meta. url ;
78 static shadowStyle = true ;
89 static shadowTemplate = `
10+ <slot name="color-channel">
11+ <channel-picker id="channel_picker" part="color-channel"></channel-picker>
12+ </slot>
913 <div id="chart-container">
1014 <div id="chart">
1115 <slot></slot>
@@ -26,28 +30,40 @@ const Self = class ColorChart extends ColorElement {
2630 super ( ) ;
2731
2832 this . _el = {
29- slot : this . shadowRoot . querySelector ( "slot" ) ,
33+ slot : this . shadowRoot . querySelector ( "slot:not([name])" ) ,
34+ channel_picker : this . shadowRoot . getElementById ( "channel_picker" ) ,
3035 chart : this . shadowRoot . getElementById ( "chart" ) ,
3136 xTicks : this . shadowRoot . querySelector ( "#x_axis .ticks" ) ,
3237 yTicks : this . shadowRoot . querySelector ( "#y_axis .ticks" ) ,
3338 xLabel : this . shadowRoot . querySelector ( "#x_axis .label" ) ,
3439 yLabel : this . shadowRoot . querySelector ( "#y_axis .label" ) ,
3540 } ;
41+
42+ this . _slots = {
43+ color_channel : this . shadowRoot . querySelector ( "slot[name=color-channel]" ) ,
44+ } ;
3645 }
3746
3847 connectedCallback ( ) {
3948 super . connectedCallback ( ) ;
4049 this . _el . chart . addEventListener ( "colorschange" , this , { capture : true } ) ;
50+ this . _slots . color_channel . addEventListener ( "input" , this ) ;
4151 }
4252
4353 disconnectedCallback ( ) {
4454 this . _el . chart . removeEventListener ( "colorschange" , this , { capture : true } ) ;
55+ this . _slots . color_channel . removeEventListener ( "input" , this ) ;
4556 }
4657
4758 handleEvent ( evt ) {
48- if ( evt . target . tagName === "COLOR-SCALE" && evt . name === "computedColors" ) {
59+ let source = evt . target ;
60+ if ( source . tagName === "COLOR-SCALE" && evt . name === "computedColors" ) {
4961 this . render ( evt ) ;
5062 }
63+
64+ if ( this . _el . channel_picker === source || this . _slots . color_channel . assignedElements ( ) . includes ( source ) ) {
65+ this . y = source . value ;
66+ }
5167 }
5268
5369 series = new WeakMap ( ) ;
@@ -239,6 +255,18 @@ const Self = class ColorChart extends ColorElement {
239255 static props = {
240256 y : {
241257 default : "oklch.l" ,
258+ convert ( value ) {
259+ // Try setting the value to the channel picker. The picker will handle possible erroneous values.
260+ this . _el . channel_picker . value = value ;
261+
262+ // If the value is not set, that means it's invalid.
263+ // In that case, we are falling back to the picker's current value.
264+ if ( this . _el . channel_picker . value !== value ) {
265+ return this . _el . channel_picker . value ;
266+ }
267+
268+ return value ;
269+ } ,
242270 } ,
243271
244272 yResolved : {
0 commit comments