Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pointer): trigger contextmenu on mousedown #811

Merged
merged 3 commits into from
Dec 13, 2021
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
9 changes: 4 additions & 5 deletions src/pointer/pointerPress.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable complexity */

import {fireEvent} from '@testing-library/dom'
import {
findClosest,
firePointerEvent,
Expand Down Expand Up @@ -134,6 +133,10 @@ function down(
})
}

if (pointerType === 'mouse' && button === 'secondary') {
fire('contextmenu')
}

return pressObj

function fire(type: string) {
Expand Down Expand Up @@ -231,10 +234,6 @@ function up(
)
}
}

if (pointerType === 'mouse' && button === 'secondary') {
fireEvent.contextMenu(target)
}
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/utils/pointer/firePointerEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,15 @@ export function firePointerEvent(
if (['pointerdown', 'pointerup'].includes(type)) {
init.isPrimary = isPrimary
}
init.button = getMouseButton(button ?? 0)
init.buttons = getMouseButtons(
...pointerState.pressed
.filter(p => p.keyDef.pointerType === pointerType)
.map(p => p.keyDef.button ?? 0),
)
if (
['mousedown', 'mouseup', 'pointerdown', 'pointerup', 'click'].includes(type)
['mousedown', 'mouseup', 'click', 'dblclick', 'contextmenu'].includes(type)
) {
init.button = getMouseButton(button ?? 0)
init.buttons = getMouseButtons(
...pointerState.pressed
.filter(p => p.keyDef.pointerType === pointerType)
.map(p => p.keyDef.button ?? 0),
)
}
if (['mousedown', 'mouseup', 'click', 'dblclick'].includes(type)) {
init.detail = clickCount
}

Expand Down
27 changes: 16 additions & 11 deletions tests/pointer/click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ test('click element', async () => {
expect(getEvents('click')).toHaveLength(1)
})

test('secondary button triggers contextmenu', async () => {
const {element, getClickEventsSnapshot, getEvents} = setup('<div />')
await userEvent.pointer({keys: '[MouseRight>]', target: element})

expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
mousedown - button=2; buttons=2; detail=1
contextmenu - button=2; buttons=2; detail=1
`)
expect(getEvents('contextmenu')).toHaveLength(1)
})

test('double click', async () => {
const {element, getClickEventsSnapshot, getEvents} = setup(`<div></div>`)

Expand Down Expand Up @@ -74,7 +86,7 @@ test('two clicks', async () => {
})

test('other keys reset click counter, but keyup/click still uses the old count', async () => {
const {element, getClickEventsSnapshot} = setup(`<div></div>`)
const {element, getClickEventsSnapshot, getEvents} = setup(`<div></div>`)

await userEvent.pointer({
keys: '[MouseLeft][MouseLeft>][MouseRight][MouseLeft]',
Expand All @@ -90,8 +102,8 @@ test('other keys reset click counter, but keyup/click still uses the old count',
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
mousedown - button=0; buttons=1; detail=2
mousedown - button=2; buttons=3; detail=1
contextmenu - button=2; buttons=3; detail=1
mouseup - button=2; buttons=1; detail=1
contextmenu - button=0; buttons=0; detail=0
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
mouseup - button=0; buttons=0; detail=2
click - button=0; buttons=0; detail=2
Expand All @@ -103,15 +115,8 @@ test('other keys reset click counter, but keyup/click still uses the old count',
click - button=0; buttons=0; detail=1
`)

// TODO: no click events after other button
// TODO: no multiple pointerdown events while another button is still pressed
// expect(getEvents('dblclick')).toHaveLength(0)
// expect(getEvents('click')).toHaveLength(1)
// expect(getEvents('mousedown')).toHaveLength(3)
// expect(getEvents('mousedown')[1]).toHaveProperty('detail', 2)
// expect(getEvents('mousedown')[3]).toHaveProperty('detail', 1)
// expect(getEvents('mouseup')).toHaveLength(3)
// expect(getEvents('mouseup')[1]).toHaveProperty('detail', 2)
expect(getEvents('mouseup')[2]).toHaveProperty('detail', 2)
expect(getEvents('mousedown')[3]).toHaveProperty('detail', 1)
})

test('click per touch device', async () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/pointer/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('drag sequence', async () => {
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
mousedown - button=0; buttons=1; detail=1
pointermove - pointerId=1; pointerType=mouse; isPrimary=undefined
mousemove - button=0; buttons=0; detail=0
mousemove - button=0; buttons=1; detail=0
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
mouseup - button=0; buttons=0; detail=1
click - button=0; buttons=0; detail=1
Expand Down