Skip to content

Instantly share code, notes, and snippets.

@sebastijandumancic
Created September 11, 2019 11:06
Show Gist options
  • Save sebastijandumancic/7afba0fd1c7f2b1562ae6a4d72e44e99 to your computer and use it in GitHub Desktop.
Save sebastijandumancic/7afba0fd1c7f2b1562ae6a4d72e44e99 to your computer and use it in GitHub Desktop.
Chat head code snippet
class DraggablePerson extends React.Component<Props> {
static whyDidYouRender = false;
state: State = {
isDragging: false
};
private panResponder: PanResponderInstance;
private pan = new Animated.ValueXY() as AnimatedValue;
private position = { x: 0, y: 0 };
constructor(props: Props) {
super(props);
this.pan.addListener((value) => this.position = value);
this.panResponder = PanResponder.create({
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onPanResponderGrant: () => {
// Set the initial value to the current state
// Should be rewritten to use Animated.addEventListener({value} => ...)
this.setState({
isDragging: true
});
this.pan.setOffset({
x: this.position.x,
y: this.position.y
});
this.pan.setValue({ x: 0, y: 0 });
},
// Move object while onPress is active. Snapping is handled later.
onPanResponderMove: Animated.event([
null, { dx: this.pan.x, dy: this.pan.y }
]),
// Handle swiper behaviour after object is released.
onPanResponderRelease: (e, { dx, dy, vx }) => {
// Fix jumping when moving the object second time.
this.pan.flattenOffset();
this.setState({
isDragging: false
});
if (!this.props.person) {
return;
}
// Send ending position to parent component.
this.props.calculateOverlapping(dx, dy, this.props.person.id);
// Animate springy tuff.
Animated.spring(this.pan, { toValue: { x: 0, y: 0 } }).start();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment