All files / layer layer.js

91.18% Statements 31/34
84.62% Branches 33/39
100% Functions 8/8
91.18% Lines 31/34

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 83 84 85 86 87 88 89 90 91 92                                        11x 11x 1x 1x         10x 10x 9x         19x 19x     19x 1x         4x 4x 4x 4x             10x   10x     10x 10x 10x 10x     10x 10x           30x 30x 30x 30x   19x   11x             19x 20x      
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
/* global document */
import * as React from 'react';
import ReactDOM from 'react-dom';
import {Consumer} from './layers-manager.js';
import type {LayerPropsT, LayerComponentPropsT, LayerStateT} from './types.js';
 
class LayerComponent extends React.Component<
  LayerComponentPropsT,
  LayerStateT,
> {
  state = {container: null};
 
  componentDidMount() {
    const {onMount, mountNode} = this.props;
    if (mountNode) {
      onMount && onMount();
      return;
    }
    // There was no LayersManager added if this.props.host === undefined.
    // Use document.body is the case no LayersManager is used.
    const host =
      this.props.host !== undefined ? this.props.host : document.body;
    if (host) {
      this.addContainer(host);
    }
  }
 
  componentDidUpdate(prevProps) {
    const {host, mountNode} = this.props;
    Iif (mountNode) {
      return;
    }
    if (host && host !== prevProps.host && prevProps.host === null) {
      this.addContainer(host);
    }
  }
 
  componentWillUnmount() {
    const {container} = this.state;
    const {host, onUnmount} = this.props;
    onUnmount && onUnmount();
    host &&
      container &&
      host.contains(container) &&
      host.removeChild(container);
  }
 
  addContainer(host) {
    const {index, mountNode, onMount} = this.props;
    // Do nothing if mountNode is provided
    Iif (mountNode) {
      return;
    }
    Eif (host) {
      const container = host.ownerDocument.createElement('div');
      const sibling = typeof index === 'number' ? host.children[index] : null;
      sibling
        ? host.insertBefore(container, sibling)
        : host.appendChild(container);
      this.setState({container}, () => {
        onMount && onMount();
      });
    }
  }
 
  render() {
    const {container} = this.state;
    const {children, mountNode} = this.props;
    Eif (__BROWSER__) {
      if (mountNode || container) {
        // $FlowFixMe
        return ReactDOM.createPortal(children, mountNode || container);
      }
      return null;
    }
    return null;
  }
}
 
export default function Layer(props: LayerPropsT) {
  return (
    <Consumer>{({host}) => <LayerComponent {...props} host={host} />}</Consumer>
  );
}