Skip to content

Commit a5cf039

Browse files
committed
fix: touch and mouse events
1 parent 3240ce2 commit a5cf039

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/index.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@
5454
data-tooltip-type="click">
5555
Click Me
5656
</button>
57-
58-
5957
</div>
60-
6158
</body>
6259

6360
</html>

src/js/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,27 @@ describe('SmartTooltip', () => {
3333
assert.equal(tooltipElement?.textContent, 'Test tooltip')
3434
})
3535

36+
test('should show tooltip on touchstart', () => {
37+
trigger.dispatchEvent(new MouseEvent('touchstart', { bubbles: true }))
38+
const tooltipElement = document.querySelector('div[class*="tooltip"]')
39+
assert.ok(tooltipElement?.classList.contains('visible'))
40+
assert.equal(tooltipElement?.textContent, 'Test tooltip')
41+
})
42+
3643
test('should hide tooltip on mouseout', () => {
3744
trigger.dispatchEvent(new MouseEvent('mouseover', { bubbles: true }))
3845
trigger.dispatchEvent(new MouseEvent('mouseout', { bubbles: true }))
3946
const tooltipElement = document.querySelector('div[class*="tooltip"]')
4047
assert.ok(!tooltipElement?.classList.contains('visible'))
4148
})
4249

50+
test('should hide tooltip on touchend', () => {
51+
trigger.dispatchEvent(new MouseEvent('touchstart', { bubbles: true }))
52+
trigger.dispatchEvent(new MouseEvent('touchend', { bubbles: true }))
53+
const tooltipElement = document.querySelector('div[class*="tooltip"]')
54+
assert.ok(!tooltipElement?.classList.contains('visible'))
55+
})
56+
4357
test('should toggle tooltip on click for click-type triggers', () => {
4458
trigger.setAttribute('data-tooltip-type', 'click')
4559
trigger.click()

src/js/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ export class SmartTooltip {
6666
}
6767

6868
private setupEventListeners() {
69-
document.addEventListener('pointerenter', this.handleMouseOver)
70-
document.addEventListener('pointerleave', this.handleMouseOut)
69+
document.addEventListener('mouseover', this.handleMouseOver)
70+
document.addEventListener('mouseout', this.handleMouseOut)
71+
document.addEventListener('touchstart', this.handleMouseOver)
72+
document.addEventListener('touchend', this.handleMouseOut)
7173
document.addEventListener('click', this.handleClick)
7274
window.addEventListener('resize', this.handleResize)
7375
window.addEventListener('scroll', this.handleScroll, true)
@@ -244,8 +246,10 @@ export class SmartTooltip {
244246
}
245247

246248
public destroy(): void {
247-
document.removeEventListener('pointerenter', this.handleMouseOver)
248-
document.removeEventListener('pointerleave', this.handleMouseOut)
249+
document.removeEventListener('mouseover', this.handleMouseOver)
250+
document.removeEventListener('mouseout', this.handleMouseOut)
251+
document.removeEventListener('touchstart', this.handleMouseOver)
252+
document.removeEventListener('touchend', this.handleMouseOut)
249253
document.removeEventListener('click', this.handleClick)
250254
window.removeEventListener('resize', this.handleResize)
251255
window.removeEventListener('scroll', this.handleScroll, true)

0 commit comments

Comments
 (0)