|
1 | | -import { CSSResultGroup, html, LitElement, TemplateResult } from "lit"; |
| 1 | +import { CSSResultGroup, html, LitElement, PropertyValues, TemplateResult } from "lit"; |
2 | 2 | import { customElement, property, query } from "lit/decorators.js"; |
3 | 3 | import { ifDefined } from "lit/directives/if-defined.js"; |
4 | 4 | import { ReferenceElement } from "@floating-ui/core"; |
| 5 | +import { getTarget } from "../../utilities/elements"; |
5 | 6 | import { event, EventDispatcher } from "../../utilities/event"; |
6 | 7 | import "../popover/bl-popover"; |
7 | 8 | import BlPopover, { Placement } from "../popover/bl-popover"; |
@@ -39,11 +40,64 @@ export default class BlTooltip extends LitElement { |
39 | 40 | */ |
40 | 41 | @event("bl-tooltip-hide") private onHide: EventDispatcher<string>; |
41 | 42 |
|
| 43 | + @property() target: string | Element; |
| 44 | + |
| 45 | + protected update(changedProperties: PropertyValues) { |
| 46 | + if (changedProperties.has("target")) { |
| 47 | + const prev = changedProperties.get("target"); |
| 48 | + |
| 49 | + if (prev) { |
| 50 | + this._removeEvents(prev); |
| 51 | + } |
| 52 | + |
| 53 | + this._addEvents(); |
| 54 | + } |
| 55 | + |
| 56 | + super.update(changedProperties); |
| 57 | + } |
| 58 | + |
| 59 | + private _addEvents() { |
| 60 | + const target = getTarget(this.target); |
| 61 | + |
| 62 | + if (target) { |
| 63 | + target.addEventListener("focus", this.show, { capture: true }); |
| 64 | + target.addEventListener("mouseover", this.show); |
| 65 | + target.addEventListener("blur", this.hide, { capture: true }); |
| 66 | + target.addEventListener("mouseleave", this.hide); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private _removeEvents(value: string | Element) { |
| 71 | + const target = getTarget(value); |
| 72 | + |
| 73 | + if (target) { |
| 74 | + target.removeEventListener("focus", this.show, { capture: true }); |
| 75 | + target.removeEventListener("mouseover", this.show); |
| 76 | + target.removeEventListener("blur", this.hide, { capture: true }); |
| 77 | + target.removeEventListener("mouseleave", this.hide); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + connectedCallback() { |
| 82 | + super.connectedCallback(); |
| 83 | + |
| 84 | + this.show = this.show.bind(this); |
| 85 | + this.hide = this.hide.bind(this); |
| 86 | + |
| 87 | + this._addEvents(); |
| 88 | + } |
| 89 | + |
| 90 | + disconnectedCallback() { |
| 91 | + super.disconnectedCallback(); |
| 92 | + |
| 93 | + this._removeEvents(this.target); |
| 94 | + } |
| 95 | + |
42 | 96 | /** |
43 | 97 | * Shows tooltip |
44 | 98 | */ |
45 | 99 | show() { |
46 | | - this._popover.target = this.trigger; |
| 100 | + this._popover.target = this.target ?? this.trigger; |
47 | 101 | this._popover.show(); |
48 | 102 | this.onShow(""); |
49 | 103 | } |
@@ -78,9 +132,9 @@ export default class BlTooltip extends LitElement { |
78 | 132 |
|
79 | 133 | render(): TemplateResult { |
80 | 134 | return html` |
81 | | - ${this.triggerTemplate()} |
| 135 | + ${this.target ? "" : this.triggerTemplate()} |
82 | 136 | <bl-popover |
83 | | - .target="${this.trigger}" |
| 137 | + .target="${this.target ?? this.trigger}" |
84 | 138 | placement="${ifDefined(this.placement)}" |
85 | 139 | @bl-popover-hide="${() => this.onHide("")}" |
86 | 140 | > |
|
0 commit comments