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

new(zoom): add pinch and zoom support #1305

Merged
merged 6 commits into from
Aug 11, 2021

Conversation

tonyneel923
Copy link
Contributor

@tonyneel923 tonyneel923 commented Aug 4, 2021

💥 Breaking Changes

  • zoom.containerRef needs to be added to whatever element needs zoom functionality.
  • props.transformMatrix was changed to => initialTransformMatrix
  • passive/style/className were removed

🚀 Enhancements

Zoom now supports mobile pinch.

📝 Documentation

I consolidated the handlers all into react-use-gesture. A user can opt out by not adding the containerRef so it seems like passive was no longer necessary.

In order for mobile zoom to work fully users must add style={{ touchAction: 'none' }} to the element in order for the browser to allow js to handle pinch and zoom.

Also I had to use the newest version of use-gesture which is technically in beta because of bugs in version 9. By the author's own admission it is ready for production so I do not see an issue with it.

Discussion which prompted this PR: #1292

@tonyneel923
Copy link
Contributor Author

It seems the build fails because it tries to download from my companies mirror registry. I only have a work laptop so I cannot fix that unless I remove the yarn lock changes completely but that would make it not in sync.

Is it possible for someone to fix that minor issue for me? Anything else I can do.

@williaster
Copy link
Collaborator

Wow thanks for the great contribution @tonyneel923 ! ❤️

I will try to take a look at this tomorrow (sorry it's a bigger change so want to look at it more closely!). I just pulled your branch to update the lock file and it allowed me to push to your branch. Sorry if that's not what you meant, my only other thought was to add a .npmrc file in the monorepo root which specified the npm or yarn registry so this wouldn't be a problem in the future (thanks for pointing it out! I may add that separately in another PR)

Copy link
Collaborator

@williaster williaster left a comment

Choose a reason for hiding this comment

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

Overall this is looking killer! Just did a quick first pass and will check it out again tomorrow.

I think in the breaking changes (in addition to zoom.containerRef that you mentioned) we should also note that

  • props.transformMatrix was changed to => initialTransformMatrix
  • passive/style/className were removed

packages/visx-zoom/src/types.ts Outdated Show resolved Hide resolved
packages/visx-zoom/src/types.ts Outdated Show resolved Hide resolved
@@ -44,6 +46,8 @@ export interface ProvidedZoom {
reset: () => void;
/** Callback for a wheel event, updating scale based on props.wheelDelta, relative to the mouse position. */
handleWheel: (event: React.WheelEvent | WheelEvent) => void;
/** Callback for a react-use-gesture on pinch event, updating scale based on props.pinchDelta, relative to the pinch position */
handlePinch: Parameters<typeof useGesture>[0]['onPinch'];
Copy link
Collaborator

Choose a reason for hiding this comment

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

looks like they export UserHandlers from @use-gesture/react, so UserHandlers['onPinch'] might work

packages/visx-zoom/src/Zoom.tsx Outdated Show resolved Hide resolved
packages/visx-zoom/src/Zoom.tsx Show resolved Hide resolved
packages/visx-zoom/src/Zoom.tsx Outdated Show resolved Hide resolved
packages/visx-zoom/src/Zoom.tsx Outdated Show resolved Hide resolved
onPinch: handlePinch,
onWheel: ({ event }) => handleWheel(event),
},
{ target: containerRef, eventOptions: { passive: false }, drag: { filterTaps: true } },
Copy link
Collaborator

Choose a reason for hiding this comment

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

options seem legit to me

@tonyneel923
Copy link
Contributor Author

Wow thanks for the great contribution @tonyneel923 ! ❤️

I will try to take a look at this tomorrow (sorry it's a bigger change so want to look at it more closely!). I just pulled your branch to update the lock file and it allowed me to push to your branch. Sorry if that's not what you meant, my only other thought was to add a .npmrc file in the monorepo root which specified the npm or yarn registry so this wouldn't be a problem in the future (thanks for pointing it out! I may add that separately in another PR)

I am not sure why I did not think of a .npmrc file. We will go with I was tired. Thanks for the feedback on the pr. I will address it today.

@tonyneel923
Copy link
Contributor Author

I have updated the pr with your requested changes. I also changed pinchDelta to match wheelDelta as I realized the way it was would not work since you would have access to zoom in the callback. I tested on example and it worked perfectly.

@tonyneel923
Copy link
Contributor Author

Final note is I changed the test in zoom to not fail but I am not sure why it failed at finding one div because it definitely renders the contents. I do not have much experience with enzyme though and generally use rtl.

Copy link
Collaborator

@williaster williaster left a comment

Choose a reason for hiding this comment

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

Woot! @tonyneel923 this looks great to me 🔥 I pulled it locally and tested on my phone 💯

I had a couple super minor comments but then I think it's ready to land! Thanks again for the great addition 🙏 We are coordinating some breaking changes with react@17, so this may land first as an alpha but will be out soon.

packages/visx-zoom/src/Zoom.tsx Outdated Show resolved Hide resolved
packages/visx-zoom/src/Zoom.tsx Outdated Show resolved Hide resolved
packages/visx-zoom/src/Zoom.tsx Outdated Show resolved Hide resolved
packages/visx-zoom/src/Zoom.tsx Outdated Show resolved Hide resolved
@@ -33,8 +33,7 @@ describe('<Zoom />', () => {
</Zoom>,
);

expect(wrapper.find('div')).toHaveLength(1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think previously when passive=false (default), Zoom wrapped children in a div so that's why this is no longer there 👍

@williaster williaster changed the title feat: added pinch and zoom support to visx-zoom new(zoom): add pinch and zoom support Aug 9, 2021
@tonyneel923
Copy link
Contributor Author

I added in the changes you requested and I found a way to keep from running getBoundingClientRect all the time using the built in memo parameter in onPinch.

Copy link
Collaborator

@williaster williaster left a comment

Choose a reason for hiding this comment

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

Nice approach on the memoization! working on a release issue then will merge this 🎉

@github-actions
Copy link

🎉 This PR is included in version v2.0.2-alpha.0 of the packages modified 🎉

@github-actions
Copy link

🎉 This PR is included in version v2.0.3-alpha.0 of the packages modified 🎉

@github-actions
Copy link

🎉 This PR is included in version v2.1.0 of the packages modified 🎉

@boy51
Copy link
Contributor

boy51 commented Nov 10, 2021

Hey I appreciate this change! I'm using zoom in a very tight component and it would be really helpful we could be more explicit about breaking changes. For now, I don't feel like I can implement this for fear of introducing bugs.

For example, I used passive because we are handling mousewheel manually. How am I supposed to migrate? I see a brief mention of omitting the containerRef. What does it do for me in the first place?

Also why the change to initialTransformMatrix? Is it purely semantic? Should I just rename transformMatrix, or does it change the behaviour? If not, why is a breaking change needed for this?`

Also there are undocumented breakign changes, the interface ProvidedZoom now requires generic.

If there is documentation surrounding this please let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants