Skip to content

Commit 5273de2

Browse files
committed
feat(tooltip): Refactor show and hide methods to use a delay helper function
1 parent 8ae5c7e commit 5273de2

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

src/components/tooltip/tooltip.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -257,38 +257,39 @@ export default class IgcTooltipComponent extends EventEmitterMixin<
257257
this._animationPlayer.stopAll();
258258
}
259259

260+
private _setDelay(ms: number): Promise<void> {
261+
clearTimeout(this._timeoutId);
262+
return new Promise((resolve) => {
263+
this._timeoutId = setTimeout(resolve, ms);
264+
});
265+
}
266+
260267
/** Shows the tooltip if not already showing. */
261268
public show = async (): Promise<boolean> => {
262-
clearTimeout(this._timeoutId);
263-
if (this.open) {
264-
return false;
265-
}
269+
if (this.open) return false;
266270

267-
return new Promise<boolean>((resolve) => {
268-
this._timeoutId = setTimeout(async () => {
269-
this.open = true;
270-
this.toBeShown = true;
271-
resolve(await this._toggleAnimation('open'));
272-
this.toBeShown = false;
273-
}, this.showDelay);
274-
});
271+
await this._setDelay(this.showDelay);
272+
273+
this.open = true;
274+
this.toBeShown = true;
275+
const result = await this._toggleAnimation('open');
276+
this.toBeShown = false;
277+
278+
return result;
275279
};
276280

277281
/** Hides the tooltip if not already hidden. */
278282
public hide = async (): Promise<boolean> => {
279-
clearTimeout(this._timeoutId);
280-
if (!this.open) {
281-
return false;
282-
}
283+
if (!this.open) return false;
283284

284-
return new Promise<boolean>((resolve) => {
285-
this._timeoutId = setTimeout(async () => {
286-
this.open = false;
287-
this.toBeHidden = true;
288-
resolve(await this._toggleAnimation('close'));
289-
this.toBeHidden = false;
290-
}, this.hideDelay);
291-
});
285+
await this._setDelay(this.hideDelay);
286+
287+
this.open = false;
288+
this.toBeHidden = true;
289+
const result = await this._toggleAnimation('close');
290+
this.toBeHidden = false;
291+
292+
return result;
292293
};
293294

294295
/** Toggles the tooltip between shown/hidden state after the appropriate delay. */

0 commit comments

Comments
 (0)