-
Notifications
You must be signed in to change notification settings - Fork 730
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
typescript(vx-drag): re-write package in TypeScript #499
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c12d49f
typescript(vx-drag): re-write package in TypeScript
dc51704
docs(vx-drag): update type doc string
081e50e
typescript(vx-drag): use generic in raise util
961f399
fix(vx-drag): use functional setState and callbacks
fc75adc
fix(vx-drag): simplify handleDragEnd setState
c26afbc
deps(vx-drag): require react@^16.3.0-0 peerDep
40f8643
fix(Drag): handle null localPoint
9c1ea94
fix(vx-geo): fix declaration conflict
115df87
fix(vx-responsive): Fix getBoundingClientRect jest mock
da882cd
fix: lint + prettier
5c353ca
fix(vx-drag): MouseEvent => MouseEvent | TouchEvent
3b42ebb
deps(vx-text): add @types/lodash
afe9aff
deps(vx-responsive): add @types/lodash
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { localPoint } from '@vx/event'; | ||
import React from 'react'; | ||
|
||
export type DragProps = { | ||
/** Children render function which is passed the state of dragging and callbacks for drag start/end/move. */ | ||
children: (args: ChildrenArgs) => 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; | ||
}; | ||
|
||
type DragState = { | ||
x: number | undefined; | ||
y: number | undefined; | ||
dx: number; | ||
dy: number; | ||
isDragging: boolean; | ||
}; | ||
|
||
type HandlerArgs = DragState & { event: React.MouseEvent }; | ||
|
||
type ChildrenArgs = DragState & { | ||
dragEnd: (event: React.MouseEvent) => void; | ||
dragMove: (event: React.MouseEvent) => void; | ||
dragStart: (event: React.MouseEvent) => 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: React.MouseEvent) => { | ||
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 : -dx + point.x, | ||
y: resetOnStart ? point.y : -dy + point.y, | ||
}; | ||
}, | ||
onDragStart && | ||
(() => { | ||
onDragStart({ ...this.state, event }); | ||
}), | ||
); | ||
}; | ||
|
||
handleDragMove = (event: React.MouseEvent) => { | ||
const { onDragMove } = this.props; | ||
event.persist(); | ||
|
||
this.setState( | ||
({ x, y, isDragging }) => { | ||
const point = localPoint(event) || { x: 0, y: 0 }; | ||
return isDragging | ||
? { | ||
isDragging: true, | ||
dx: -((x || 0) - point.x), | ||
dy: -((y || 0) - point.y), | ||
} | ||
: null; | ||
}, | ||
onDragMove && | ||
(() => { | ||
if (this.state.isDragging) onDragMove({ ...this.state, event }); | ||
}), | ||
); | ||
}; | ||
|
||
handleDragEnd = (event: React.MouseEvent) => { | ||
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, | ||
})} | ||
</> | ||
); | ||
} | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** Given at an array of items, moves the item at the specified index to the end of the array. */ | ||
export default function raise<T>(items: T[], raiseIndex: number) { | ||
const array = [...items]; | ||
const lastIndex = array.length - 1; | ||
const [raiseItem] = array.splice(raiseIndex, 1); | ||
array.splice(lastIndex, 0, raiseItem); | ||
return array; | ||
} |
File renamed without changes.
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
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.
these could also be TouchEvent
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.
good catch, did a sweep 🧹