<commit_before>import React from 'react';

import focusTrap from 'focus-trap';
import tabbable from 'tabbable';

import { whichAnimationEvent } from './utils';


const bodyActiveClass = 'u-body-modal-active';
const animationEvent = whichAnimationEvent();

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

const Modal = React.createClass({

    propTypes: {
        isOpen: React.PropTypes.bool.isRequired,
        onRequestClose: React.PropTypes.func,
        onAfterClose: React.PropTypes.func,
        overlayClick: React.PropTypes.bool,
        label: React.PropTypes.string,
        className: React.PropTypes.string,
        controls: React.PropTypes.object,
        children: React.PropTypes.object,
        ariaHideApp: React.PropTypes.bool,
    },

    getDefaultProps() {
        return {
            isOpen: false,
            ariaHideApp: true,
            onRequestClose: null,
            onAfterClose: () => {},
            overlayClick: true,
            className: '',
            label: '',
            controls: null,
        };
    },

    getInitialState: () => {
        return {
            afterOpen: false,
            beforeClose: false,
        };
    },

    componentDidMount() {
        // Focus needs to be set when mounting and already open
        if (this.props.isOpen) {
            this.open();
            window.addEventListener('keydown', this.handleKeyDown);
        }
    },

    componentWillUnmount() {
        window.removeEventListener('keydown', this.handleKeyDown);
    },

    componentWillReceiveProps(newProps) {
        // Focus only needs to be set once when the modal is being opened
        if (!this.props.isOpen && newProps.isOpen) {
            this.open();
        } else if (this.props.isOpen && !newProps.isOpen) {
            this.handleClose();
        }
    },

    open() {
        document.body.classList.add(bodyActiveClass);
        this.setAriaHidden(false);
        this.setFocusTrap();

        this.setState({afterOpen: true});
    },

    requestClose() {
        const { onRequestClose } = this.props;

        if (onRequestClose) {
            onRequestClose();
        }
    },

    handleClose() {
        const { onRequestClose } = this.props;

        document.body.classList.remove(bodyActiveClass);

        if (!onRequestClose) {
            return;
        }

        if (animationEvent) {
            const modal = this.refs.modal;
            modal.addEventListener(animationEvent, this.close );
            modal.classList.remove('modal--active');
            modal.classList.add('modal--exit');
            this.setState({
                beforeClose: true,
            });
            return;
        }

        this.close();
    },

    close(e) {
        const { modal } = this.refs;

        // make sure we're listening to the modals animationEvent
        const target = e.target || e.srcElement;
        if (e && target !== modal) {
            return;
        }

        this.setAriaHidden(true);

        if (animationEvent) {
            modal.removeEventListener(animationEvent, this.close);
        }

        this.setState({
            afterOpen: false,
            beforeClose: false,
        }, this.afterClose);
    },

    afterClose() {
        focusTrap.deactivate(this.refs.modal);
        this.props.onAfterClose();
    },

    handleKeyDown(e) {
        // ESC key
        if (e.keyCode === 27) {
            this.requestClose();
        }
    },

    handleOverlayClick() {
        const { overlayClick } = this.props;
        if (!overlayClick) {
            return;
        }

        this.requestClose();
    },

    shouldBeClosed() {
        return !this.props.isOpen && !this.state.beforeClose;
    },

    setAriaHidden(isHidden) {
        const { ariaHideApp } = this.props;
        const { content } = this.refs;

        if (!ariaHideApp) {
            return;
        }

        if (!content) {
            return;
        }

        console.log("content", content);
<commit_msg>fixed esc key<commit_after>import React from 'react';

import focusTrap from 'focus-trap';
import tabbable from 'tabbable';

import { whichAnimationEvent } from './utils';


const bodyActiveClass = 'u-body-modal-active';
const animationEvent = whichAnimationEvent();

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

const Modal = React.createClass({

    propTypes: {
        isOpen: React.PropTypes.bool.isRequired,
        onRequestClose: React.PropTypes.func,
        onAfterClose: React.PropTypes.func,
        overlayClick: React.PropTypes.bool,
        label: React.PropTypes.string,
        className: React.PropTypes.string,
        controls: React.PropTypes.object,
        children: React.PropTypes.object,
        ariaHideApp: React.PropTypes.bool,
    },

    getDefaultProps() {
        return {
            isOpen: false,
            ariaHideApp: true,
            onRequestClose: null,
            onAfterClose: () => {},
            overlayClick: true,
            className: '',
            label: '',
            controls: null,
        };
    },

    getInitialState: () => {
        return {
            afterOpen: false,
            beforeClose: false,
        };
    },

    componentDidMount() {
        // Focus needs to be set when mounting and already open
        if (this.props.isOpen) {
            this.open();
        }
    },

    componentWillUnmount() {
    },

    componentWillReceiveProps(newProps) {
        // Focus only needs to be set once when the modal is being opened
        if (!this.props.isOpen && newProps.isOpen) {
            this.open();
        } else if (this.props.isOpen && !newProps.isOpen) {
            this.handleClose();
        }
    },

    open() {
        document.body.classList.add(bodyActiveClass);
        this.setAriaHidden(false);
        this.setFocusTrap();

        this.setState({afterOpen: true}, function() {
            window.addEventListener('keydown', this.handleKeyDown);
        });
    },

    requestClose() {
        const { onRequestClose } = this.props;

        if (onRequestClose) {
            onRequestClose();
        }
    },

    handleClose() {
        const { onRequestClose } = this.props;

        document.body.classList.remove(bodyActiveClass);

        if (!onRequestClose) {
            return;
        }

        if (animationEvent) {
            const modal = this.refs.modal;
            modal.addEventListener(animationEvent, this.close );
            modal.classList.remove('modal--active');
            modal.classList.add('modal--exit');
            this.setState({
                beforeClose: true
            });
            return;
        }

        this.close();
    },

    close(e) {
        const { modal } = this.refs;

        // make sure we're listening to the modals animationEvent
        const target = e.target || e.srcElement;
        if (e && target !== modal) {
            return;
        }

        this.setAriaHidden(true);

        if (animationEvent) {
            modal.removeEventListener(animationEvent, this.close);
        }

        this.setState({
            afterOpen: false,
            beforeClose: false,
        }, this.afterClose);
    },

    afterClose() {
        focusTrap.deactivate(this.refs.modal);
        window.removeEventListener('keydown', this.handleKeyDown);

        this.props.onAfterClose();
    },

    handleKeyDown(e) {
        // ESC key
        if (e.keyCode === 27) {
            this.requestClose();
        }
    },

    handleOverlayClick() {
        const { overlayClick } = this.props;
        if (!overlayClick) {
            return;
        }

        this.requestClose();
    },

    shouldBeClosed() {
        return !this.props.isOpen && !this.state.beforeClose;
    },

    setAriaHidden(isHidden) {
        const { ariaHideApp } = this.props;
        const { content } = this.refs;

        if (!ariaHideApp) {
            return;
        }

        if (!content) {
            return;
        }

{'fraction_non_alphanumeric': 0.11741951584726729, 'fraction_numerical': 0.0004991265285749938, 'mean_word_length': 1.7476859787452863, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2451085', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2185, 'n_words': 429}