<commit_before>/*
Copyright 2017 Vector Creations Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React from 'react';
import FocusTrap from 'focus-trap-react';
import PropTypes from 'prop-types';

import { KeyCode } from '../../../Keyboard';
import AccessibleButton from '../elements/AccessibleButton';
import sdk from '../../../index';

/**
 * Basic container for modal dialogs.
 *
 * Includes a div for the title, and a keypress handler which cancels the
 * dialog on escape.
 */
export default React.createClass({
    displayName: 'BaseDialog',

    propTypes: {
        // onFinished callback to call when Escape is pressed
        onFinished: PropTypes.func.isRequired,

        // called when a key is pressed
        onKeyDown: PropTypes.func,

        // CSS class to apply to dialog div
        className: PropTypes.string,

        // Title for the dialog.
        // (could probably actually be something more complicated than a string if desired)
        title: PropTypes.string.isRequired,

        // children should be the content of the dialog
        children: PropTypes.node,

        // Id of content element
        // If provided, this is used to add a aria-describedby attribute
        contentId: React.PropTypes.string,
    },

    _onKeyDown: function(e) {
        if (this.props.onKeyDown) {
            this.props.onKeyDown(e);
        }
        if (e.keyCode === KeyCode.ESCAPE) {
            e.stopPropagation();
            e.preventDefault();
            this.props.onFinished();
        }
    },

    _onCancelClick: function(e) {
        this.props.onFinished();
    },

    render: function() {
        const TintableSvg = sdk.getComponent("elements.TintableSvg");

        return (
            <FocusTrap onKeyDown={this._onKeyDown}
                className={this.props.className}
                role="dialog"
                aria-labelledby='mx_BaseDialog_title'
                aria-describedby={this.props.contentId}
            >
                <AccessibleButton onClick={this._onCancelClick}
                    className="mx_Dialog_cancelButton"
                >
                    <TintableSvg src="img/icons-close-button.svg" width="35" height="35" />
                </AccessibleButton>
                <div className={'mx_Dialog_title ' + this.props.titleClass} id='mx_BaseDialog_title'>
                    { this.props.title }
                </div>
                { this.props.children }
            </FocusTrap>
        );
    },
});<commit_msg>Add comments explaining our non standard usage of aria-described-by<commit_after>/*
Copyright 2017 Vector Creations Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React from 'react';
import FocusTrap from 'focus-trap-react';
import PropTypes from 'prop-types';

import { KeyCode } from '../../../Keyboard';
import AccessibleButton from '../elements/AccessibleButton';
import sdk from '../../../index';

/**
 * Basic container for modal dialogs.
 *
 * Includes a div for the title, and a keypress handler which cancels the
 * dialog on escape.
 */
export default React.createClass({
    displayName: 'BaseDialog',

    propTypes: {
        // onFinished callback to call when Escape is pressed
        onFinished: PropTypes.func.isRequired,

        // called when a key is pressed
        onKeyDown: PropTypes.func,

        // CSS class to apply to dialog div
        className: PropTypes.string,

        // Title for the dialog.
        // (could probably actually be something more complicated than a string if desired)
        title: PropTypes.string.isRequired,

        // children should be the content of the dialog
        children: PropTypes.node,

        // Id of content element
        // If provided, this is used to add a aria-describedby attribute
        contentId: React.PropTypes.string,
    },

    _onKeyDown: function(e) {
        if (this.props.onKeyDown) {
            this.props.onKeyDown(e);
        }
        if (e.keyCode === KeyCode.ESCAPE) {
            e.stopPropagation();
            e.preventDefault();
            this.props.onFinished();
        }
    },

    _onCancelClick: function(e) {
        this.props.onFinished();
    },

    render: function() {
        const TintableSvg = sdk.getComponent("elements.TintableSvg");

        return (
            <FocusTrap onKeyDown={this._onKeyDown}
                className={this.props.className}
                role="dialog"
                aria-labelledby='mx_BaseDialog_title'
                // This should point to a node describing the dialog.
                // If we were about to completelly follow this recommendation we'd need to
                // make all the components relying on BaseDialog to be aware of it.
                // So instead we will use the whole content as the description.
                // Description comes first and if the content contains more text,
                // AT users can skip its presentation.
                aria-describedby={this.props.contentId}
            >
                <AccessibleButton onClick={this._onCancelClick}
                    className="mx_Dialog_cancelButton"
                >
                    <TintableSvg src="img/icons-close-button.svg" width="35" height="35" />
                </AccessibleButton>
                <div className={'mx_Dialog_title ' + this.props.titleClass} id='mx_BaseDialog_title'>
                    { this.props.title }
                </div>
                { this.props.children }
            </FocusTrap>
        );
    },
});
{'fraction_non_alphanumeric': 0.09230769230769231, 'fraction_numerical': 0.0036923076923076922, 'mean_word_length': 2.14818401937046, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3619711', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1654, 'n_words': 564}