Skip to content

Commit

Permalink
Add PointerEvent support to getEventCoordinates method (#437)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman Kartsev <roman.kartsev@revolut.com>
Co-authored-by: Claudéric Demers <clauderic.d@gmail.com>
  • Loading branch information
3 people committed Aug 31, 2021
1 parent 8844cc7 commit 0e628bc
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/fix-pointerseensor-cypress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@dnd-kit/modifiers": patch
"@dnd-kit/utilities": patch
---

Added PointerEvent support to the `getEventCoordinates` method. This fixes testing the PointerSensor with Cypress (#436)
5 changes: 3 additions & 2 deletions packages/modifiers/src/snapCenterToCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {Modifier} from '@dnd-kit/core';
import {
getEventCoordinates,
isTouchEvent,
isMouseEvent,
hasViewportRelativeCoordinates,
} from '@dnd-kit/utilities';

export const snapCenterToCursor: Modifier = ({
Expand All @@ -13,7 +13,8 @@ export const snapCenterToCursor: Modifier = ({
if (
activeNodeRect &&
activatorEvent &&
(isTouchEvent(activatorEvent) || isMouseEvent(activatorEvent))
(isTouchEvent(activatorEvent) ||
hasViewportRelativeCoordinates(activatorEvent))
) {
const activatorCoordinates = getEventCoordinates(activatorEvent);
const offsetX = activatorCoordinates.x - activeNodeRect.left;
Expand Down
4 changes: 2 additions & 2 deletions packages/utilities/src/coordinates/getEventCoordinates.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Coordinates} from './types';
import {isMouseEvent, isTouchEvent} from '../event';
import {isTouchEvent, hasViewportRelativeCoordinates} from '../event';

/**
* Returns the normalized x and y coordinates for mouse and touch events.
Expand All @@ -23,7 +23,7 @@ export function getEventCoordinates(event: Event): Coordinates {
}
}

if (isMouseEvent(event)) {
if (hasViewportRelativeCoordinates(event)) {
return {
x: event.clientX,
y: event.clientY,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function hasViewportRelativeCoordinates(
event: Event
): event is Event & Pick<PointerEvent, 'clientX' | 'clientY'> {
return 'clientX' in event && 'clientY' in event;
}
2 changes: 1 addition & 1 deletion packages/utilities/src/event/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {isMouseEvent} from './isMouseEvent';
export {hasViewportRelativeCoordinates} from './hasViewportRelativeCoordinates';
export {isTouchEvent} from './isTouchEvent';
6 changes: 0 additions & 6 deletions packages/utilities/src/event/isMouseEvent.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/utilities/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export type {Coordinates} from './coordinates';
export {getEventCoordinates} from './coordinates';
export {CSS} from './css';
export type {Transform, Transition} from './css';
export {isMouseEvent, isTouchEvent} from './event';
export {hasViewportRelativeCoordinates, isTouchEvent} from './event';
export {canUseDOM} from './execution-context';
export type {Arguments, FirstArgument, Without} from './types';

0 comments on commit 0e628bc

Please sign in to comment.