Skip to content

Commit 82d0242

Browse files
committed
Fix buggy tooltips.
1 parent cd8ef04 commit 82d0242

File tree

5 files changed

+57
-28
lines changed

5 files changed

+57
-28
lines changed

Diff for: package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jwt.io",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/jsonwebtoken/jsonwebtoken.github.io"

Diff for: src/editor/claims-tooltip.js

+52-23
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,50 @@ import tippy from 'tippy.js';
1111

1212
const timeClaims = ['exp', 'nbf', 'iat', 'auth_time', 'updated_at'];
1313

14+
let instance;
15+
1416
function 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

2560
function 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

70114
export 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
}

Diff for: test/functional/editor.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('Editor', function() {
7878
return element._tippy.state.visible;
7979
}
8080

81-
expect(await this.page.$eval('#decoded-jwt .output', tippyVisible)).
81+
expect(await this.page.$eval('[data-tippy]', tippyVisible)).
8282
to.be.false;
8383

8484
const iatPos = await this.page.evaluate(() => {
@@ -93,7 +93,7 @@ describe('Editor', function() {
9393
// Wait for animation
9494
await this.page.waitFor(2000);
9595

96-
expect(await this.page.$eval('#decoded-jwt .output', tippyVisible))
96+
expect(await this.page.$eval('[data-tippy]', tippyVisible))
9797
.to.be.true;
9898
});
9999

Diff for: views/token-editor-common.pug

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#encoded-jwt.box-content.current
1212
.input.js-input
1313
#decoded-jwt.box-content
14-
.output(title='placeholder')
14+
.output
1515
.jwt-explained.jwt-header
1616
p.text-line HEADER:
1717
span ALGORITHM & TOKEN TYPE

0 commit comments

Comments
 (0)