<commit_before>import { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { closePopup } from 'rrp/actions';
import HigherOrderPopupComponent from 'rrp/higher-order-popup-component';
import { TYPE_POPUP } from 'rrp/popup-collection';
import { popupSelector } from 'rrp/portal';
import { getPopupPosition } from 'rrp/utils';

const PROP_TYPES = {
    anchor: PropTypes.oneOf(['bottom', 'left', 'right', 'top']),
    closePopup: PropTypes.func,
    dispatch: PropTypes.func,
    getRect: PropTypes.func.isRequired,
    id: PropTypes.string.isRequired,
    popupClassName: PropTypes.string,
    refreshPosition: PropTypes.bool,
    offset: PropTypes.number.isRequired
};

export const HOCPopup = ComposedComponent => {
    class Popup extends Component {
        constructor(props) {
            super(props);
            this.state = {};

            this.closePopup = () => {
                if (props.closePopup) {
                    props.closePopup(props.id);
                    return;
                }
                props.dispatch(closePopup(props.id));
            };
            this.refreshPositionHandler = () => this.refreshPosition();
        }

        componentDidMount() {
            this.setPopupPosition();
            window.addEventListener('mouseup', this.closePopup);
            window.addEventListener('resize', this.refreshPositionHandler);
        }

        componentWillReceiveProps(nextProps) {
            if (this.props.refreshPosition !== nextProps.refreshPosition) {
                this.refreshPosition();
            }
        }

        componentWillUnmount() {
            window.removeEventListener('mouseup', this.closePopup);
            window.removeEventListener('resize', this.refreshPositionHandler);
        }

        setPopupPosition() {
            const style = getPopupPosition(
                this.props.anchor,
                this.props.getRect(),
                this.popup.clientWidth,
                this.popup.clientHeight,
                window.innerWidth,
                window.innerHeight,
                this.props.offset);
            this.setState({ style });
        }

        refreshPosition() {
            window.requestAnimationFrame(() => this.setPopupPosition());
        }

        stopEvent(event) {
            event.stopPropagation();
        }

        render() {
            return (
                // eslint-disable-next-line jsx-a11y/no-static-element-interactions
                <div
                    className={this.props.popupClassName}
                    onMouseUp={this.stopEvent}
                    ref={e => { this.popup = e; }}
                    style={this.state.style}
                >
                    <ComposedComponent {...this.props} /><commit_msg>Fixed right position of popup with getBoundingClientRect<commit_after>import { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { closePopup } from 'rrp/actions';
import HigherOrderPopupComponent from 'rrp/higher-order-popup-component';
import { TYPE_POPUP } from 'rrp/popup-collection';
import { popupSelector } from 'rrp/portal';
import { getPopupPosition } from 'rrp/utils';

const PROP_TYPES = {
    anchor: PropTypes.oneOf(['bottom', 'left', 'right', 'top']),
    closePopup: PropTypes.func,
    dispatch: PropTypes.func,
    getRect: PropTypes.func.isRequired,
    id: PropTypes.string.isRequired,
    popupClassName: PropTypes.string,
    refreshPosition: PropTypes.bool,
    offset: PropTypes.number.isRequired
};

export const HOCPopup = ComposedComponent => {
    class Popup extends Component {
        constructor(props) {
            super(props);
            this.state = {};

            this.closePopup = () => {
                if (props.closePopup) {
                    props.closePopup(props.id);
                    return;
                }
                props.dispatch(closePopup(props.id));
            };
            this.refreshPositionHandler = () => this.refreshPosition();
        }

        componentDidMount() {
            this.setPopupPosition();
            window.addEventListener('mouseup', this.closePopup);
            window.addEventListener('resize', this.refreshPositionHandler);
        }

        componentWillReceiveProps(nextProps) {
            if (this.props.refreshPosition !== nextProps.refreshPosition) {
                this.refreshPosition();
            }
        }

        componentWillUnmount() {
            window.removeEventListener('mouseup', this.closePopup);
            window.removeEventListener('resize', this.refreshPositionHandler);
        }

        setPopupPosition() {
            const popupRect = this.popup.getBoundingClientRect();
            const style = getPopupPosition(
                this.props.anchor,
                this.props.getRect(),
                popupRect.width,
                popupRect.height,
                window.innerWidth,
                window.innerHeight,
                this.props.offset);
            this.setState({ style });
        }

        refreshPosition() {
            window.requestAnimationFrame(() => this.setPopupPosition());
        }

        stopEvent(event) {
            event.stopPropagation();
        }

        render() {
            return (
                // eslint-disable-next-line jsx-a11y/no-static-element-interactions
                <div
                    className={this.props.popupClassName}
                    onMouseUp={this.stopEvent}
                    ref={e => { this.popup = e; }}
                    style={this.state.style}
                >
                    <ComposedComponent {...this.props} />
{'fraction_non_alphanumeric': 0.11124673060156931, 'fraction_numerical': 0.0006974716652136007, 'mean_word_length': 1.8551518168242906, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7506022', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1445, 'n_words': 247}