@@ -11,15 +11,50 @@ import tippy from 'tippy.js';
1111
1212const timeClaims = [ 'exp' , 'nbf' , 'iat' , 'auth_time' , 'updated_at' ] ;
1313
14+ let instance ;
15+
1416function hideTooltip ( ) {
15- decodedElement . _tippy . popper . style . opacity = 0 ;
17+ if ( instance ) {
18+ instance . destroy ( ) ;
19+ instance = null ;
20+ }
1621}
1722
18- function showTooltip ( text ) {
19- decodedElement . _tippy . popper . querySelector ( '.tippy-content' )
20- . textContent = text ;
21- decodedElement . _tippy . popperInstance . update ( ) ;
22- decodedElement . _tippy . popper . style . opacity = 1 ;
23+ function showTooltip ( element , text , placement ) {
24+ function newTooltip ( ) {
25+ element . title = text ;
26+
27+ tippy ( element , {
28+ placement : placement ,
29+ arrow : true ,
30+ performance : true ,
31+ size : 'large' ,
32+ dynamicTitle : true ,
33+ arrowTransform : 'scale(0.75)' ,
34+ distance : 10 ,
35+ updateDuration : 100 ,
36+ trigger : 'manual'
37+ } ) ;
38+
39+ return element . _tippy ;
40+ }
41+
42+ if ( instance ) {
43+ if ( instance . reference !== element ||
44+ instance . options . placement !== placement ) {
45+ instance . destroy ( ) ;
46+ instance = newTooltip ( ) ;
47+ } else if ( instance . popper . querySelector ( '.tippy-content' ) . textContent !==
48+ text ) {
49+ instance . popper . querySelector ( '.tippy-content' ) . textContent = text ;
50+ }
51+ } else {
52+ instance = newTooltip ( ) ;
53+ }
54+
55+ if ( ! instance . state . visible ) {
56+ instance . show ( ) ;
57+ }
2358}
2459
2560function getTimeText ( timeStr ) {
@@ -49,12 +84,21 @@ function tooltipHandler(event) {
4984
5085 const claim = matches [ 1 ] ;
5186
87+ const element = event . target . tagName === 'SPAN' ?
88+ event . target :
89+ event . target . querySelector ( 'span' ) ;
90+
91+ if ( ! element || element . tagName !== 'SPAN' ) {
92+ hideTooltip ( ) ;
93+ return ;
94+ }
95+
5296 // If this is a time claim and the mouse cursor is on top of the time,
5397 // let the time tooltip handle this, do nothing.
5498 // TODO: merge both tooltip handlers?
5599 const claimEnd = line . indexOf ( ':' ) ;
56100 if ( result . ch >= claimEnd && timeClaims . includes ( claim ) ) {
57- showTooltip ( getTimeText ( matches [ 2 ] ) ) ;
101+ showTooltip ( element , getTimeText ( matches [ 2 ] ) , 'right' ) ;
58102 return ;
59103 }
60104
@@ -64,25 +108,10 @@ function tooltipHandler(event) {
64108 return ;
65109 }
66110
67- showTooltip ( claimText ) ;
111+ showTooltip ( element , claimText , 'left' ) ;
68112}
69113
70114export function setupClaimsTooltip ( ) {
71- tippy ( decodedElement , {
72- placement : 'left' ,
73- arrow : true ,
74- followCursor : true ,
75- performance : true ,
76- size : 'large' ,
77- dynamicTitle : true ,
78- arrowTransform : 'scale(0.75)' ,
79- distance : 20 ,
80- sticky : true ,
81- updateDuration : 100
82- } ) ;
83-
84- decodedElement . _tippy . popper . style . opacity = 0 ;
85-
86115 payloadElement . addEventListener ( 'mousemove' , tooltipHandler ) ;
87116 headerElement . addEventListener ( 'mousemove' , tooltipHandler ) ;
88117}
0 commit comments