Skip to content

Commit e0b28da

Browse files
committed
Address PR feedback
1 parent 708ef2c commit e0b28da

3 files changed

Lines changed: 82 additions & 25 deletions

File tree

‎src/types/core/events.d.ts‎

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,17 @@ export interface LegendTitleClickEvent {
265265
fullLayout: Partial<Layout>;
266266
}
267267

268-
/** Payload for `plotly_react`, emitted after a `Plotly.react` call that left the config unchanged. */
268+
/**
269+
* Payload for `plotly_react`. The event does not fire if the call changed the config.
270+
* The `config` and `layout` values are `undefined` if the `Plotly.react` call omitted them.
271+
*/
269272
export interface PlotReactEvent {
270273
/** User-supplied config object. */
271-
config: Partial<Config>;
274+
config: Partial<Config> | undefined;
272275
/** User-supplied data array. */
273276
data: Data[];
274277
/** User-supplied layout object. */
275-
layout: Partial<Layout>;
278+
layout: Partial<Layout> | undefined;
276279
}
277280

278281
/** Payload for `plotly_buttonclicked`, emitted when a button of an updatemenu is clicked. */
@@ -336,8 +339,8 @@ export interface WebGLContextLostEvent {
336339

337340
/** Point payload for `plotly_sunburstclick`. Treemap and icicle traces emit the same point shape. */
338341
export interface SunburstPlotDatum {
339-
/** Color value associated with the sector. */
340-
color: number;
342+
/** Color value associated with the sector. Present only if the trace sets `marker.colors`. */
343+
color?: number | undefined;
341344
/** Index of the trace producing this sector. */
342345
curveNumber: number;
343346
/** The trace this sector belongs to. */
@@ -346,14 +349,14 @@ export interface SunburstPlotDatum {
346349
entry: string;
347350
/** Fully-resolved trace data. */
348351
fullData: Data;
349-
/** Hover text shown for this sector. */
350-
hovertext: string;
351-
/** Hierarchical id of the sector. */
352-
id: string;
353-
/** Display label of the sector. */
354-
label: string;
355-
/** Parent sector id. */
356-
parent: string;
352+
/** Hover text shown for this sector. Present only if the trace sets `hovertext`. */
353+
hovertext?: string | undefined;
354+
/** Hierarchical id of the sector. Present only if the trace sets `ids`. */
355+
id?: string | undefined;
356+
/** Display label of the sector. Present only if the trace sets `labels`. */
357+
label?: string | undefined;
358+
/** Parent sector id. Absent on the root sector. */
359+
parent?: string | undefined;
357360
/** Percentage of the immediate entry sector this sector represents. */
358361
percentEntry: number;
359362
/** Percentage of the parent sector this sector represents. */
@@ -364,8 +367,8 @@ export interface SunburstPlotDatum {
364367
pointNumber: number;
365368
/** Root sector id. */
366369
root: string;
367-
/** Numeric value of the sector. */
368-
value: number;
370+
/** Numeric value of the sector. Present only if the trace sets `values`. */
371+
value?: number | undefined;
369372
}
370373

371374
/** Point payload for `plotly_treemapclick`. Same shape as `SunburstPlotDatum`. */
@@ -432,34 +435,37 @@ export interface PlotlyHTMLElement extends HTMLElement {
432435
/** Subscribe to per-frame animation events. */
433436
on(event: 'plotly_animatingframe', callback: (event: FrameAnimationEvent) => void): void;
434437
/** Subscribe to legend click events; return `false` to suppress default toggle. */
435-
on(event: 'plotly_legendclick' | 'plotly_legenddoubleclick', callback: (event: LegendClickEvent) => boolean): void;
438+
on(
439+
event: 'plotly_legendclick' | 'plotly_legenddoubleclick',
440+
callback: (event: LegendClickEvent) => boolean | void
441+
): void;
436442
/** Subscribe to legend title click events; return `false` to suppress default behavior. */
437443
on(
438444
event: 'plotly_legendtitleclick' | 'plotly_legendtitledoubleclick',
439-
callback: (event: LegendTitleClickEvent) => boolean
445+
callback: (event: LegendTitleClickEvent) => boolean | void
440446
): void;
441447
/** Subscribe to react events. */
442448
on(event: 'plotly_react', callback: (event: PlotReactEvent) => void): void;
443449
/** Subscribe to updatemenu button click events. */
444450
on(event: 'plotly_buttonclicked', callback: (event: ButtonClickedEvent) => void): void;
445451
/** Subscribe to pre-hover events; return `false` to cancel the hover. */
446-
on(event: 'plotly_beforehover', callback: (event: MouseEvent) => boolean): void;
452+
on(event: 'plotly_beforehover', callback: (event: MouseEvent) => boolean | void): void;
447453
/** Subscribe to slider change events. */
448454
on(event: 'plotly_sliderchange', callback: (event: SliderChangeEvent) => void): void;
449455
/** Subscribe to slider end-of-interaction events. */
450456
on(event: 'plotly_sliderend', callback: (event: SliderEndEvent) => void): void;
451457
/** Subscribe to slider start-of-interaction events. */
452458
on(event: 'plotly_sliderstart', callback: (event: SliderStartEvent) => void): void;
453-
/** Subscribe to sunburst sector click events. */
454-
on(event: 'plotly_sunburstclick', callback: (event: SunburstClickEvent) => void): void;
455-
/** Subscribe to treemap sector click events. */
456-
on(event: 'plotly_treemapclick', callback: (event: TreemapClickEvent) => void): void;
457-
/** Subscribe to icicle sector click events. */
458-
on(event: 'plotly_icicleclick', callback: (event: IcicleClickEvent) => void): void;
459+
/** Subscribe to sunburst sector click events; return `false` to suppress the default zoom. */
460+
on(event: 'plotly_sunburstclick', callback: (event: SunburstClickEvent) => boolean | void): void;
461+
/** Subscribe to treemap sector click events; return `false` to suppress the default zoom. */
462+
on(event: 'plotly_treemapclick', callback: (event: TreemapClickEvent) => boolean | void): void;
463+
/** Subscribe to icicle sector click events; return `false` to suppress the default zoom. */
464+
on(event: 'plotly_icicleclick', callback: (event: IcicleClickEvent) => boolean | void): void;
459465
/** Subscribe to generic plotly events (low-level catch-all). */
460466
on(event: 'plotly_event', callback: (data: any) => void): void;
461467
/** Subscribe to pre-plot events; return `false` to cancel plotting. */
462-
on(event: 'plotly_beforeplot', callback: (event: BeforePlotEvent) => boolean): void;
468+
on(event: 'plotly_beforeplot', callback: (event: BeforePlotEvent) => boolean | void): void;
463469
/** Subscribe to bare lifecycle events that carry no payload. */
464470
on(
465471
event:

‎test/types/events.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,57 @@ gd.on('plotly_webglcontextlost', (event) => {
3636

3737
gd.on('plotly_react', (event) => {
3838
const dataLength: number = event.data.length;
39+
const responsive: boolean | undefined = event.config?.responsive;
40+
const autosize: boolean | undefined = event.layout?.autosize;
41+
42+
// @ts-expect-error `config` is `undefined` if the `Plotly.react` call omitted it
43+
const unchecked: boolean | undefined = event.config.responsive;
3944
});
4045

4146
gd.on('plotly_legendtitleclick', (event) => {
4247
const legendId: string = event.legendId;
4348
return false;
4449
});
4550

51+
// A handler that only observes the event is valid
52+
gd.on('plotly_legendclick', (event) => {
53+
console.log(event.curveNumber);
54+
});
55+
56+
gd.on('plotly_legenddoubleclick', (event) => {
57+
console.log(event.curveNumber);
58+
});
59+
60+
gd.on('plotly_beforeplot', (event) => {
61+
console.log(event.data.length);
62+
});
63+
64+
gd.on('plotly_sunburstclick', (event) => {
65+
console.log(event.points.length);
66+
});
67+
68+
gd.on('plotly_treemapclick', (event) => {
69+
console.log(event.points.length);
70+
});
71+
72+
gd.on('plotly_icicleclick', (event) => {
73+
console.log(event.points.length);
74+
});
75+
76+
// A handler can return `false` to cancel the default behavior
77+
gd.on('plotly_legendclick', () => false);
78+
gd.on('plotly_beforeplot', () => false);
79+
gd.on('plotly_sunburstclick', () => false);
80+
gd.on('plotly_treemapclick', () => false);
81+
82+
gd.on('plotly_legendtitleclick', (event) => {
83+
console.log(event.legendId);
84+
});
85+
86+
gd.on('plotly_beforehover', (event) => {
87+
console.log(event.clientX);
88+
});
89+
4690
gd.on('plotly_buttonclicked', (event) => {
4791
const active: number = event.active;
4892
});
@@ -58,6 +102,10 @@ gd.on('plotly_icicleclick', (event) => {
58102

59103
gd.on('plotly_treemapclick', (event) => {
60104
const points: TreemapPlotDatum[] = event.points;
105+
106+
// @ts-expect-error `value` is absent unless the trace sets `values`
107+
const value: number = points[0].value;
108+
const optionalValue: number | undefined = points[0].value;
61109
});
62110

63111
gd.on('plotly_animating', () => {});

‎test/types/tsconfig.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"skipLibCheck": false
5+
},
36
"include": ["./**/*.ts"],
47
"exclude": []
58
}

0 commit comments

Comments
 (0)