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(drag): add useDrag hook #902

Merged
merged 13 commits into from
Nov 5, 2020
Merged

new(drag): add useDrag hook #902

merged 13 commits into from
Nov 5, 2020

Conversation

williaster
Copy link
Collaborator

@williaster williaster commented Oct 29, 2020

🚀 Enhancements

This PR

  • adds a useDrag hook to @visx/drag to complement the render props <Drag /> component
    • (I need this for some @visx/annotation work)
  • refactors <Drag /> to use useDrag
  • bumps react peerDependency to ^16.8.0-0 to support hooks
  • adds support for PointerEvents (in addition to Mouse and Touch events) in drag callbacks
  • updates @visx/demo /drag-ii to use useDrag so we have examples of both usages
  • updates /docs/drag to include useDrag
  • adds minimal tests for all @visx/drag apis

Tested via

  • verified /drag-ii is still functional
  • Updated docs

@hshoff @kristw

@@ -64,27 +64,25 @@ export default function DragI({ width, height }: DragIProps) {
setDraggingItems(raise(draggingItems, i));
}}
>
{({ dragStart, dragEnd, dragMove, isDragging, dx, dy }) =>
(false && isDragging && console.log(d.id, d.x, dx)) || (
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

just removed this guy 😱


<g>
{isDragging && (
/* capture mouse events (note: <Drag /> does this for you) */
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

💬 this is the key missing feature from useDrag

);

// if we use useEffect, some callback invocations are skipped
useLayoutEffect(() => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the hook is the general approach I saw for trying to re-create the setState(state, callback) component api in hooks, except they all recommend useEffect. However, when I used useEffect I noticed in the /drag-ii demo that some of the callbacks were skipped (e.g., the useDragOptions.onDragStart [the callback] would be skipped sometimes, which resulted in the previous line being connected to the new line you were starting to draw [onDragStart creates a new line in /drag-ii]).

I don't have a ton of experience using useLayoutEffect vs useEffect, I just know that useLayoutEffect is blocking while useEffect isn't as documented in the react api and we get a warning in next now that useLayoutEffect cannot be used server-side (for this functionality I don't think it matters).

Would love any thoughts on this!

Copy link
Member

Choose a reason for hiding this comment

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

Hm. Not ideal but this could be something we revisit in the future.

@coveralls
Copy link

coveralls commented Oct 29, 2020

Pull Request Test Coverage Report for Build 344323691

  • 0 of 32 (0.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.2%) to 60.387%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/visx-drag/src/Drag.tsx 0 2 0.0%
packages/visx-drag/src/util/useStateWithCallback.ts 0 10 0.0%
packages/visx-drag/src/useDrag.tsx 0 20 0.0%
Totals Coverage Status
Change from base Build 336575030: -0.2%
Covered Lines: 1534
Relevant Lines: 2429

💛 - Coveralls

import { localPoint } from '@visx/event';
import useStateWithCallback from './util/useStateWithCallback';

type MouseOrTouchEvent = React.MouseEvent | React.TouchEvent;
Copy link
Member

Choose a reason for hiding this comment

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

Could add React.PointerEvent. Was added in React 16.4 https://reactjs.org/blog/2018/05/23/react-v-16-4.html#pointer-events

);

// if we use useEffect, some callback invocations are skipped
useLayoutEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Hm. Not ideal but this could be something we revisit in the future.

Copy link
Member

@hshoff hshoff left a comment

Choose a reason for hiding this comment

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

Just remembered, need to bump react peerDep to ^16.8.0-0. similar to @visx/tooltip. (hooks were added in 16.8 https://reactjs.org/docs/hooks-intro.html).
https://github.com/airbnb/visx/blob/master/packages/visx-tooltip/package.json#L39

@williaster williaster merged commit 762291e into master Nov 5, 2020
@williaster williaster deleted the chris--useDrag branch November 5, 2020 19:01
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