{"size": 1460, "ext": "js", "max_stars_repo_path": "components/placer/src/PlacerContext.js", "max_stars_repo_name": "fi11/uikit", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "components/placer/src/PlacerContext.js", "max_issues_repo_name": "fi11/uikit", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "components/placer/src/PlacerContext.js", "max_forks_repo_name": "fi11/uikit", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.9344262295, "max_line_length": 67, "alphanum_fraction": 0.6678082192}
import React from 'react';
import { findDOMNode } from 'react-dom';

const createPortalDomNode = zIndex => {
  if (typeof document === 'undefined') {
    return null;
  }

  const domNode = document.createElement('div');
  domNode.style.position = 'absolute';
  domNode.style.top = '0px';
  domNode.style.left = '0px';
  domNode.style.zIndex = zIndex;

  return domNode;
};

const { Provider, Consumer } = React.createContext({
  createPortalDOMNode(zIndex) {
    const domNode = createPortalDomNode(zIndex);

    document.body.appendChild(domNode);
    return domNode;
  },
});

export const PlacerContextConsumer = Consumer;

export class PlacerContext extends React.Component {
  createPortalDOMNode = zIndex => {
    const domNode = createPortalDomNode(zIndex);

    const selfDOMNode = findDOMNode(this);

    if (selfDOMNode) {
      const parentDOMNode = findDOMNode(this).parentNode;
      if (parentDOMNode) {
        const originalPosition = parentDOMNode.style.position;
        parentDOMNode.style.position = 'relative';
        parentDOMNode.appendChild(domNode);

        const originRemove = domNode.remove.bind(domNode);

        domNode.remove = () => {
          parentDOMNode.style.position = originalPosition;
          originRemove();
        };

        return domNode;
      }
    }

    document.body.appendChild(domNode);
    return domNode;
  };

  render() {
    return <Provider value={this}>{this.props.children}</Provider>;
  }
}
