TouchableNativeFeedback.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow strict-local
  8. * @format
  9. */
  10. 'use strict';
  11. import Pressability, {
  12. type PressabilityConfig,
  13. } from '../../Pressability/Pressability';
  14. import {PressabilityDebugView} from '../../Pressability/PressabilityDebug';
  15. import TVTouchable from './TVTouchable';
  16. import typeof TouchableWithoutFeedback from './TouchableWithoutFeedback';
  17. import {Commands} from 'react-native/Libraries/Components/View/ViewNativeComponent';
  18. import ReactNative from 'react-native/Libraries/Renderer/shims/ReactNative';
  19. import type {PressEvent} from 'react-native/Libraries/Types/CoreEventTypes';
  20. import Platform from '../../Utilities/Platform';
  21. import View from '../../Components/View/View';
  22. import processColor from '../../StyleSheet/processColor';
  23. import * as React from 'react';
  24. import invariant from 'invariant';
  25. type Props = $ReadOnly<{|
  26. ...React.ElementConfig<TouchableWithoutFeedback>,
  27. /**
  28. * Determines the type of background drawable that's going to be used to
  29. * display feedback. It takes an object with `type` property and extra data
  30. * depending on the `type`. It's recommended to use one of the static
  31. * methods to generate that dictionary.
  32. */
  33. background?: ?(
  34. | $ReadOnly<{|
  35. type: 'ThemeAttrAndroid',
  36. attribute:
  37. | 'selectableItemBackground'
  38. | 'selectableItemBackgroundBorderless',
  39. rippleRadius: ?number,
  40. |}>
  41. | $ReadOnly<{|
  42. type: 'RippleAndroid',
  43. color: ?number,
  44. borderless: boolean,
  45. rippleRadius: ?number,
  46. |}>
  47. ),
  48. /**
  49. * TV preferred focus (see documentation for the View component).
  50. */
  51. hasTVPreferredFocus?: ?boolean,
  52. /**
  53. * TV next focus down (see documentation for the View component).
  54. */
  55. nextFocusDown?: ?number,
  56. /**
  57. * TV next focus forward (see documentation for the View component).
  58. */
  59. nextFocusForward?: ?number,
  60. /**
  61. * TV next focus left (see documentation for the View component).
  62. */
  63. nextFocusLeft?: ?number,
  64. /**
  65. * TV next focus right (see documentation for the View component).
  66. */
  67. nextFocusRight?: ?number,
  68. /**
  69. * TV next focus up (see documentation for the View component).
  70. */
  71. nextFocusUp?: ?number,
  72. /**
  73. * Set to true to add the ripple effect to the foreground of the view, instead
  74. * of the background. This is useful if one of your child views has a
  75. * background of its own, or you're e.g. displaying images, and you don't want
  76. * the ripple to be covered by them.
  77. *
  78. * Check TouchableNativeFeedback.canUseNativeForeground() first, as this is
  79. * only available on Android 6.0 and above. If you try to use this on older
  80. * versions, this will fallback to background.
  81. */
  82. useForeground?: ?boolean,
  83. |}>;
  84. type State = $ReadOnly<{|
  85. pressability: Pressability,
  86. |}>;
  87. class TouchableNativeFeedback extends React.Component<Props, State> {
  88. /**
  89. * Creates a value for the `background` prop that uses the Android theme's
  90. * default background for selectable elements.
  91. */
  92. static SelectableBackground: (
  93. rippleRadius: ?number,
  94. ) => $ReadOnly<{|
  95. attribute: 'selectableItemBackground',
  96. type: 'ThemeAttrAndroid',
  97. rippleRadius: ?number,
  98. |}> = (rippleRadius: ?number) => ({
  99. type: 'ThemeAttrAndroid',
  100. attribute: 'selectableItemBackground',
  101. rippleRadius,
  102. });
  103. /**
  104. * Creates a value for the `background` prop that uses the Android theme's
  105. * default background for borderless selectable elements. Requires API 21+.
  106. */
  107. static SelectableBackgroundBorderless: (
  108. rippleRadius: ?number,
  109. ) => $ReadOnly<{|
  110. attribute: 'selectableItemBackgroundBorderless',
  111. type: 'ThemeAttrAndroid',
  112. rippleRadius: ?number,
  113. |}> = (rippleRadius: ?number) => ({
  114. type: 'ThemeAttrAndroid',
  115. attribute: 'selectableItemBackgroundBorderless',
  116. rippleRadius,
  117. });
  118. /**
  119. * Creates a value for the `background` prop that uses the Android ripple with
  120. * the supplied color. If `borderless` is true, the ripple will render outside
  121. * of the view bounds. Requires API 21+.
  122. */
  123. static Ripple: (
  124. color: string,
  125. borderless: boolean,
  126. rippleRadius: ?number,
  127. ) => $ReadOnly<{|
  128. borderless: boolean,
  129. color: ?number,
  130. rippleRadius: ?number,
  131. type: 'RippleAndroid',
  132. |}> = (color: string, borderless: boolean, rippleRadius: ?number) => {
  133. const processedColor = processColor(color);
  134. invariant(
  135. processedColor == null || typeof processedColor === 'number',
  136. 'Unexpected color given for Ripple color',
  137. );
  138. return {
  139. type: 'RippleAndroid',
  140. color: processedColor,
  141. borderless,
  142. rippleRadius,
  143. };
  144. };
  145. /**
  146. * Whether `useForeground` is supported.
  147. */
  148. static canUseNativeForeground: () => boolean = () =>
  149. Platform.OS === 'android' && Platform.Version >= 23;
  150. _tvTouchable: ?TVTouchable;
  151. state: State = {
  152. pressability: new Pressability(this._createPressabilityConfig()),
  153. };
  154. _createPressabilityConfig(): PressabilityConfig {
  155. return {
  156. cancelable: !this.props.rejectResponderTermination,
  157. disabled: this.props.disabled,
  158. hitSlop: this.props.hitSlop,
  159. delayLongPress: this.props.delayLongPress,
  160. delayPressIn: this.props.delayPressIn,
  161. delayPressOut: this.props.delayPressOut,
  162. minPressDuration: 0,
  163. pressRectOffset: this.props.pressRetentionOffset,
  164. android_disableSound: this.props.touchSoundDisabled,
  165. onLongPress: this.props.onLongPress,
  166. onPress: this.props.onPress,
  167. onPressIn: event => {
  168. if (Platform.OS === 'android') {
  169. this._dispatchPressedStateChange(true);
  170. this._dispatchHotspotUpdate(event);
  171. }
  172. if (this.props.onPressIn != null) {
  173. this.props.onPressIn(event);
  174. }
  175. },
  176. onPressMove: event => {
  177. if (Platform.OS === 'android') {
  178. this._dispatchHotspotUpdate(event);
  179. }
  180. },
  181. onPressOut: event => {
  182. if (Platform.OS === 'android') {
  183. this._dispatchPressedStateChange(false);
  184. }
  185. if (this.props.onPressOut != null) {
  186. this.props.onPressOut(event);
  187. }
  188. },
  189. };
  190. }
  191. _dispatchPressedStateChange(pressed: boolean): void {
  192. if (Platform.OS === 'android') {
  193. const hostComponentRef = ReactNative.findHostInstance_DEPRECATED(this);
  194. if (hostComponentRef == null) {
  195. console.warn(
  196. 'Touchable: Unable to find HostComponent instance. ' +
  197. 'Has your Touchable component been unmounted?',
  198. );
  199. } else {
  200. Commands.setPressed(hostComponentRef, pressed);
  201. }
  202. }
  203. }
  204. _dispatchHotspotUpdate(event: PressEvent): void {
  205. if (Platform.OS === 'android') {
  206. const {locationX, locationY} = event.nativeEvent;
  207. const hostComponentRef = ReactNative.findHostInstance_DEPRECATED(this);
  208. if (hostComponentRef == null) {
  209. console.warn(
  210. 'Touchable: Unable to find HostComponent instance. ' +
  211. 'Has your Touchable component been unmounted?',
  212. );
  213. } else {
  214. Commands.hotspotUpdate(
  215. hostComponentRef,
  216. locationX ?? 0,
  217. locationY ?? 0,
  218. );
  219. }
  220. }
  221. }
  222. render(): React.Node {
  223. const element = React.Children.only(this.props.children);
  224. const children = [element.props.children];
  225. if (__DEV__) {
  226. if (element.type === View) {
  227. children.push(
  228. <PressabilityDebugView color="brown" hitSlop={this.props.hitSlop} />,
  229. );
  230. }
  231. }
  232. // BACKWARD-COMPATIBILITY: Focus and blur events were never supported before
  233. // adopting `Pressability`, so preserve that behavior.
  234. const {
  235. onBlur,
  236. onFocus,
  237. ...eventHandlersWithoutBlurAndFocus
  238. } = this.state.pressability.getEventHandlers();
  239. return React.cloneElement(
  240. element,
  241. {
  242. ...eventHandlersWithoutBlurAndFocus,
  243. ...getBackgroundProp(
  244. this.props.background === undefined
  245. ? TouchableNativeFeedback.SelectableBackground()
  246. : this.props.background,
  247. this.props.useForeground === true,
  248. ),
  249. accessible: this.props.accessible !== false,
  250. accessibilityLabel: this.props.accessibilityLabel,
  251. accessibilityRole: this.props.accessibilityRole,
  252. accessibilityState: this.props.accessibilityState,
  253. accessibilityActions: this.props.accessibilityActions,
  254. onAccessibilityAction: this.props.onAccessibilityAction,
  255. accessibilityValue: this.props.accessibilityValue,
  256. importantForAccessibility: this.props.importantForAccessibility,
  257. accessibilityLiveRegion: this.props.accessibilityLiveRegion,
  258. accessibilityViewIsModal: this.props.accessibilityViewIsModal,
  259. accessibilityElementsHidden: this.props.accessibilityElementsHidden,
  260. hasTVPreferredFocus: this.props.hasTVPreferredFocus,
  261. hitSlop: this.props.hitSlop,
  262. focusable:
  263. this.props.focusable !== false &&
  264. this.props.onPress !== undefined &&
  265. !this.props.disabled,
  266. nativeID: this.props.nativeID,
  267. nextFocusDown: this.props.nextFocusDown,
  268. nextFocusForward: this.props.nextFocusForward,
  269. nextFocusLeft: this.props.nextFocusLeft,
  270. nextFocusRight: this.props.nextFocusRight,
  271. nextFocusUp: this.props.nextFocusUp,
  272. onLayout: this.props.onLayout,
  273. testID: this.props.testID,
  274. },
  275. ...children,
  276. );
  277. }
  278. componentDidMount(): void {
  279. if (Platform.isTV) {
  280. this._tvTouchable = new TVTouchable(this, {
  281. getDisabled: () => this.props.disabled === true,
  282. onBlur: event => {
  283. if (this.props.onBlur != null) {
  284. this.props.onBlur(event);
  285. }
  286. },
  287. onFocus: event => {
  288. if (this.props.onFocus != null) {
  289. this.props.onFocus(event);
  290. }
  291. },
  292. onPress: event => {
  293. if (this.props.onPress != null) {
  294. this.props.onPress(event);
  295. }
  296. },
  297. });
  298. }
  299. }
  300. componentDidUpdate(prevProps: Props, prevState: State) {
  301. this.state.pressability.configure(this._createPressabilityConfig());
  302. }
  303. componentWillUnmount(): void {
  304. if (Platform.isTV) {
  305. if (this._tvTouchable != null) {
  306. this._tvTouchable.destroy();
  307. }
  308. }
  309. this.state.pressability.reset();
  310. }
  311. }
  312. const getBackgroundProp =
  313. Platform.OS === 'android'
  314. ? (background, useForeground) =>
  315. useForeground && TouchableNativeFeedback.canUseNativeForeground()
  316. ? {nativeForegroundAndroid: background}
  317. : {nativeBackgroundAndroid: background}
  318. : (background, useForeground) => null;
  319. module.exports = TouchableNativeFeedback;