Skip to content

Commit db6c3da

Browse files
authored
fix: tooltip: Tooltip delay should be reset on close (#5336)
* fix: tooltip: Tooltip delay should be reset on close * fix unit tests, add unit test
1 parent d0a677c commit db6c3da

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

components/tooltip/test/tooltip.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,37 @@ describe('d2l-tooltip', () => {
261261
await oneEvent(tooltipFixture, 'd2l-tooltip-show');
262262
expect(tooltip.showing).to.be.true;
263263
});
264+
265+
it('should show second tooltip without delay after hovering over first tooltip for greater than delay', async() => {
266+
const doubleTooltipFixture = html`
267+
<div>
268+
<button id="explicit-target1">Hover me for tips</button>
269+
<d2l-tooltip for="explicit-target1" for-type="descriptor">If I got a problem then a problem's got a problem.</d2l-tooltip>
270+
<button id="explicit-target2">Hover me for tips</button>
271+
<d2l-tooltip for="explicit-target2" for-type="descriptor">There might be another problem.</d2l-tooltip>
272+
</div>
273+
`;
274+
275+
const testFixture = await fixture(doubleTooltipFixture);
276+
277+
const target1 = testFixture.querySelector('#explicit-target1');
278+
const target2 = testFixture.querySelector('#explicit-target2');
279+
280+
const tooltips = testFixture.querySelectorAll('d2l-tooltip');
281+
const tooltip1 = tooltips[0];
282+
const tooltip2 = tooltips[1];
283+
284+
// display tooltip 1 then leave
285+
target1.dispatchEvent(new Event('mouseenter'));
286+
await oneEvent(testFixture, 'd2l-tooltip-show');
287+
expect(tooltip1.showing).to.be.true;
288+
target1.dispatchEvent(new Event('mouseleave'));
289+
290+
// don't wait delay, enter target2, tooltip 2 should show without having to wait for delay
291+
target2.dispatchEvent(new Event('mouseenter'));
292+
await aTimeout(tooltip.delay / 2);
293+
expect(tooltip2.showing).to.be.true;
294+
});
264295
});
265296

266297
describe('force-show', () => {

components/tooltip/tooltip.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ const contentBorderSize = 1;
2929
const contentHorizontalPadding = 15;
3030
const outlineSize = 1;
3131

32-
/* once a user shows a tooltip, ignore delay if they hover adjacent target within this timeout */
32+
/* once a user closes a tooltip, ignore delay if they hover adjacent target within this timeout */
3333
let delayTimeoutId;
3434
const resetDelayTimeout = () => {
3535
if (delayTimeoutId) clearTimeout(delayTimeoutId);
3636
delayTimeoutId = setTimeout(() => delayTimeoutId = null, 1000);
3737
};
38+
/* ignore delay if user hovers adjacent target when a tooltip is already open */
3839
const getDelay = delay => {
3940
if (delayTimeoutId) return 0;
4041
else return delay;
@@ -847,7 +848,6 @@ class Tooltip extends RtlMixin(LitElement) {
847848
if (!this._truncating) return;
848849
}
849850

850-
resetDelayTimeout();
851851
this._isHovering = true;
852852
this._updateShowing();
853853
}, getDelay(this.delay));
@@ -925,6 +925,7 @@ class Tooltip extends RtlMixin(LitElement) {
925925
this._dismissibleId = null;
926926
}
927927
if (dispatch) {
928+
resetDelayTimeout();
928929
this.dispatchEvent(new CustomEvent(
929930
'd2l-tooltip-hide', { bubbles: true, composed: true }
930931
));

0 commit comments

Comments
 (0)