Skip to content

Commit

Permalink
Tooltip: Recalculate target after slotchange (#831)
Browse files Browse the repository at this point in the history
* Tooltip: Recalculate target after slotchange

The tooltip's positioning breaks after a slotchange, unless the tooltips target is recalculated.

* Update src/components/tooltip/tooltip.test.ts

Co-authored-by: Cory LaViska <cory@abeautifulsite.net>
  • Loading branch information
straversi and claviska committed Jul 20, 2022
1 parent c39c4a9 commit 849b643
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/components/tooltip/tooltip.test.ts
Expand Up @@ -134,4 +134,17 @@ describe('<sl-tooltip>', () => {
expect(afterHideHandler).to.have.been.calledOnce;
expect(base.hidden).to.be.true;
});

it('should recalculate its target when the slotted element changes', async () => {
const el = await fixture<SlTooltip>(html`
<sl-tooltip content="This is a tooltip" open>
<sl-button>Hover me</sl-button>
</sl-tooltip>
`);

el.innerHTML = '<sl-button>New element</sl-button>';
await el.updateComplete;

expect(el.getTarget().textContent).to.equal('New element');
});
});
6 changes: 5 additions & 1 deletion src/components/tooltip/tooltip.ts
Expand Up @@ -262,6 +262,10 @@ export default class SlTooltip extends LitElement {
return triggers.includes(triggerType);
}

handleSlotChange() {
this.target = this.getTarget();
}

private startPositioner() {
this.stopPositioner();
this.updatePositioner();
Expand Down Expand Up @@ -319,7 +323,7 @@ export default class SlTooltip extends LitElement {
render() {
return html`
<div class="tooltip-target" aria-describedby="tooltip">
<slot></slot>
<slot @slotchange=${this.handleSlotChange}></slot>
</div>
<div class="tooltip-positioner">
Expand Down

0 comments on commit 849b643

Please sign in to comment.