Skip to content

Commit f248239

Browse files
authored
fix: defer resolution of tooltip target (#5674)
1 parent bc227bd commit f248239

2 files changed

Lines changed: 49 additions & 9 deletions

File tree

‎packages/tooltip/src/vaadin-tooltip.js‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import './vaadin-tooltip-overlay.js';
77
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
88
import { isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js';
9+
import { microTask } from '@vaadin/component-base/src/async.js';
10+
import { Debouncer } from '@vaadin/component-base/src/debounce.js';
911
import { addValueToAttribute, removeValueFromAttribute } from '@vaadin/component-base/src/dom-utils.js';
1012
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
1113
import { OverlayClassMixin } from '@vaadin/component-base/src/overlay-class-mixin.js';
@@ -551,13 +553,24 @@ class Tooltip extends OverlayClassMixin(ThemePropertyMixin(ElementMixin(PolymerE
551553
/** @private */
552554
__forChanged(forId) {
553555
if (forId) {
554-
const target = this.getRootNode().getElementById(forId);
556+
this.__setTargetByIdDebouncer = Debouncer.debounce(this.__setTargetByIdDebouncer, microTask, () =>
557+
this.__setTargetById(forId),
558+
);
559+
}
560+
}
555561

556-
if (target) {
557-
this.target = target;
558-
} else {
559-
console.warn(`No element with id="${forId}" found to show tooltip.`);
560-
}
562+
/** @private */
563+
__setTargetById(targetId) {
564+
if (!this.isConnected) {
565+
return;
566+
}
567+
568+
const target = this.getRootNode().getElementById(targetId);
569+
570+
if (target) {
571+
this.target = target;
572+
} else {
573+
console.warn(`No element with id="${targetId}" found to show tooltip.`);
561574
}
562575
}
563576

‎packages/tooltip/test/tooltip.test.js‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '@vaadin/testing-helpers';
1313
import sinon from 'sinon';
1414
import './not-animated-styles.js';
15+
import { html, render } from 'lit';
1516
import { Tooltip } from '../vaadin-tooltip.js';
1617
import { mouseenter, mouseleave, waitForIntersectionObserver } from './helpers.js';
1718

@@ -170,11 +171,35 @@ describe('vaadin-tooltip', () => {
170171
});
171172

172173
describe('element found', () => {
173-
it('should use for attribute to link target using ID', () => {
174+
it('should use for attribute to link target using ID', async () => {
174175
target.setAttribute('id', 'foo');
175176
tooltip.for = 'foo';
177+
await nextFrame();
176178
expect(tooltip.target).to.eql(target);
177179
});
180+
181+
it('should still target correct element after sorting the items differently', async () => {
182+
const container = fixtureSync('<div></div>');
183+
function renderTooltips(items) {
184+
render(
185+
html`
186+
${items.map(
187+
(item) => html`
188+
<vaadin-tooltip for="${item}"></vaadin-tooltip>
189+
<div id=${item}></div>
190+
`,
191+
)}
192+
`,
193+
container,
194+
);
195+
}
196+
197+
renderTooltips(['bar', 'foo']);
198+
renderTooltips(['foo']);
199+
200+
await nextFrame();
201+
expect(container.querySelector('vaadin-tooltip[for="foo"]').target).to.equal(container.querySelector('#foo'));
202+
});
178203
});
179204

180205
describe('element not found', () => {
@@ -186,14 +211,16 @@ describe('vaadin-tooltip', () => {
186211
console.warn.restore();
187212
});
188213

189-
it('should warn when element with given ID is not found', () => {
214+
it('should warn when element with given ID is not found', async () => {
190215
tooltip.for = 'bar';
216+
await nextFrame();
191217
expect(console.warn.called).to.be.true;
192218
});
193219

194-
it('should keep the target when providing incorrect for', () => {
220+
it('should keep the target when providing incorrect for', async () => {
195221
tooltip.target = target;
196222
tooltip.for = 'bar';
223+
await nextFrame();
197224
expect(tooltip.target).to.eql(target);
198225
});
199226
});

0 commit comments

Comments
 (0)