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

fixes #436 - Add PointerEvent support to getEventCoordinates method #437

Merged
merged 5 commits into from
Aug 31, 2021

Conversation

chestozo
Copy link
Contributor

@chestozo chestozo commented Aug 26, 2021

In order to test DnD with Pointer events - like mentioned here - #208 we need to change how getEventCoordinates handles pointer* events (right now we are getting always {x:0, y:0}.

@@ -0,0 +1,6 @@
export function isPointerEvent(event: Event): event is PointerEvent {
return (
(window?.PointerEvent && event instanceof PointerEvent) ||
Copy link

@exah exah Aug 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this for server side check, I believe we can't use optional chaining.

typeof window !== 'undefined' && window.PointerEvent != null && event instanceof window.PointerEvent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so there is mistake in isMouseEvent too, might be out of scope of the PR :)
However, optional chaining can't help with checking of global variable - it will fail with reference error.

Screenshot 2021-08-27 at 11 14 19

@@ -23,7 +23,7 @@ export function getEventCoordinates(event: Event): Coordinates {
}
}

if (isMouseEvent(event)) {
if (isMouseEvent(event) || isPointerEvent(event)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be much simpler to just refactor this to:

Suggested change
if (isMouseEvent(event) || isPointerEvent(event)) {
if ('clientX' in event && 'clientY' in event) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can do!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clauderic updated

@changeset-bot
Copy link

changeset-bot bot commented Aug 28, 2021

🦋 Changeset detected

Latest commit: 06cbdc1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@dnd-kit/modifiers Patch
@dnd-kit/utilities Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chestozo chestozo changed the title fixes #436 - Added isPointerEvent and updated getEventCoordinates fixes #436 - Add PointerEvent support to getEventCoordinates method Aug 28, 2021
(isTouchEvent(activatorEvent) || isMouseEvent(activatorEvent))
(isTouchEvent(activatorEvent) ||
isMouseEvent(activatorEvent) ||
isPointerEvent(activatorEvent))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like isPointerEvent is still beneficial to have because of this check

@@ -23,10 +23,11 @@ export function getEventCoordinates(event: Event): Coordinates {
}
}

if (isMouseEvent(event)) {
// In case of MouseEvent or PointerEvent.
if ('clientX' in event && 'clientY' in event) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function hasViewportRelativeCoordinates(event: Event): event is Event & Pick<PointerEvent, 'clientX' | 'clientY'> {
  return 'clientX' in event && 'clientY' in event;
}
Suggested change
if ('clientX' in event && 'clientY' in event) {
if (hasViewportRelativeCoordinates(event)) {

Comment on lines 29 to 30
x: (event as MouseEvent).clientX,
y: (event as MouseEvent).clientY,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x: (event as MouseEvent).clientX,
y: (event as MouseEvent).clientY,
x: event.clientX,
y: event.clientY,

export function isPointerEvent(event: Event): event is PointerEvent {
return (
(window?.PointerEvent && event instanceof PointerEvent) ||
event.type.includes('pointer')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is event.type.includes('pointer') required? Is this for Cypress?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we are using dnd-lib with pointer events and we were trying to reuse your cypress extensions but for pointer events

@chestozo
Copy link
Contributor Author

addressed comments

Copy link
Owner

@clauderic clauderic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution 🙏

@clauderic clauderic merged commit 0e628bc into clauderic:master Aug 31, 2021
@github-actions github-actions bot mentioned this pull request Aug 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants