All files / react-native-web/src/modules/usePlatformMethods index.js

85.71% Statements 24/28
78.57% Branches 11/14
62.5% Functions 5/8
92% Lines 23/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                                  17x     8x 8x             8x   8x 4x     4x 12x 8x         8x   8x                                 608x 608x 608x       608x 884x 443x 443x   443x 443x 8x 8x         608x    
/**
 * Copyright (c) Nicolas Gallagher.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow
 */
 
import type { GenericStyleProp } from '../../types';
import type { ViewProps } from '../../exports/View';
 
import UIManager from '../../exports/UIManager';
import createDOMProps from '../createDOMProps';
import useStable from '../useStable';
import { useRef } from 'react';
 
const emptyObject = {};
 
function setNativeProps(node, nativeProps, classList, pointerEvents, style, previousStyleRef) {
  Eif (node != null && nativeProps) {
    const domProps = createDOMProps(null, {
      pointerEvents,
      ...nativeProps,
      classList: [classList, nativeProps.className],
      style: [style, nativeProps.style]
    });
 
    const nextDomStyle = domProps.style;
 
    if (previousStyleRef.current != null) {
      Iif (domProps.style == null) {
        domProps.style = {};
      }
      for (const styleName in previousStyleRef.current) {
        if (domProps.style[styleName] == null) {
          domProps.style[styleName] = '';
        }
      }
    }
 
    previousStyleRef.current = nextDomStyle;
 
    UIManager.updateView(node, domProps);
  }
}
 
/**
 * Adds non-standard methods to the hode element. This is temporarily until an
 * API like `ReactNative.measure(hostRef, callback)` is added to React Native.
 */
export default function usePlatformMethods({
  classList,
  pointerEvents,
  style
}: {
  classList?: Array<string | boolean>,
  style?: GenericStyleProp<*>,
  pointerEvents?: $PropertyType<ViewProps, 'pointerEvents'>
}): (hostNode: any) => void {
  const previousStyleRef = useRef(null);
  const setNativePropsArgsRef = useRef(null);
  setNativePropsArgsRef.current = { classList, pointerEvents, style };
 
  // Avoid creating a new ref on every render. The props only need to be
  // available to 'setNativeProps' when it is called.
  const ref = useStable(() => (hostNode: any) => {
    if (hostNode != null) {
      hostNode.measure = (callback) => UIManager.measure(hostNode, callback);
      hostNode.measureLayout = (relativeToNode, success, failure) =>
        UIManager.measureLayout(hostNode, relativeToNode, failure, success);
      hostNode.measureInWindow = (callback) => UIManager.measureInWindow(hostNode, callback);
      hostNode.setNativeProps = (nativeProps) => {
        const { classList, style, pointerEvents } = setNativePropsArgsRef.current || emptyObject;
        setNativeProps(hostNode, nativeProps, classList, pointerEvents, style, previousStyleRef);
      };
    }
  });
 
  return ref;
}