ndla-ui
Version:
UI component library for NDLA.
98 lines (86 loc) • 2.84 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
/**
* Copyright (c) 2017-present, NDLA.
*
* This source code is licensed under the GPLv3 license found in the
* LICENSE file in the root directory of this source tree.
*
*/
// N.B These component is used to render static markup serverside
// Any interactivty is added by scripts located in the ndla-article-scripts package
import React from 'react';
import PropTypes from 'prop-types';
import BEMHelper from 'react-bem-helper';
import { createUniversalPortal } from '../utils/createUniversalPortal';
var classes = new BEMHelper({
name: 'dialog',
prefix: 'c-'
});
var Dialog = function Dialog(_ref) {
var children = _ref.children,
messages = _ref.messages,
id = _ref.id,
labelledby = _ref.labelledby,
label = _ref.label,
modifier = _ref.modifier,
disablePortal = _ref.disablePortal,
hidden = _ref.hidden,
onClose = _ref.onClose,
rest = _objectWithoutProperties(_ref, ['children', 'messages', 'id', 'labelledby', 'label', 'modifier', 'disablePortal', 'hidden', 'onClose']);
var content = React.createElement(
'div',
_extends({}, classes('', modifier), {
'data-dialog-id': id,
role: 'dialog',
'aria-hidden': hidden,
'aria-labelledby': labelledby,
'aria-label': label
}, rest),
React.createElement(
'div',
classes('content'),
React.createElement(
'button',
_extends({}, classes('close'), {
type: 'button',
onClick: function onClick() {
if (onClose) {
onClose();
}
} }),
messages.close
),
children
),
React.createElement('div', { className: 'o-backdrop' })
);
if (disablePortal) {
return content;
}
return createUniversalPortal(content, 'body');
};
export { Dialog };
Dialog.propTypes = {
id: PropTypes.string.isRequired,
labelledby: PropTypes.string,
label: PropTypes.string,
hidden: PropTypes.bool,
children: PropTypes.node,
messages: PropTypes.shape({
close: PropTypes.string.isRequired
}),
modifier: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
disablePortal: PropTypes.bool,
onClose: PropTypes.func
};
Dialog.defaultProps = {
disablePortal: false,
labelledby: null,
label: null,
hidden: true,
onClose: null,
messages: {
close: 'Lukk'
}
};