Skip to content

Commit b6a6f58

Browse files
authored
fix: Fix tooltip animation management (#42)
1 parent c16cc51 commit b6a6f58

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"javascript"
1313
],
1414
"private": false,
15+
"repository": "https://github.com/untemps/svelte-use-tooltip.git",
16+
"bugs": "https://github.com/untemps/svelte-use-tooltip/issues",
1517
"publishConfig": {
1618
"access": "public"
1719
},

src/Tooltip.js

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,8 @@ import { DOMObserver } from '@untemps/dom-observer'
33
class Tooltip {
44
static #instances = []
55

6-
#observer = null
7-
#events = []
8-
#enterDelay = 0
9-
#leaveDelay = 0
10-
#delay = null
11-
126
#tooltip = null
137

14-
#boundEnterHandler = null
15-
#boundLeaveHandler = null
16-
#boundWindowChangeHandler = null
17-
188
#target = null
199
#content = null
2010
#contentSelector = null
@@ -24,8 +14,19 @@ class Tooltip {
2414
#animated = false
2515
#animationEnterClassName = null
2616
#animationLeaveClassName = null
17+
#enterDelay = 0
18+
#leaveDelay = 0
2719
#offset = 10
2820

21+
#observer = null
22+
#events = []
23+
#delay = null
24+
#transitioning = false
25+
26+
#boundEnterHandler = null
27+
#boundLeaveHandler = null
28+
#boundWindowChangeHandler = null
29+
2930
static destroy() {
3031
Tooltip.#instances.forEach((instance) => {
3132
instance.destroy()
@@ -88,10 +89,13 @@ class Tooltip {
8889
offset,
8990
disabled
9091
) {
91-
const hasContentChanged = contentSelector !== this.#contentSelector || content !== this.#content
92-
const hasContainerClassNameChanged = containerClassName !== this.#containerClassName
93-
const hasPositionChanged = position !== this.#position
94-
const hasOffsetChanged = position !== this.#offset
92+
const hasContentChanged =
93+
(contentSelector !== undefined && contentSelector !== this.#contentSelector) ||
94+
(content !== undefined && content !== this.#content)
95+
const hasContainerClassNameChanged =
96+
containerClassName !== undefined && containerClassName !== this.#containerClassName
97+
const hasPositionChanged = position !== undefined && position !== this.#position
98+
const hasOffsetChanged = offset !== undefined && offset !== this.#offset
9599
const hasToDisableTarget = disabled && this.#boundEnterHandler
96100
const hasToEnableTarget = !disabled && !this.#boundEnterHandler
97101

@@ -326,31 +330,32 @@ class Tooltip {
326330

327331
#transitionTooltip(direction) {
328332
return new Promise((resolve) => {
329-
let classToAdd, classToRemove
330333
switch (direction) {
331334
case 1: {
332-
classToAdd = this.#animationEnterClassName
333-
classToRemove = this.#animationLeaveClassName
335+
this.#tooltip.classList.add(this.#animationEnterClassName)
336+
this.#tooltip.classList.remove(this.#animationLeaveClassName)
337+
this.#transitioning = false
338+
resolve()
334339
break
335340
}
336341
default: {
337-
classToAdd = this.#animationLeaveClassName
338-
classToRemove = this.#animationEnterClassName
339-
}
340-
}
341-
this.#tooltip.classList.add(classToAdd)
342-
this.#tooltip.classList.remove(classToRemove)
343-
344-
if (direction === 1) {
345-
resolve()
346-
}
342+
const onTransitionEnd = () => {
343+
this.#tooltip.removeEventListener('animationend', onTransitionEnd)
344+
this.#tooltip.classList.remove(this.#animationLeaveClassName)
345+
if (this.#transitioning) {
346+
this.#transitioning = false
347+
resolve()
348+
}
349+
}
347350

348-
const onTransitionEnd = () => {
349-
this.#tooltip.removeEventListener('animationend', onTransitionEnd)
350-
this.#tooltip.classList.remove(classToAdd)
351-
resolve()
351+
if (!this.#transitioning) {
352+
this.#tooltip.addEventListener('animationend', onTransitionEnd)
353+
this.#tooltip.classList.add(this.#animationLeaveClassName)
354+
this.#tooltip.classList.remove(this.#animationEnterClassName)
355+
this.#transitioning = true
356+
}
357+
}
352358
}
353-
this.#tooltip.addEventListener('animationend', onTransitionEnd)
354359
})
355360
}
356361

src/__tests__/useTooltip.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ describe('useTooltip', () => {
454454
const content = getElement('#content')
455455
expect(content).toBeInTheDocument()
456456
await fireEvent.animationEnd(content.parentNode)
457-
await standby(10)
458457
expect(content).not.toBeInTheDocument()
459458
})
460459

@@ -467,7 +466,6 @@ describe('useTooltip', () => {
467466
const content = getElement('#content')
468467
expect(content).toBeInTheDocument()
469468
await fireEvent.animationEnd(content.parentNode)
470-
await standby(10)
471469
expect(content).not.toBeInTheDocument()
472470
})
473471
})

0 commit comments

Comments
 (0)