@@ -9,7 +9,7 @@ import { colorChartsLineTick } from "@cloudscape-design/design-tokens";
99import Popover from "../internal/components/popover" ;
1010import AsyncStore , { useSelector } from "../internal/utils/async-store" ;
1111import { DebouncedCall } from "../internal/utils/utils" ;
12- import { Point , TooltipContent , TooltipContentGetter } from "./interfaces-core" ;
12+ import { Point , TooltipContent } from "./interfaces-core" ;
1313
1414const MOUSE_LEAVE_DELAY = 300 ;
1515const LAST_DISMISS_DELAY = 250 ;
@@ -19,15 +19,14 @@ const LAST_DISMISS_DELAY = 250;
1919// The tooltip is can be hidden then we receive mouse-leave. It can also be pinned/unpinned on mouse click.
2020// Despite event names, they events also fire on keyboard interactions.
2121
22- export function useChartTooltip < State > (
22+ export function useChartTooltip (
2323 highcharts : null | typeof Highcharts ,
2424 getChart : ( ) => Highcharts . Chart ,
2525 tooltipProps ?: {
26- state : State ;
27- getContent : TooltipContentGetter < State > ;
26+ getContent : ( point : Point ) => null | TooltipContent ;
2827 } ,
2928) {
30- const tooltipStore = useRef ( new TooltipStore ( getChart , tooltipProps ?. getContent ) ) . current ;
29+ const tooltipStore = useRef ( new TooltipStore ( getChart ) ) . current ;
3130
3231 const options : Highcharts . Options = {
3332 chart : {
@@ -63,21 +62,24 @@ export function useChartTooltip<State>(
6362 } ,
6463 } ;
6564
66- return { options, props : { tooltipStore } } ;
65+ return { options, props : { tooltipStore, getContent : tooltipProps ?. getContent ?? ( ( ) => null ) } } ;
6766}
6867
69- export function ChartTooltip < TooltipState > ( {
68+ export function ChartTooltip ( {
7069 tooltipStore,
71- tooltipState ,
70+ getContent ,
7271} : {
73- tooltipStore : TooltipStore < TooltipState > ;
74- tooltipState : TooltipState ;
72+ tooltipStore : TooltipStore ;
73+ getContent : ( point : Point ) => null | TooltipContent ;
7574} ) {
7675 const tooltip = useSelector ( tooltipStore , ( s ) => s ) ;
77- if ( ! tooltip . content ) {
76+ if ( ! tooltip . visible ) {
77+ return null ;
78+ }
79+ const content = getContent ( tooltip . point ) ;
80+ if ( ! content ) {
7881 return null ;
7982 }
80- const renderedContent = tooltip . content ( tooltipState ) ;
8183 return (
8284 < Popover
8385 getTrack = { tooltipStore . getTrack }
@@ -87,36 +89,33 @@ export function ChartTooltip<TooltipState>({
8789 onDismiss = { tooltipStore . onDismiss }
8890 onMouseEnter = { tooltipStore . onMouseEnterTooltip }
8991 onMouseLeave = { tooltipStore . onMouseLeaveTooltip }
90- title = { renderedContent . title }
91- footer = { renderedContent . footer }
92+ title = { content . title }
93+ footer = { content . footer }
9294 >
93- { renderedContent . body }
95+ { content . body }
9496 </ Popover >
9597 ) ;
9698}
9799
98- interface ReactiveTooltipState < TooltipState > {
100+ interface ReactiveTooltipState {
99101 visible : boolean ;
100102 pinned : boolean ;
101103 point : Point ;
102- content : null | ( ( state : TooltipState ) => TooltipContent ) ;
103104}
104105
105- class TooltipStore < TooltipState > extends AsyncStore < ReactiveTooltipState < TooltipState > > {
106+ class TooltipStore extends AsyncStore < ReactiveTooltipState > {
106107 public getTrack : ( ) => null | HTMLElement | SVGElement = ( ) => null ;
107108
108109 private getChart : ( ) => Highcharts . Chart ;
109- private getContent ?: TooltipContentGetter < TooltipState > ;
110110 private targetElement : null | Highcharts . SVGElement = null ;
111111 private markerElement : null | Highcharts . SVGElement = null ;
112112 private mouseLeaveCall = new DebouncedCall ( ) ;
113113 private lastDismissTime = 0 ;
114114 private tooltipHovered = false ;
115115
116- constructor ( getChart : ( ) => Highcharts . Chart , getContent ?: TooltipContentGetter < TooltipState > ) {
117- super ( { visible : false , pinned : false , point : { x : 0 , y : 0 } , content : null } ) ;
116+ constructor ( getChart : ( ) => Highcharts . Chart ) {
117+ super ( { visible : false , pinned : false , point : { x : 0 , y : 0 } } ) ;
118118 this . getChart = getChart ;
119- this . getContent = getContent ;
120119 }
121120
122121 // When hovering (or focusing) over the target (point, bar, segment, etc.) we show the tooltip in the target coordinate.
@@ -131,8 +130,7 @@ class TooltipStore<TooltipState> extends AsyncStore<ReactiveTooltipState<Tooltip
131130 this . moveMarkers ( target ) ;
132131 this . set ( ( ) => {
133132 const point = { x : target . x , y : target . y ?? 0 } ;
134- const content = this . getContent ?.( point ) ?? null ;
135- return { visible : ! ! content , pinned : false , point, content } ;
133+ return { visible : true , pinned : false , point } ;
136134 } ) ;
137135 } ;
138136
@@ -147,16 +145,14 @@ class TooltipStore<TooltipState> extends AsyncStore<ReactiveTooltipState<Tooltip
147145 this . moveMarkers ( target ) ;
148146 this . set ( ( prev ) => {
149147 const point = target ? { x : target . x , y : target . y ?? 0 } : prev . point ;
150- const content = this . getContent ?.( point ) ?? null ;
151- return { visible : ! ! content , pinned : false , point, content } ;
148+ return { visible : true , pinned : false , point } ;
152149 } ) ;
153150 }
154151 // If the click point is missing or matches the current position and it wasn't recently dismissed - it is pinned in this position.
155152 else if ( new Date ( ) . getTime ( ) - this . lastDismissTime > LAST_DISMISS_DELAY ) {
156153 this . set ( ( prev ) => {
157154 const point = target ? { x : target . x , y : target . y ?? 0 } : prev . point ;
158- const content = this . getContent ?.( point ) ?? null ;
159- return { visible : ! ! content , pinned : ! ! content , point, content } ;
155+ return { visible : true , pinned : true , point } ;
160156 } ) ;
161157 }
162158 } ;
0 commit comments