2015-03-10 21:23:03 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-10 21:23:03 +00:00
|
|
|
*
|
2015-03-25 19:55:10 +00:00
|
|
|
* @flow
|
2017-09-06 23:28:50 +00:00
|
|
|
* @format
|
2015-03-10 21:23:03 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const Animated = require('Animated');
|
|
|
|
const EdgeInsetsPropType = require('EdgeInsetsPropType');
|
|
|
|
const NativeMethodsMixin = require('NativeMethodsMixin');
|
|
|
|
const React = require('React');
|
|
|
|
const createReactClass = require('create-react-class');
|
|
|
|
const PropTypes = require('prop-types');
|
|
|
|
const Touchable = require('Touchable');
|
2018-05-13 08:38:35 +00:00
|
|
|
const TouchableWithoutFeedback = require('TouchableWithoutFeedback');
|
|
|
|
const ViewPropTypes = require('ViewPropTypes');
|
|
|
|
|
|
|
|
import type {EdgeInsetsProp} from 'EdgeInsetsPropType';
|
|
|
|
import type {Props as TouchableWithoutFeedbackProps} from 'TouchableWithoutFeedback';
|
|
|
|
import type {ViewStyleProp} from 'StyleSheet';
|
2015-03-10 21:23:03 +00:00
|
|
|
|
2015-08-21 08:52:13 +00:00
|
|
|
type Event = Object;
|
2015-11-16 21:51:13 +00:00
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
type State = {
|
2016-08-09 13:32:41 +00:00
|
|
|
animationID: ?number,
|
|
|
|
scale: Animated.Value,
|
2015-03-25 19:55:10 +00:00
|
|
|
};
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const PRESS_RETENTION_OFFSET = {top: 20, left: 20, right: 20, bottom: 30};
|
2015-11-16 21:51:13 +00:00
|
|
|
|
2018-05-13 08:38:35 +00:00
|
|
|
type Props = $ReadOnly<{|
|
|
|
|
...TouchableWithoutFeedbackProps,
|
|
|
|
|
|
|
|
onPressWithCompletion?: ?Function,
|
|
|
|
onPressAnimationComplete?: ?Function,
|
|
|
|
pressRetentionOffset?: ?EdgeInsetsProp,
|
|
|
|
releaseVelocity?: ?number,
|
|
|
|
releaseBounciness?: ?number,
|
|
|
|
style?: ?ViewStyleProp,
|
|
|
|
|}>;
|
|
|
|
|
2015-03-10 21:23:03 +00:00
|
|
|
/**
|
|
|
|
* Example of using the `TouchableMixin` to play well with other responder
|
|
|
|
* locking views including `ScrollView`. `TouchableMixin` provides touchable
|
|
|
|
* hooks (`this.touchableHandle*`) that we forward events to. In turn,
|
|
|
|
* `TouchableMixin` expects us to implement some abstract methods to handle
|
|
|
|
* interesting interactions such as `handleTouchablePress`.
|
|
|
|
*/
|
2018-05-13 08:38:35 +00:00
|
|
|
const TouchableBounce = ((createReactClass({
|
2017-07-07 21:24:25 +00:00
|
|
|
displayName: 'TouchableBounce',
|
2015-03-10 21:23:03 +00:00
|
|
|
mixins: [Touchable.Mixin, NativeMethodsMixin],
|
|
|
|
|
|
|
|
propTypes: {
|
2018-05-13 08:38:35 +00:00
|
|
|
...TouchableWithoutFeedback.propTypes,
|
2015-03-10 21:23:03 +00:00
|
|
|
// The function passed takes a callback to start the animation which should
|
|
|
|
// be run after this onPress handler is done. You can use this (for example)
|
|
|
|
// to update UI before starting the animation.
|
2017-04-12 23:09:48 +00:00
|
|
|
onPressWithCompletion: PropTypes.func,
|
2015-03-10 21:23:03 +00:00
|
|
|
// the function passed is called after the animation is complete
|
2017-04-12 23:09:48 +00:00
|
|
|
onPressAnimationComplete: PropTypes.func,
|
2015-11-16 21:51:13 +00:00
|
|
|
/**
|
|
|
|
* When the scroll view is disabled, this defines how far your touch may
|
|
|
|
* move off of the button, before deactivating the button. Once deactivated,
|
|
|
|
* try moving it back and you'll see that the button is once again
|
|
|
|
* reactivated! Move it back and forth several times while the scroll view
|
|
|
|
* is disabled. Ensure you pass in a constant to reduce memory allocations.
|
|
|
|
*/
|
|
|
|
pressRetentionOffset: EdgeInsetsPropType,
|
2017-09-06 23:28:50 +00:00
|
|
|
releaseVelocity: PropTypes.number.isRequired,
|
|
|
|
releaseBounciness: PropTypes.number.isRequired,
|
2018-05-13 08:38:35 +00:00
|
|
|
/**
|
|
|
|
* Style to apply to the container/underlay. Most commonly used to make sure
|
|
|
|
* rounded corners match the wrapped component.
|
|
|
|
*/
|
|
|
|
style: ViewPropTypes.style,
|
2017-09-06 23:28:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {releaseBounciness: 10, releaseVelocity: 10};
|
2015-03-10 21:23:03 +00:00
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
getInitialState: function(): State {
|
2015-07-23 00:09:18 +00:00
|
|
|
return {
|
|
|
|
...this.touchableGetInitialState(),
|
|
|
|
scale: new Animated.Value(1),
|
|
|
|
};
|
2015-03-10 21:23:03 +00:00
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
bounceTo: function(
|
|
|
|
value: number,
|
|
|
|
velocity: number,
|
|
|
|
bounciness: number,
|
2017-09-06 23:28:50 +00:00
|
|
|
callback?: ?Function,
|
2015-03-25 19:55:10 +00:00
|
|
|
) {
|
2015-07-23 00:09:18 +00:00
|
|
|
Animated.spring(this.state.scale, {
|
|
|
|
toValue: value,
|
|
|
|
velocity,
|
|
|
|
bounciness,
|
2016-11-01 22:52:23 +00:00
|
|
|
useNativeDriver: true,
|
2015-07-23 00:09:18 +00:00
|
|
|
}).start(callback);
|
2015-03-10 21:23:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `Touchable.Mixin` self callbacks. The mixin will invoke these if they are
|
|
|
|
* defined on your component.
|
|
|
|
*/
|
2015-08-21 08:52:13 +00:00
|
|
|
touchableHandleActivePressIn: function(e: Event) {
|
2015-03-10 21:23:03 +00:00
|
|
|
this.bounceTo(0.93, 0.1, 0);
|
2015-08-21 08:52:13 +00:00
|
|
|
this.props.onPressIn && this.props.onPressIn(e);
|
2015-03-10 21:23:03 +00:00
|
|
|
},
|
|
|
|
|
2015-08-21 08:52:13 +00:00
|
|
|
touchableHandleActivePressOut: function(e: Event) {
|
2015-03-10 21:23:03 +00:00
|
|
|
this.bounceTo(1, 0.4, 0);
|
2015-08-21 08:52:13 +00:00
|
|
|
this.props.onPressOut && this.props.onPressOut(e);
|
2015-03-10 21:23:03 +00:00
|
|
|
},
|
|
|
|
|
2015-08-21 08:52:13 +00:00
|
|
|
touchableHandlePress: function(e: Event) {
|
2018-03-03 23:04:46 +00:00
|
|
|
const onPressWithCompletion = this.props.onPressWithCompletion;
|
2015-03-25 19:55:10 +00:00
|
|
|
if (onPressWithCompletion) {
|
2015-07-23 00:09:18 +00:00
|
|
|
onPressWithCompletion(() => {
|
|
|
|
this.state.scale.setValue(0.93);
|
2017-09-06 23:28:50 +00:00
|
|
|
this.bounceTo(
|
|
|
|
1,
|
|
|
|
this.props.releaseVelocity,
|
|
|
|
this.props.releaseBounciness,
|
|
|
|
this.props.onPressAnimationComplete,
|
|
|
|
);
|
2015-07-23 00:09:18 +00:00
|
|
|
});
|
2015-03-10 21:23:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-06 23:28:50 +00:00
|
|
|
this.bounceTo(
|
|
|
|
1,
|
|
|
|
this.props.releaseVelocity,
|
|
|
|
this.props.releaseBounciness,
|
|
|
|
this.props.onPressAnimationComplete,
|
|
|
|
);
|
2015-08-21 08:52:13 +00:00
|
|
|
this.props.onPress && this.props.onPress(e);
|
2015-03-10 21:23:03 +00:00
|
|
|
},
|
|
|
|
|
2015-11-16 21:51:13 +00:00
|
|
|
touchableGetPressRectOffset: function(): typeof PRESS_RETENTION_OFFSET {
|
|
|
|
return this.props.pressRetentionOffset || PRESS_RETENTION_OFFSET;
|
2015-03-10 21:23:03 +00:00
|
|
|
},
|
|
|
|
|
2016-02-17 00:50:35 +00:00
|
|
|
touchableGetHitSlop: function(): ?Object {
|
|
|
|
return this.props.hitSlop;
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
touchableGetHighlightDelayMS: function(): number {
|
2015-03-10 21:23:03 +00:00
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
|
2016-10-16 11:11:59 +00:00
|
|
|
render: function(): React.Element<any> {
|
2015-07-23 00:09:18 +00:00
|
|
|
return (
|
|
|
|
<Animated.View
|
|
|
|
style={[{transform: [{scale: this.state.scale}]}, this.props.style]}
|
2016-06-21 15:24:07 +00:00
|
|
|
accessible={this.props.accessible !== false}
|
2016-02-04 13:12:36 +00:00
|
|
|
accessibilityLabel={this.props.accessibilityLabel}
|
2018-07-26 00:33:58 +00:00
|
|
|
accessibilityHint={this.props.accessibilityHint}
|
2018-07-26 06:37:16 +00:00
|
|
|
accessibilityRole={this.props.accessibilityRole}
|
|
|
|
accessibilityStates={this.props.accessibilityStates}
|
2017-04-07 18:47:35 +00:00
|
|
|
nativeID={this.props.nativeID}
|
2015-07-23 00:09:18 +00:00
|
|
|
testID={this.props.testID}
|
2016-02-17 00:50:35 +00:00
|
|
|
hitSlop={this.props.hitSlop}
|
2015-07-23 00:09:18 +00:00
|
|
|
onStartShouldSetResponder={this.touchableHandleStartShouldSetResponder}
|
2017-09-06 23:28:50 +00:00
|
|
|
onResponderTerminationRequest={
|
|
|
|
this.touchableHandleResponderTerminationRequest
|
|
|
|
}
|
2015-07-23 00:09:18 +00:00
|
|
|
onResponderGrant={this.touchableHandleResponderGrant}
|
|
|
|
onResponderMove={this.touchableHandleResponderMove}
|
|
|
|
onResponderRelease={this.touchableHandleResponderRelease}
|
|
|
|
onResponderTerminate={this.touchableHandleResponderTerminate}>
|
2018-05-14 07:09:00 +00:00
|
|
|
{this.props.children}
|
2017-09-06 23:28:50 +00:00
|
|
|
{Touchable.renderDebugView({
|
|
|
|
color: 'orange',
|
|
|
|
hitSlop: this.props.hitSlop,
|
|
|
|
})}
|
2015-07-23 00:09:18 +00:00
|
|
|
</Animated.View>
|
|
|
|
);
|
2017-09-06 23:28:50 +00:00
|
|
|
},
|
2018-05-13 08:38:35 +00:00
|
|
|
}): any): React.ComponentType<Props>);
|
2015-03-10 21:23:03 +00:00
|
|
|
|
|
|
|
module.exports = TouchableBounce;
|