Skip to content

Commit e45abd5

Browse files
committed
feat(tooltip): add visible boolean
1 parent a605582 commit e45abd5

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

elements/pf-v6-tooltip/pf-v6-tooltip.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { property } from 'lit/decorators/property.js';
66
import { styleMap } from 'lit/directives/style-map.js';
77
import { classMap } from 'lit/directives/class-map.js';
88

9+
import { observes } from '@patternfly/pfe-core/decorators/observes.js';
10+
911
import {
1012
FloatingDOMController,
1113
type Placement,
@@ -66,7 +68,10 @@ const EXIT_EVENTS: readonly string[] = ['focusout', 'mouseleave'];
6668
export class PfV6Tooltip extends LitElement {
6769
static readonly styles: CSSStyleSheet[] = [styles];
6870

69-
/** Position of the tooltip relative to the trigger element */
71+
/** When true, the tooltip is displayed. */
72+
@property({ type: Boolean }) visible = false;
73+
74+
/** Position of the tooltip relative to the trigger element. */
7075
@property() position: Placement = 'top';
7176

7277
/** Tooltip content text. Overridden by the content slot. */
@@ -190,27 +195,35 @@ export class PfV6Tooltip extends LitElement {
190195

191196
/** Show the tooltip programmatically */
192197
async show(): Promise<void> {
198+
this.visible = true;
193199
await this.updateComplete;
194200
const placement = this.position;
195201
const offset =
196-
!placement?.match(/top|bottom/) ? 15
197-
: { mainAxis: 15, alignmentAxis: -4 };
198-
await this.#float.show({
199-
offset,
200-
placement,
201-
flip: !this.noFlip,
202-
fallbackPlacements: this.flipBehavior,
203-
});
202+
!placement?.match(/top|bottom/) ? 15
203+
: { mainAxis: 15, alignmentAxis: -4 };
204+
const flip = !this.noFlip;
205+
const fallbackPlacements = this.flipBehavior;
206+
await this.#float.show({ offset, placement, flip, fallbackPlacements });
204207
this.#setAriaDescribedBy(true);
205208
}
206209

207210
/** Hide the tooltip programmatically */
208211
async hide(): Promise<void> {
212+
this.visible = false;
209213
this.#clearTimers();
210214
this.#setAriaDescribedBy(false);
211215
await this.#float.hide();
212216
}
213217

218+
@observes('visible')
219+
protected _visibleChanged(): void {
220+
if (this.visible) {
221+
this.show();
222+
} else {
223+
this.hide();
224+
}
225+
}
226+
214227
#setAriaDescribedBy(add: boolean): void {
215228
const trigger = this.#invokerElement;
216229
const content = this.shadowRoot?.querySelector('#content') ?? null;

0 commit comments

Comments
 (0)