2015-02-20 04:10:52 +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-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* @providesModule ScrollResponder
|
2015-03-25 19:55:10 +00:00
|
|
|
* @flow
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2018-01-05 20:35:43 +00:00
|
|
|
const Dimensions = require('Dimensions');
|
|
|
|
const FrameRateLogger = require('FrameRateLogger');
|
|
|
|
const Keyboard = require('Keyboard');
|
|
|
|
const ReactNative = require('ReactNative');
|
|
|
|
const Subscribable = require('Subscribable');
|
|
|
|
const TextInputState = require('TextInputState');
|
|
|
|
const UIManager = require('UIManager');
|
|
|
|
|
|
|
|
const invariant = require('fbjs/lib/invariant');
|
|
|
|
const nullthrows = require('fbjs/lib/nullthrows');
|
|
|
|
const performanceNow = require('fbjs/lib/performanceNow');
|
|
|
|
const warning = require('fbjs/lib/warning');
|
|
|
|
|
|
|
|
const { ScrollViewManager } = require('NativeModules');
|
2015-11-25 11:07:06 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* Mixin that can be integrated in order to handle scrolling that plays well
|
|
|
|
* with `ResponderEventPlugin`. Integrate with your platform specific scroll
|
|
|
|
* views, or even your custom built (every-frame animating) scroll views so that
|
|
|
|
* all of these systems play well with the `ResponderEventPlugin`.
|
|
|
|
*
|
|
|
|
* iOS scroll event timing nuances:
|
|
|
|
* ===============================
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Scrolling without bouncing, if you touch down:
|
|
|
|
* -------------------------------
|
|
|
|
*
|
|
|
|
* 1. `onMomentumScrollBegin` (when animation begins after letting up)
|
|
|
|
* ... physical touch starts ...
|
|
|
|
* 2. `onTouchStartCapture` (when you press down to stop the scroll)
|
|
|
|
* 3. `onTouchStart` (same, but bubble phase)
|
|
|
|
* 4. `onResponderRelease` (when lifting up - you could pause forever before * lifting)
|
|
|
|
* 5. `onMomentumScrollEnd`
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Scrolling with bouncing, if you touch down:
|
|
|
|
* -------------------------------
|
|
|
|
*
|
|
|
|
* 1. `onMomentumScrollBegin` (when animation begins after letting up)
|
|
|
|
* ... bounce begins ...
|
|
|
|
* ... some time elapses ...
|
|
|
|
* ... physical touch during bounce ...
|
|
|
|
* 2. `onMomentumScrollEnd` (Makes no sense why this occurs first during bounce)
|
|
|
|
* 3. `onTouchStartCapture` (immediately after `onMomentumScrollEnd`)
|
|
|
|
* 4. `onTouchStart` (same, but bubble phase)
|
|
|
|
* 5. `onTouchEnd` (You could hold the touch start for a long time)
|
|
|
|
* 6. `onMomentumScrollBegin` (When releasing the view starts bouncing back)
|
|
|
|
*
|
|
|
|
* So when we receive an `onTouchStart`, how can we tell if we are touching
|
|
|
|
* *during* an animation (which then causes the animation to stop)? The only way
|
|
|
|
* to tell is if the `touchStart` occurred immediately after the
|
|
|
|
* `onMomentumScrollEnd`.
|
|
|
|
*
|
|
|
|
* This is abstracted out for you, so you can just call this.scrollResponderIsAnimating() if
|
|
|
|
* necessary
|
|
|
|
*
|
|
|
|
* `ScrollResponder` also includes logic for blurring a currently focused input
|
|
|
|
* if one is focused while scrolling. The `ScrollResponder` is a natural place
|
|
|
|
* to put this logic since it can support not dismissing the keyboard while
|
|
|
|
* scrolling, unless a recognized "tap"-like gesture has occurred.
|
|
|
|
*
|
|
|
|
* The public lifecycle API includes events for keyboard interaction, responder
|
|
|
|
* interaction, and scrolling (among others). The keyboard callbacks
|
|
|
|
* `onKeyboardWill/Did/*` are *global* events, but are invoked on scroll
|
|
|
|
* responder's props so that you can guarantee that the scroll responder's
|
|
|
|
* internal state has been updated accordingly (and deterministically) by
|
|
|
|
* the time the props callbacks are invoke. Otherwise, you would always wonder
|
|
|
|
* if the scroll responder is currently in a state where it recognizes new
|
|
|
|
* keyboard positions etc. If coordinating scrolling with keyboard movement,
|
|
|
|
* *always* use these hooks instead of listening to your own global keyboard
|
|
|
|
* events.
|
|
|
|
*
|
|
|
|
* Public keyboard lifecycle API: (props callbacks)
|
|
|
|
*
|
|
|
|
* Standard Keyboard Appearance Sequence:
|
|
|
|
*
|
|
|
|
* this.props.onKeyboardWillShow
|
|
|
|
* this.props.onKeyboardDidShow
|
|
|
|
*
|
|
|
|
* `onScrollResponderKeyboardDismissed` will be invoked if an appropriate
|
|
|
|
* tap inside the scroll responder's scrollable region was responsible
|
|
|
|
* for the dismissal of the keyboard. There are other reasons why the
|
|
|
|
* keyboard could be dismissed.
|
|
|
|
*
|
|
|
|
* this.props.onScrollResponderKeyboardDismissed
|
|
|
|
*
|
|
|
|
* Standard Keyboard Hide Sequence:
|
|
|
|
*
|
|
|
|
* this.props.onKeyboardWillHide
|
|
|
|
* this.props.onKeyboardDidHide
|
|
|
|
*/
|
|
|
|
|
2018-01-05 20:35:43 +00:00
|
|
|
const IS_ANIMATING_TOUCH_START_THRESHOLD_MS = 16;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
type State = {
|
2016-08-09 13:32:41 +00:00
|
|
|
isTouching: boolean,
|
|
|
|
lastMomentumScrollBeginTime: number,
|
|
|
|
lastMomentumScrollEndTime: number,
|
|
|
|
observedScrollSinceBecomingResponder: boolean,
|
|
|
|
becameResponderWhileAnimating: boolean,
|
2015-03-25 19:55:10 +00:00
|
|
|
};
|
|
|
|
type Event = Object;
|
|
|
|
|
2018-01-05 20:35:43 +00:00
|
|
|
const ScrollResponderMixin = {
|
2015-02-20 04:10:52 +00:00
|
|
|
mixins: [Subscribable.Mixin],
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderMixinGetInitialState: function(): State {
|
2015-02-20 04:10:52 +00:00
|
|
|
return {
|
|
|
|
isTouching: false,
|
|
|
|
lastMomentumScrollBeginTime: 0,
|
|
|
|
lastMomentumScrollEndTime: 0,
|
|
|
|
|
|
|
|
// Reset to false every time becomes responder. This is used to:
|
|
|
|
// - Determine if the scroll view has been scrolled and therefore should
|
|
|
|
// refuse to give up its responder lock.
|
|
|
|
// - Determine if releasing should dismiss the keyboard when we are in
|
2016-12-08 05:39:54 +00:00
|
|
|
// tap-to-dismiss mode (this.props.keyboardShouldPersistTaps !== 'always').
|
2015-02-20 04:10:52 +00:00
|
|
|
observedScrollSinceBecomingResponder: false,
|
|
|
|
becameResponderWhileAnimating: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke this from an `onScroll` event.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleScrollShouldSetResponder: function(): boolean {
|
2015-02-20 04:10:52 +00:00
|
|
|
return this.state.isTouching;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merely touch starting is not sufficient for a scroll view to become the
|
|
|
|
* responder. Being the "responder" means that the very next touch move/end
|
|
|
|
* event will result in an action/movement.
|
|
|
|
*
|
|
|
|
* Invoke this from an `onStartShouldSetResponder` event.
|
|
|
|
*
|
|
|
|
* `onStartShouldSetResponder` is used when the next move/end will trigger
|
|
|
|
* some UI movement/action, but when you want to yield priority to views
|
|
|
|
* nested inside of the view.
|
|
|
|
*
|
|
|
|
* There may be some cases where scroll views actually should return `true`
|
|
|
|
* from `onStartShouldSetResponder`: Any time we are detecting a standard tap
|
|
|
|
* that gives priority to nested views.
|
|
|
|
*
|
|
|
|
* - If a single tap on the scroll view triggers an action such as
|
|
|
|
* recentering a map style view yet wants to give priority to interaction
|
|
|
|
* views inside (such as dropped pins or labels), then we would return true
|
|
|
|
* from this method when there is a single touch.
|
|
|
|
*
|
|
|
|
* - Similar to the previous case, if a two finger "tap" should trigger a
|
|
|
|
* zoom, we would check the `touches` count, and if `>= 2`, we would return
|
|
|
|
* true.
|
|
|
|
*
|
|
|
|
*/
|
2016-12-08 05:39:54 +00:00
|
|
|
scrollResponderHandleStartShouldSetResponder: function(e: Event): boolean {
|
2018-01-05 20:35:43 +00:00
|
|
|
const currentlyFocusedTextInput = TextInputState.currentlyFocusedField();
|
2016-12-08 05:39:54 +00:00
|
|
|
|
|
|
|
if (this.props.keyboardShouldPersistTaps === 'handled' &&
|
|
|
|
currentlyFocusedTextInput != null &&
|
|
|
|
e.target !== currentlyFocusedTextInput) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* There are times when the scroll view wants to become the responder
|
|
|
|
* (meaning respond to the next immediate `touchStart/touchEnd`), in a way
|
|
|
|
* that *doesn't* give priority to nested views (hence the capture phase):
|
|
|
|
*
|
|
|
|
* - Currently animating.
|
2016-11-25 13:30:05 +00:00
|
|
|
* - Tapping anywhere that is not a text input, while the keyboard is
|
2015-02-20 04:10:52 +00:00
|
|
|
* up (which should dismiss the keyboard).
|
|
|
|
*
|
|
|
|
* Invoke this from an `onStartShouldSetResponderCapture` event.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleStartShouldSetResponderCapture: function(e: Event): boolean {
|
2018-04-16 20:03:50 +00:00
|
|
|
// The scroll view should receive taps instead of its descendants if:
|
|
|
|
// * it is already animating/decelerating
|
|
|
|
if (this.scrollResponderIsAnimating()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// * the keyboard is up, keyboardShouldPersistTaps is 'never' (the default),
|
|
|
|
// and a new touch starts with a non-textinput target (in which case the
|
|
|
|
// first tap should be sent to the scroll view and dismiss the keyboard,
|
|
|
|
// then the second tap goes to the actual interior view)
|
2018-01-05 20:35:43 +00:00
|
|
|
const currentlyFocusedTextInput = TextInputState.currentlyFocusedField();
|
|
|
|
const {keyboardShouldPersistTaps} = this.props;
|
|
|
|
const keyboardNeverPersistTaps = !keyboardShouldPersistTaps ||
|
2016-12-08 05:39:54 +00:00
|
|
|
keyboardShouldPersistTaps === 'never';
|
|
|
|
if (keyboardNeverPersistTaps &&
|
2018-04-16 20:03:50 +00:00
|
|
|
currentlyFocusedTextInput != null
|
|
|
|
/* && !TextInputState.isTextInput(e.target) */) {
|
2015-02-20 04:10:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-16 20:03:50 +00:00
|
|
|
|
|
|
|
return false;
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke this from an `onResponderReject` event.
|
|
|
|
*
|
|
|
|
* Some other element is not yielding its role as responder. Normally, we'd
|
|
|
|
* just disable the `UIScrollView`, but a touch has already began on it, the
|
|
|
|
* `UIScrollView` will not accept being disabled after that. The easiest
|
|
|
|
* solution for now is to accept the limitation of disallowing this
|
|
|
|
* altogether. To improve this, find a way to disable the `UIScrollView` after
|
|
|
|
* a touch has already started.
|
|
|
|
*/
|
|
|
|
scrollResponderHandleResponderReject: function() {
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We will allow the scroll view to give up its lock iff it acquired the lock
|
|
|
|
* during an animation. This is a very useful default that happens to satisfy
|
|
|
|
* many common user experiences.
|
|
|
|
*
|
|
|
|
* - Stop a scroll on the left edge, then turn that into an outer view's
|
|
|
|
* backswipe.
|
|
|
|
* - Stop a scroll mid-bounce at the top, continue pulling to have the outer
|
|
|
|
* view dismiss.
|
|
|
|
* - However, without catching the scroll view mid-bounce (while it is
|
|
|
|
* motionless), if you drag far enough for the scroll view to become
|
|
|
|
* responder (and therefore drag the scroll view a bit), any backswipe
|
|
|
|
* navigation of a swipe gesture higher in the view hierarchy, should be
|
|
|
|
* rejected.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleTerminationRequest: function(): boolean {
|
2015-02-20 04:10:52 +00:00
|
|
|
return !this.state.observedScrollSinceBecomingResponder;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke this from an `onTouchEnd` event.
|
|
|
|
*
|
|
|
|
* @param {SyntheticEvent} e Event.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleTouchEnd: function(e: Event) {
|
2018-01-05 20:35:43 +00:00
|
|
|
const nativeEvent = e.nativeEvent;
|
2015-02-20 04:10:52 +00:00
|
|
|
this.state.isTouching = nativeEvent.touches.length !== 0;
|
|
|
|
this.props.onTouchEnd && this.props.onTouchEnd(e);
|
|
|
|
},
|
|
|
|
|
2017-10-19 22:15:09 +00:00
|
|
|
/**
|
|
|
|
* Invoke this from an `onTouchCancel` event.
|
|
|
|
*
|
|
|
|
* @param {SyntheticEvent} e Event.
|
|
|
|
*/
|
|
|
|
scrollResponderHandleTouchCancel: function(e: Event) {
|
|
|
|
this.state.isTouching = false;
|
|
|
|
this.props.onTouchCancel && this.props.onTouchCancel(e);
|
|
|
|
},
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* Invoke this from an `onResponderRelease` event.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleResponderRelease: function(e: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
this.props.onResponderRelease && this.props.onResponderRelease(e);
|
|
|
|
|
|
|
|
// By default scroll views will unfocus a textField
|
|
|
|
// if another touch occurs outside of it
|
2018-01-05 20:35:43 +00:00
|
|
|
const currentlyFocusedTextInput = TextInputState.currentlyFocusedField();
|
2016-12-08 05:39:54 +00:00
|
|
|
if (this.props.keyboardShouldPersistTaps !== true &&
|
|
|
|
this.props.keyboardShouldPersistTaps !== 'always' &&
|
2015-02-20 04:10:52 +00:00
|
|
|
currentlyFocusedTextInput != null &&
|
2015-05-18 22:30:17 +00:00
|
|
|
e.target !== currentlyFocusedTextInput &&
|
2015-02-20 04:10:52 +00:00
|
|
|
!this.state.observedScrollSinceBecomingResponder &&
|
|
|
|
!this.state.becameResponderWhileAnimating) {
|
|
|
|
this.props.onScrollResponderKeyboardDismissed &&
|
|
|
|
this.props.onScrollResponderKeyboardDismissed(e);
|
|
|
|
TextInputState.blurTextInput(currentlyFocusedTextInput);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleScroll: function(e: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
this.state.observedScrollSinceBecomingResponder = true;
|
|
|
|
this.props.onScroll && this.props.onScroll(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke this from an `onResponderGrant` event.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleResponderGrant: function(e: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
this.state.observedScrollSinceBecomingResponder = false;
|
|
|
|
this.props.onResponderGrant && this.props.onResponderGrant(e);
|
|
|
|
this.state.becameResponderWhileAnimating = this.scrollResponderIsAnimating();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unfortunately, `onScrollBeginDrag` also fires when *stopping* the scroll
|
|
|
|
* animation, and there's not an easy way to distinguish a drag vs. stopping
|
|
|
|
* momentum.
|
|
|
|
*
|
|
|
|
* Invoke this from an `onScrollBeginDrag` event.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleScrollBeginDrag: function(e: Event) {
|
2017-03-25 01:15:42 +00:00
|
|
|
FrameRateLogger.beginScroll(); // TODO: track all scrolls after implementing onScrollEndAnimation
|
2015-02-20 04:10:52 +00:00
|
|
|
this.props.onScrollBeginDrag && this.props.onScrollBeginDrag(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke this from an `onScrollEndDrag` event.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleScrollEndDrag: function(e: Event) {
|
2017-03-25 01:15:42 +00:00
|
|
|
const {velocity} = e.nativeEvent;
|
|
|
|
// - If we are animating, then this is a "drag" that is stopping the scrollview and momentum end
|
|
|
|
// will fire.
|
|
|
|
// - If velocity is non-zero, then the interaction will stop when momentum scroll ends or
|
|
|
|
// another drag starts and ends.
|
|
|
|
// - If we don't get velocity, better to stop the interaction twice than not stop it.
|
|
|
|
if (!this.scrollResponderIsAnimating() &&
|
|
|
|
(!velocity || velocity.x === 0 && velocity.y === 0)) {
|
|
|
|
FrameRateLogger.endScroll();
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
this.props.onScrollEndDrag && this.props.onScrollEndDrag(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke this from an `onMomentumScrollBegin` event.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleMomentumScrollBegin: function(e: Event) {
|
2017-04-06 01:42:46 +00:00
|
|
|
this.state.lastMomentumScrollBeginTime = performanceNow();
|
2015-02-20 04:10:52 +00:00
|
|
|
this.props.onMomentumScrollBegin && this.props.onMomentumScrollBegin(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke this from an `onMomentumScrollEnd` event.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleMomentumScrollEnd: function(e: Event) {
|
2017-03-25 01:15:42 +00:00
|
|
|
FrameRateLogger.endScroll();
|
2017-04-06 01:42:46 +00:00
|
|
|
this.state.lastMomentumScrollEndTime = performanceNow();
|
2015-02-20 04:10:52 +00:00
|
|
|
this.props.onMomentumScrollEnd && this.props.onMomentumScrollEnd(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke this from an `onTouchStart` event.
|
|
|
|
*
|
|
|
|
* Since we know that the `SimpleEventPlugin` occurs later in the plugin
|
|
|
|
* order, after `ResponderEventPlugin`, we can detect that we were *not*
|
|
|
|
* permitted to be the responder (presumably because a contained view became
|
|
|
|
* responder). The `onResponderReject` won't fire in that case - it only
|
|
|
|
* fires when a *current* responder rejects our request.
|
|
|
|
*
|
|
|
|
* @param {SyntheticEvent} e Touch Start event.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleTouchStart: function(e: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
this.state.isTouching = true;
|
|
|
|
this.props.onTouchStart && this.props.onTouchStart(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke this from an `onTouchMove` event.
|
|
|
|
*
|
|
|
|
* Since we know that the `SimpleEventPlugin` occurs later in the plugin
|
|
|
|
* order, after `ResponderEventPlugin`, we can detect that we were *not*
|
|
|
|
* permitted to be the responder (presumably because a contained view became
|
|
|
|
* responder). The `onResponderReject` won't fire in that case - it only
|
|
|
|
* fires when a *current* responder rejects our request.
|
|
|
|
*
|
|
|
|
* @param {SyntheticEvent} e Touch Start event.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderHandleTouchMove: function(e: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
this.props.onTouchMove && this.props.onTouchMove(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper function for this class that lets us quickly determine if the
|
|
|
|
* view is currently animating. This is particularly useful to know when
|
|
|
|
* a touch has just started or ended.
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderIsAnimating: function(): boolean {
|
2018-01-05 20:35:43 +00:00
|
|
|
const now = performanceNow();
|
|
|
|
const timeSinceLastMomentumScrollEnd = now - this.state.lastMomentumScrollEndTime;
|
|
|
|
const isAnimating = timeSinceLastMomentumScrollEnd < IS_ANIMATING_TOUCH_START_THRESHOLD_MS ||
|
2015-02-20 04:10:52 +00:00
|
|
|
this.state.lastMomentumScrollEndTime < this.state.lastMomentumScrollBeginTime;
|
|
|
|
return isAnimating;
|
|
|
|
},
|
|
|
|
|
2016-02-03 19:12:58 +00:00
|
|
|
/**
|
|
|
|
* Returns the node that represents native view that can be scrolled.
|
|
|
|
* Components can pass what node to use by defining a `getScrollableNode`
|
|
|
|
* function otherwise `this` is used.
|
|
|
|
*/
|
|
|
|
scrollResponderGetScrollableNode: function(): any {
|
|
|
|
return this.getScrollableNode ?
|
|
|
|
this.getScrollableNode() :
|
2016-04-08 02:43:49 +00:00
|
|
|
ReactNative.findNodeHandle(this);
|
2016-02-03 19:12:58 +00:00
|
|
|
},
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2017-01-27 18:09:20 +00:00
|
|
|
* A helper function to scroll to a specific point in the ScrollView.
|
|
|
|
* This is currently used to help focus child TextViews, but can also
|
2016-02-03 11:59:20 +00:00
|
|
|
* be used to quickly scroll to any element we want to focus. Syntax:
|
|
|
|
*
|
2017-01-27 18:09:20 +00:00
|
|
|
* `scrollResponderScrollTo(options: {x: number = 0; y: number = 0; animated: boolean = true})`
|
2016-02-03 11:59:20 +00:00
|
|
|
*
|
|
|
|
* Note: The weird argument signature is due to the fact that, for historical reasons,
|
|
|
|
* the function also accepts separate arguments as as alternative to the options object.
|
|
|
|
* This is deprecated due to ambiguity (y before x), and SHOULD NOT BE USED.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2016-02-03 11:59:20 +00:00
|
|
|
scrollResponderScrollTo: function(
|
2016-08-09 13:32:41 +00:00
|
|
|
x?: number | { x?: number, y?: number, animated?: boolean },
|
2016-02-03 11:59:20 +00:00
|
|
|
y?: number,
|
|
|
|
animated?: boolean
|
|
|
|
) {
|
|
|
|
if (typeof x === 'number') {
|
|
|
|
console.warn('`scrollResponderScrollTo(x, y, animated)` is deprecated. Use `scrollResponderScrollTo({x: 5, y: 5, animated: true})` instead.');
|
|
|
|
} else {
|
|
|
|
({x, y, animated} = x || {});
|
|
|
|
}
|
2016-02-01 12:03:55 +00:00
|
|
|
UIManager.dispatchViewManagerCommand(
|
2017-04-14 23:10:20 +00:00
|
|
|
nullthrows(this.scrollResponderGetScrollableNode()),
|
2016-02-01 12:03:55 +00:00
|
|
|
UIManager.RCTScrollView.Commands.scrollTo,
|
2016-02-03 11:59:20 +00:00
|
|
|
[x || 0, y || 0, animated !== false],
|
2016-02-01 12:03:55 +00:00
|
|
|
);
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2017-01-27 18:09:20 +00:00
|
|
|
/**
|
|
|
|
* Scrolls to the end of the ScrollView, either immediately or with a smooth
|
|
|
|
* animation.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* `scrollResponderScrollToEnd({animated: true})`
|
|
|
|
*/
|
|
|
|
scrollResponderScrollToEnd: function(
|
|
|
|
options?: { animated?: boolean },
|
|
|
|
) {
|
|
|
|
// Default to true
|
|
|
|
const animated = (options && options.animated) !== false;
|
|
|
|
UIManager.dispatchViewManagerCommand(
|
|
|
|
this.scrollResponderGetScrollableNode(),
|
|
|
|
UIManager.RCTScrollView.Commands.scrollToEnd,
|
|
|
|
[animated],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-09-23 12:57:00 +00:00
|
|
|
/**
|
2016-01-14 15:41:24 +00:00
|
|
|
* Deprecated, do not use.
|
2015-09-23 12:57:00 +00:00
|
|
|
*/
|
2015-12-29 20:41:34 +00:00
|
|
|
scrollResponderScrollWithoutAnimationTo: function(offsetX: number, offsetY: number) {
|
2016-01-14 15:41:24 +00:00
|
|
|
console.warn('`scrollResponderScrollWithoutAnimationTo` is deprecated. Use `scrollResponderScrollTo` instead');
|
2016-02-25 14:25:29 +00:00
|
|
|
this.scrollResponderScrollTo({x: offsetX, y: offsetY, animated: false});
|
2015-09-23 12:57:00 +00:00
|
|
|
},
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2016-02-03 11:59:20 +00:00
|
|
|
* A helper function to zoom to a specific rect in the scrollview. The argument has the shape
|
|
|
|
* {x: number; y: number; width: number; height: number; animated: boolean = true}
|
|
|
|
*
|
|
|
|
* @platform ios
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2016-02-03 11:59:20 +00:00
|
|
|
scrollResponderZoomTo: function(
|
2018-01-05 20:35:43 +00:00
|
|
|
rect: {| x: number, y: number, width: number, height: number, animated?: boolean |},
|
2016-02-03 11:59:20 +00:00
|
|
|
animated?: boolean // deprecated, put this inside the rect argument instead
|
|
|
|
) {
|
2016-11-29 22:29:53 +00:00
|
|
|
invariant(ScrollViewManager && ScrollViewManager.zoomToRect, 'zoomToRect is not implemented');
|
|
|
|
if ('animated' in rect) {
|
2018-01-05 20:35:43 +00:00
|
|
|
animated = rect.animated;
|
|
|
|
delete rect.animated;
|
2016-11-29 22:29:53 +00:00
|
|
|
} else if (typeof animated !== 'undefined') {
|
|
|
|
console.warn('`scrollResponderZoomTo` `animated` argument is deprecated. Use `options.animated` instead');
|
2015-11-25 11:07:06 +00:00
|
|
|
}
|
2016-11-29 22:29:53 +00:00
|
|
|
ScrollViewManager.zoomToRect(this.scrollResponderGetScrollableNode(), rect, animated !== false);
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2017-06-06 20:00:30 +00:00
|
|
|
/**
|
|
|
|
* Displays the scroll indicators momentarily.
|
|
|
|
*/
|
|
|
|
scrollResponderFlashScrollIndicators: function() {
|
2017-08-28 05:16:15 +00:00
|
|
|
UIManager.dispatchViewManagerCommand(
|
|
|
|
this.scrollResponderGetScrollableNode(),
|
|
|
|
UIManager.RCTScrollView.Commands.flashScrollIndicators,
|
|
|
|
[]
|
|
|
|
);
|
2017-06-06 20:00:30 +00:00
|
|
|
},
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* This method should be used as the callback to onFocus in a TextInputs'
|
|
|
|
* parent view. Note that any module using this mixin needs to return
|
2015-04-03 18:13:20 +00:00
|
|
|
* the parent view's ref in getScrollViewRef() in order to use this method.
|
|
|
|
* @param {any} nodeHandle The TextInput node handle
|
2016-12-16 23:08:48 +00:00
|
|
|
* @param {number} additionalOffset The scroll view's bottom "contentInset".
|
2015-04-03 18:13:20 +00:00
|
|
|
* Default is 0.
|
|
|
|
* @param {bool} preventNegativeScrolling Whether to allow pulling the content
|
|
|
|
* down to make it meet the keyboard's top. Default is false.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2015-04-03 18:13:20 +00:00
|
|
|
scrollResponderScrollNativeHandleToKeyboard: function(nodeHandle: any, additionalOffset?: number, preventNegativeScrollOffset?: bool) {
|
2015-02-20 04:10:52 +00:00
|
|
|
this.additionalScrollOffset = additionalOffset || 0;
|
2015-04-03 18:13:20 +00:00
|
|
|
this.preventNegativeScrollOffset = !!preventNegativeScrollOffset;
|
2015-11-25 11:07:06 +00:00
|
|
|
UIManager.measureLayout(
|
2015-02-20 04:10:52 +00:00
|
|
|
nodeHandle,
|
2016-04-08 02:43:49 +00:00
|
|
|
ReactNative.findNodeHandle(this.getInnerViewNode()),
|
2015-02-20 04:10:52 +00:00
|
|
|
this.scrollResponderTextInputFocusError,
|
|
|
|
this.scrollResponderInputMeasureAndScrollToKeyboard
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The calculations performed here assume the scroll view takes up the entire
|
|
|
|
* screen - even if has some content inset. We then measure the offsets of the
|
|
|
|
* keyboard, and compensate both for the scroll view's "contentInset".
|
|
|
|
*
|
|
|
|
* @param {number} left Position of input w.r.t. table view.
|
|
|
|
* @param {number} top Position of input w.r.t. table view.
|
|
|
|
* @param {number} width Width of the text input.
|
|
|
|
* @param {number} height Height of the text input.
|
|
|
|
*/
|
2015-04-03 18:13:20 +00:00
|
|
|
scrollResponderInputMeasureAndScrollToKeyboard: function(left: number, top: number, width: number, height: number) {
|
2018-01-05 20:35:43 +00:00
|
|
|
let keyboardScreenY = Dimensions.get('window').height;
|
2015-02-20 04:10:52 +00:00
|
|
|
if (this.keyboardWillOpenTo) {
|
2015-11-13 03:14:59 +00:00
|
|
|
keyboardScreenY = this.keyboardWillOpenTo.endCoordinates.screenY;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2018-01-05 20:35:43 +00:00
|
|
|
let scrollOffsetY = top - keyboardScreenY + height + this.additionalScrollOffset;
|
2015-11-13 03:14:59 +00:00
|
|
|
|
|
|
|
// By default, this can scroll with negative offset, pulling the content
|
|
|
|
// down so that the target component's bottom meets the keyboard's top.
|
|
|
|
// If requested otherwise, cap the offset at 0 minimum to avoid content
|
|
|
|
// shifting down.
|
|
|
|
if (this.preventNegativeScrollOffset) {
|
|
|
|
scrollOffsetY = Math.max(0, scrollOffsetY);
|
|
|
|
}
|
2016-02-19 13:08:11 +00:00
|
|
|
this.scrollResponderScrollTo({x: 0, y: scrollOffsetY, animated: true});
|
2015-11-13 03:14:59 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
this.additionalOffset = 0;
|
2015-04-03 18:13:20 +00:00
|
|
|
this.preventNegativeScrollOffset = false;
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderTextInputFocusError: function(e: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
console.error('Error measuring text field: ', e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `componentWillMount` is the closest thing to a standard "constructor" for
|
|
|
|
* React components.
|
|
|
|
*
|
|
|
|
* The `keyboardWillShow` is called before input focus.
|
|
|
|
*/
|
2018-02-08 18:26:45 +00:00
|
|
|
UNSAFE_componentWillMount: function() {
|
2018-01-05 20:35:43 +00:00
|
|
|
const {keyboardShouldPersistTaps} = this.props;
|
2016-12-08 05:39:54 +00:00
|
|
|
warning(
|
|
|
|
typeof keyboardShouldPersistTaps !== 'boolean',
|
|
|
|
`'keyboardShouldPersistTaps={${keyboardShouldPersistTaps}}' is deprecated. `
|
2017-10-10 00:37:08 +00:00
|
|
|
+ `Use 'keyboardShouldPersistTaps="${keyboardShouldPersistTaps ? 'always' : 'never'}"' instead`
|
2016-12-08 05:39:54 +00:00
|
|
|
);
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
this.keyboardWillOpenTo = null;
|
|
|
|
this.additionalScrollOffset = 0;
|
Added native event emitter
Summary:
This is a solution for the problem I raised in https://www.facebook.com/groups/react.native.community/permalink/768218933313687/
I've added a new native base class, `RCTEventEmitter` as well as an equivalent JS class/module `NativeEventEmitter` (RCTEventEmitter.js and EventEmitter.js were taken already).
Instead of arbitrary modules sending events via `bridge.eventDispatcher`, the idea is that any module that sends events should now subclass `RCTEventEmitter`, and provide an equivalent JS module that subclasses `NativeEventEmitter`.
JS code that wants to observe the events should now observe it via the specific JS module rather than via `RCTDeviceEventEmitter` directly. e.g. to observer a keyboard event, instead of writing:
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
RCTDeviceEventEmitter.addListener('keyboardWillShow', (event) => { ... });
You'd now write:
const Keyboard = require('Keyboard');
Keyboard.addListener('keyboardWillShow', (event) => { ... });
Within a component, you can also use the `Subscribable.Mixin` as you would previously, but instead of:
this.addListenerOn(RCTDeviceEventEmitter, 'keyboardWillShow', ...);
Write:
this.addListenerOn(Keyboard, 'keyboardWillShow', ...);
This approach allows the native `RCTKeyboardObserver` module to be created lazily the first time a listener is added, and to stop sending events when the last listener is removed. It also allows us to validate that the event strings being observed and omitted match the supported events for that module.
As a proof-of-concept, I've converted the `RCTStatusBarManager` and `RCTKeyboardObserver` modules to use the new system. I'll convert the rest in a follow up diff.
For now, the new `NativeEventEmitter` JS module wraps the `RCTDeviceEventEmitter` JS module, and just uses the native `RCTEventEmitter` module for bookkeeping. This allows for full backwards compatibility (code that is observing the event via `RCTDeviceEventEmitter` instead of the specific module will still work as expected, albeit with a warning). Once all legacy calls have been removed, this could be refactored to something more elegant internally, whilst maintaining the same public interface.
Note: currently, all device events still share a single global namespace, since they're really all registered on the same emitter instance internally. We should move away from that as soon as possible because it's not intuitive and will likely lead to strange bugs if people add generic events such as "onChange" or "onError" to their modules (which is common practice for components, where it's not a problem).
Reviewed By: javache
Differential Revision: D3269966
fbshipit-source-id: 1412daba850cd373020e1086673ba38ef9193050
2016-05-11 13:26:53 +00:00
|
|
|
this.addListenerOn(Keyboard, 'keyboardWillShow', this.scrollResponderKeyboardWillShow);
|
|
|
|
this.addListenerOn(Keyboard, 'keyboardWillHide', this.scrollResponderKeyboardWillHide);
|
|
|
|
this.addListenerOn(Keyboard, 'keyboardDidShow', this.scrollResponderKeyboardDidShow);
|
|
|
|
this.addListenerOn(Keyboard, 'keyboardDidHide', this.scrollResponderKeyboardDidHide);
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Warning, this may be called several times for a single keyboard opening.
|
|
|
|
* It's best to store the information in this method and then take any action
|
|
|
|
* at a later point (either in `keyboardDidShow` or other).
|
|
|
|
*
|
|
|
|
* Here's the order that events occur in:
|
|
|
|
* - focus
|
|
|
|
* - willShow {startCoordinates, endCoordinates} several times
|
|
|
|
* - didShow several times
|
|
|
|
* - blur
|
|
|
|
* - willHide {startCoordinates, endCoordinates} several times
|
|
|
|
* - didHide several times
|
|
|
|
*
|
|
|
|
* The `ScrollResponder` providesModule callbacks for each of these events.
|
|
|
|
* Even though any user could have easily listened to keyboard events
|
|
|
|
* themselves, using these `props` callbacks ensures that ordering of events
|
|
|
|
* is consistent - and not dependent on the order that the keyboard events are
|
|
|
|
* subscribed to. This matters when telling the scroll view to scroll to where
|
|
|
|
* the keyboard is headed - the scroll responder better have been notified of
|
|
|
|
* the keyboard destination before being instructed to scroll to where the
|
|
|
|
* keyboard will be. Stick to the `ScrollResponder` callbacks, and everything
|
|
|
|
* will work.
|
|
|
|
*
|
|
|
|
* WARNING: These callbacks will fire even if a keyboard is displayed in a
|
|
|
|
* different navigation pane. Filter out the events to determine if they are
|
|
|
|
* relevant to you. (For example, only if you receive these callbacks after
|
|
|
|
* you had explicitly focused a node etc).
|
|
|
|
*/
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderKeyboardWillShow: function(e: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
this.keyboardWillOpenTo = e;
|
|
|
|
this.props.onKeyboardWillShow && this.props.onKeyboardWillShow(e);
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
scrollResponderKeyboardWillHide: function(e: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
this.keyboardWillOpenTo = null;
|
|
|
|
this.props.onKeyboardWillHide && this.props.onKeyboardWillHide(e);
|
|
|
|
},
|
|
|
|
|
2015-07-14 01:17:35 +00:00
|
|
|
scrollResponderKeyboardDidShow: function(e: Event) {
|
2015-07-22 22:09:43 +00:00
|
|
|
// TODO(7693961): The event for DidShow is not available on iOS yet.
|
|
|
|
// Use the one from WillShow and do not assign.
|
|
|
|
if (e) {
|
|
|
|
this.keyboardWillOpenTo = e;
|
|
|
|
}
|
2015-07-14 01:17:35 +00:00
|
|
|
this.props.onKeyboardDidShow && this.props.onKeyboardDidShow(e);
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2015-11-13 03:14:59 +00:00
|
|
|
scrollResponderKeyboardDidHide: function(e: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
this.keyboardWillOpenTo = null;
|
2015-11-13 03:14:59 +00:00
|
|
|
this.props.onKeyboardDidHide && this.props.onKeyboardDidHide(e);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-01-05 20:35:43 +00:00
|
|
|
const ScrollResponder = {
|
2015-02-20 04:10:52 +00:00
|
|
|
Mixin: ScrollResponderMixin,
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ScrollResponder;
|