Skip to content

Commit

Permalink
[review fix] introduce hasViewportRelativeCoordinates to check both m…
Browse files Browse the repository at this point in the history
…ouse events and pointer events
  • Loading branch information
Roman Kartsev committed Aug 31, 2021
1 parent f3f76b0 commit 7e25a75
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 22 deletions.
6 changes: 2 additions & 4 deletions packages/modifiers/src/snapCenterToCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type {Modifier} from '@dnd-kit/core';
import {
getEventCoordinates,
isTouchEvent,
isMouseEvent,
isPointerEvent,
hasViewportRelativeCoordinates,
} from '@dnd-kit/utilities';

export const snapCenterToCursor: Modifier = ({
Expand All @@ -15,8 +14,7 @@ export const snapCenterToCursor: Modifier = ({
activeNodeRect &&
activatorEvent &&
(isTouchEvent(activatorEvent) ||
isMouseEvent(activatorEvent) ||
isPointerEvent(activatorEvent))
hasViewportRelativeCoordinates(activatorEvent))
) {
const activatorCoordinates = getEventCoordinates(activatorEvent);
const offsetX = activatorCoordinates.x - activeNodeRect.left;
Expand Down
5 changes: 2 additions & 3 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 {isTouchEvent} from '../event';
import {isTouchEvent, hasViewportRelativeCoordinates} from '../event';

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

// In case of MouseEvent or PointerEvent.
if ('clientX' in event && 'clientY' in event) {
if (hasViewportRelativeCoordinates(event)) {
return {
x: (event as MouseEvent).clientX,
y: (event as MouseEvent).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;
}
3 changes: 1 addition & 2 deletions packages/utilities/src/event/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export {isMouseEvent} from './isMouseEvent';
export {isPointerEvent} from './isPointerEvent';
export {hasViewportRelativeCoordinates} from './hasViewportRelativeCoordinates';
export {isTouchEvent} from './isTouchEvent';
6 changes: 0 additions & 6 deletions packages/utilities/src/event/isMouseEvent.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/utilities/src/event/isPointerEvent.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, isPointerEvent, isTouchEvent} from './event';
export {hasViewportRelativeCoordinates, isTouchEvent} from './event';
export {canUseDOM} from './execution-context';
export type {Arguments, FirstArgument, Without} from './types';

0 comments on commit 7e25a75

Please sign in to comment.