Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,104 @@ describe('whatInput', () => {
'keyboard'
)
})

describe('touch and mouse switching', () => {
const dispatchPointer = (
type: 'pointerdown' | 'pointermove',
pointerType: 'touch' | 'mouse',
screenX = 0
) =>
window.dispatchEvent(
new PointerEvent(type, { bubbles: true, pointerType, screenX })
)

beforeEach(() => {
vi.useFakeTimers()
vi.setSystemTime(new Date('2026-01-01'))
dispatchKeyboard()
})

afterEach(() => {
vi.useRealTimers()
})

it('restores mouse hover intent after continuous mouse movement following touch', () => {
dispatchPointer('pointerdown', 'touch')

for (let screenX = 1; screenX <= 3; screenX++) {
vi.advanceTimersByTime(50)
dispatchPointer('pointermove', 'mouse', screenX)

expect(
document.documentElement.getAttribute('data-whatintent')
).toBe('touch')
}

vi.advanceTimersByTime(50)
dispatchPointer('pointermove', 'mouse', 4)

expect(
document.documentElement.getAttribute('data-whatintent')
).toBe('mouse')

dispatchPointer('pointerdown', 'touch')

expect(
document.documentElement.getAttribute('data-whatintent')
).toBe('touch')
})

it('ignores compatibility mouse events without extending the touch delay', () => {
dispatchPointer('pointerdown', 'touch')
vi.advanceTimersByTime(100)
dispatchMouse()

expect(document.documentElement.getAttribute('data-whatinput')).toBe(
'touch'
)
expect(
document.documentElement.getAttribute('data-whatintent')
).toBe('touch')

vi.advanceTimersByTime(100)
dispatchMouse()

expect(document.documentElement.getAttribute('data-whatinput')).toBe(
'mouse'
)
expect(
document.documentElement.getAttribute('data-whatintent')
).toBe('mouse')

dispatchPointer('pointerdown', 'touch')

expect(document.documentElement.getAttribute('data-whatinput')).toBe(
'touch'
)
expect(
document.documentElement.getAttribute('data-whatintent')
).toBe('touch')
})

it('restarts the delay when touch movement continues', () => {
dispatchPointer('pointerdown', 'touch')
vi.advanceTimersByTime(150)
dispatchPointer('pointermove', 'touch', 10)
vi.advanceTimersByTime(50)
dispatchPointer('pointermove', 'mouse', 20)

expect(
document.documentElement.getAttribute('data-whatintent')
).toBe('touch')

vi.advanceTimersByTime(150)
dispatchPointer('pointermove', 'mouse', 30)

expect(
document.documentElement.getAttribute('data-whatintent')
).toBe('mouse')
})
})
})

describe('whatInput deferred setup', () => {
Expand Down
9 changes: 6 additions & 3 deletions packages/dnb-eufemia/src/shared/helpers/whatInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type InputType = 'initial' | 'keyboard' | 'mouse' | 'touch'
let currentInput: InputType = 'initial'
let currentIntent: InputType = 'initial'

let currentTimestamp = Date.now()
let lastTouchTimestamp = Date.now()

/** Keys to ignore – modifier keys that accompany pointer interactions. */
const ignoreMap = ['Shift', 'Control', 'Alt', 'Meta']
Expand Down Expand Up @@ -98,9 +98,12 @@ function validateTouch(value: InputType): boolean {
const isFalsePositive =
value === 'mouse' &&
currentInput === 'touch' &&
now - currentTimestamp < 200
now - lastTouchTimestamp < 200

if (value === 'touch') {
lastTouchTimestamp = now
}

currentTimestamp = now
return isFalsePositive
}

Expand Down
Loading