-
Notifications
You must be signed in to change notification settings - Fork 715
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
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1950484
new(drag): add useStateWithCallback util
williaster 5d170e9
new(drag): add useDrag
williaster c7b7d19
internal(drag): refactor Drag to use useDrag
williaster da370fe
test(drag): add useDrag test
williaster 15e79bd
internal(drag): use useLayoutEffect in useStateWithCallback
williaster a48ad55
test(drag): add tests for raise + useStateWithCallback
williaster c72d237
demo(drag-ii): refactor to useDrag
williaster 6b57391
docs(drag): update with useDrag
williaster ba22bc8
fix(drag): annotate types, re-export HandlerArgs from Drag
williaster c7af560
fix(drag): make UseDragOptions optional
williaster cd62d6c
new(drag): support PointerEvents in callbacks
williaster 48e8515
docs(drag): mention hook in Readme
williaster 049e56d
deps(drag): bump react peerDep to ^16.8.0-0
williaster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import React, { useState } from 'react'; | ||
import React, { useCallback, useState } from 'react'; | ||
import { LinePath } from '@visx/shape'; | ||
import { Drag } from '@visx/drag'; | ||
import { useDrag } from '@visx/drag'; | ||
import { curveBasis } from '@visx/curve'; | ||
import { LinearGradient } from '@visx/gradient'; | ||
|
||
|
@@ -15,6 +15,31 @@ export type DragIIProps = { | |
|
||
export default function DragII({ data = [], width, height }: DragIIProps) { | ||
const [lines, setLines] = useState<Lines>(data); | ||
const onDragStart = useCallback( | ||
currDrag => { | ||
// add the new line with the starting point | ||
setLines(currLines => [...currLines, [{ x: currDrag.x, y: currDrag.y }]]); | ||
}, | ||
[setLines], | ||
); | ||
const onDragMove = useCallback( | ||
currDrag => { | ||
// add the new point to the current line | ||
setLines(currLines => { | ||
const nextLines = [...currLines]; | ||
const newPoint = { x: currDrag.x + currDrag.dx, y: currDrag.y + currDrag.dy }; | ||
const lastIndex = nextLines.length - 1; | ||
nextLines[lastIndex] = [...(nextLines[lastIndex] || []), newPoint]; | ||
return nextLines; | ||
}); | ||
}, | ||
[setLines], | ||
); | ||
const { x = 0, y = 0, dx, dy, isDragging, dragStart, dragEnd, dragMove } = useDrag({ | ||
onDragStart, | ||
onDragMove, | ||
resetOnStart: true, | ||
}); | ||
|
||
return width < 10 ? null : ( | ||
<div className="DragII" style={{ touchAction: 'none' }}> | ||
|
@@ -33,63 +58,45 @@ export default function DragII({ data = [], width, height }: DragIIProps) { | |
y={d => d.y} | ||
/> | ||
))} | ||
<Drag | ||
width={width} | ||
height={height} | ||
resetOnStart | ||
onDragStart={({ x = 0, y = 0 }) => { | ||
// add the new line with the starting point | ||
setLines(currLines => [...currLines, [{ x, y }]]); | ||
}} | ||
onDragMove={({ x = 0, y = 0, dx, dy }) => { | ||
// add the new point to the current line | ||
setLines(currLines => { | ||
const nextLines = [...currLines]; | ||
const newPoint = { x: x + dx, y: y + dy }; | ||
const lastIndex = nextLines.length - 1; | ||
nextLines[lastIndex] = [...(nextLines[lastIndex] || []), newPoint]; | ||
return nextLines; | ||
}); | ||
}} | ||
> | ||
{({ x = 0, y = 0, dx, dy, isDragging, dragStart, dragEnd, dragMove }) => ( | ||
|
||
<g> | ||
{isDragging && ( | ||
/* capture mouse events (note: <Drag /> does this for you) */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💬 this is the key missing feature from |
||
<rect | ||
width={width} | ||
height={height} | ||
onMouseMove={dragMove} | ||
onMouseUp={dragEnd} | ||
fill="transparent" | ||
/> | ||
)} | ||
{/* decorate the currently drawing line */} | ||
{isDragging && ( | ||
<g> | ||
{/* decorate the currently drawing line */} | ||
{isDragging && ( | ||
<g> | ||
<rect | ||
fill="white" | ||
width={8} | ||
height={8} | ||
x={x + dx - 4} | ||
y={y + dy - 4} | ||
pointerEvents="none" | ||
/> | ||
<circle | ||
cx={x} | ||
cy={y} | ||
r={4} | ||
fill="transparent" | ||
stroke="white" | ||
pointerEvents="none" | ||
/> | ||
</g> | ||
)} | ||
{/* create the drawing area */} | ||
<rect | ||
fill="transparent" | ||
width={width} | ||
height={height} | ||
onMouseDown={dragStart} | ||
onMouseUp={dragEnd} | ||
onMouseMove={dragMove} | ||
onTouchStart={dragStart} | ||
onTouchEnd={dragEnd} | ||
onTouchMove={dragMove} | ||
fill="white" | ||
width={8} | ||
height={8} | ||
x={x + dx - 4} | ||
y={y + dy - 4} | ||
pointerEvents="none" | ||
/> | ||
<circle cx={x} cy={y} r={4} fill="transparent" stroke="white" pointerEvents="none" /> | ||
</g> | ||
)} | ||
</Drag> | ||
{/* create the drawing area */} | ||
<rect | ||
fill="transparent" | ||
width={width} | ||
height={height} | ||
onMouseDown={dragStart} | ||
onMouseUp={isDragging ? dragEnd : undefined} | ||
onMouseMove={isDragging ? dragMove : undefined} | ||
onTouchStart={dragStart} | ||
onTouchEnd={isDragging ? dragEnd : undefined} | ||
onTouchMove={isDragging ? dragMove : undefined} | ||
/> | ||
</g> | ||
</svg> | ||
<div className="deets"> | ||
<div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,44 @@ | ||
/* eslint-disable react/jsx-handler-names */ | ||
import React from 'react'; | ||
import { localPoint } from '@visx/event'; | ||
import useDrag, { UseDrag, UseDragOptions, HandlerArgs as HandlerArgsType } from './useDrag'; | ||
|
||
type MouseOrTouchEvent = React.MouseEvent | React.TouchEvent; | ||
export type HandlerArgs = HandlerArgsType; | ||
|
||
export type DragProps = { | ||
export type DragProps = UseDragOptions & { | ||
/** Children render function which is passed the state of dragging and callbacks for drag start/end/move. */ | ||
children: (args: ChildrenArgs) => React.ReactNode; | ||
children: (args: UseDrag) => React.ReactNode; | ||
/** Width of the drag container. */ | ||
width: number; | ||
/** Height of the drag container. */ | ||
height: number; | ||
/** Whether to render an invisible rect below children to capture the drag area as defined by width and height. */ | ||
captureDragArea?: boolean; | ||
/** Whether to reset drag state upon the start of a new drag. */ | ||
resetOnStart?: boolean; | ||
/** Optional callback invoked upon drag end. */ | ||
onDragEnd?: (args: HandlerArgs) => void; | ||
/** Optional callback invoked upon drag movement. */ | ||
onDragMove?: (args: HandlerArgs) => void; | ||
/** Optional callback invoked upon drag start. */ | ||
onDragStart?: (args: HandlerArgs) => void; | ||
}; | ||
|
||
export type DragState = { | ||
x: number | undefined; | ||
y: number | undefined; | ||
dx: number; | ||
dy: number; | ||
isDragging: boolean; | ||
}; | ||
|
||
export type HandlerArgs = DragState & { event: MouseOrTouchEvent }; | ||
|
||
type ChildrenArgs = DragState & { | ||
dragEnd: (event: MouseOrTouchEvent) => void; | ||
dragMove: (event: MouseOrTouchEvent) => void; | ||
dragStart: (event: MouseOrTouchEvent) => void; | ||
}; | ||
|
||
export default class Drag extends React.Component<DragProps, DragState> { | ||
static defaultProps = { | ||
captureDragArea: true, | ||
resetOnStart: false, | ||
}; | ||
|
||
state = { | ||
x: undefined, | ||
y: undefined, | ||
dx: 0, | ||
dy: 0, | ||
isDragging: false, | ||
}; | ||
|
||
handleDragStart = (event: MouseOrTouchEvent) => { | ||
const { onDragStart, resetOnStart } = this.props; | ||
event.persist(); | ||
|
||
this.setState( | ||
({ dx, dy }) => { | ||
const point = localPoint(event) || { x: 0, y: 0 }; | ||
return { | ||
isDragging: true, | ||
dx: resetOnStart ? 0 : dx, | ||
dy: resetOnStart ? 0 : dy, | ||
x: resetOnStart ? point.x : point.x - dx, | ||
y: resetOnStart ? point.y : point.y - dy, | ||
}; | ||
}, | ||
onDragStart && | ||
(() => { | ||
onDragStart({ ...this.state, event }); | ||
}), | ||
); | ||
}; | ||
|
||
handleDragMove = (event: MouseOrTouchEvent) => { | ||
const { onDragMove } = this.props; | ||
event.persist(); | ||
|
||
this.setState( | ||
({ x, y, isDragging }) => { | ||
const point = localPoint(event) || { x: 0, y: 0 }; | ||
return isDragging | ||
? { | ||
isDragging: true, | ||
dx: point.x - (x || 0), | ||
dy: point.y - (y || 0), | ||
} | ||
: null; | ||
}, | ||
onDragMove && | ||
(() => { | ||
if (this.state.isDragging) onDragMove({ ...this.state, event }); | ||
}), | ||
); | ||
}; | ||
|
||
handleDragEnd = (event: MouseOrTouchEvent) => { | ||
const { onDragEnd } = this.props; | ||
event.persist(); | ||
|
||
this.setState( | ||
{ isDragging: false }, | ||
onDragEnd && | ||
(() => { | ||
onDragEnd({ ...this.state, event }); | ||
}), | ||
); | ||
}; | ||
|
||
render() { | ||
const { x, y, dx, dy, isDragging } = this.state; | ||
const { children, width, height, captureDragArea } = this.props; | ||
return ( | ||
<> | ||
{isDragging && captureDragArea && ( | ||
<rect | ||
width={width} | ||
height={height} | ||
onMouseMove={this.handleDragMove} | ||
onMouseUp={this.handleDragEnd} | ||
fill="transparent" | ||
/> | ||
)} | ||
{children({ | ||
x, | ||
y, | ||
dx, | ||
dy, | ||
isDragging, | ||
dragEnd: this.handleDragEnd, | ||
dragMove: this.handleDragMove, | ||
dragStart: this.handleDragStart, | ||
})} | ||
</> | ||
); | ||
} | ||
export default function Drag({ | ||
captureDragArea = true, | ||
children, | ||
height, | ||
onDragEnd, | ||
onDragMove, | ||
onDragStart, | ||
resetOnStart, | ||
width, | ||
}: DragProps) { | ||
const drag = useDrag({ resetOnStart, onDragEnd, onDragMove, onDragStart }); | ||
|
||
return ( | ||
<> | ||
{drag.isDragging && captureDragArea && ( | ||
<rect | ||
width={width} | ||
height={height} | ||
onMouseMove={drag.dragMove} | ||
onMouseUp={drag.dragEnd} | ||
fill="transparent" | ||
/> | ||
)} | ||
{children(drag)} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as Drag } from './Drag'; | ||
export { default as useDrag } from './useDrag'; | ||
export { default as raise } from './util/raise'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just removed this guy 😱