Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
129 changes: 129 additions & 0 deletions packages/dnb-eufemia/src/shared/helpers/__tests__/whatInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,135 @@ describe('whatInput', () => {
'keyboard'
)
})

describe('touch and mouse switching', () => {
const dispatchPointer = (
type: 'pointerdown' | 'pointermove' | 'pointerup',
pointerType: 'touch' | 'mouse' | 'pen',
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')
})

it.each(['touch', 'pen'] as const)(
'ignores compatibility mouse events after a held %s is released',
(pointerType) => {
dispatchPointer('pointerdown', pointerType)
vi.advanceTimersByTime(300)
dispatchPointer('pointerup', pointerType)

for (const delay of [0, 100]) {
vi.advanceTimersByTime(delay)
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')
}
)
})
})

describe('whatInput deferred setup', () => {
Expand Down
16 changes: 13 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 All @@ -41,6 +41,7 @@ function setUp() {
if (window.PointerEvent) {
window.addEventListener('pointerdown', setInput, true)
window.addEventListener('pointermove', setIntent, passiveCapture)
window.addEventListener('pointerup', trackTouchRelease, true)
} else {
window.addEventListener('mousemove', setIntent, passiveCapture)

Expand All @@ -66,6 +67,12 @@ function resolvePointerType(event: PointerEvent): InputType {
return 'mouse'
}

function trackTouchRelease(event: PointerEvent) {
if (resolvePointerType(event) === 'touch') {
lastTouchTimestamp = Date.now()
}
}

function resolveInputType(event: Event): InputType {
const type = event.type
if (type === 'keydown' || type === 'keyup') {
Expand Down Expand Up @@ -98,9 +105,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