import { h } from 'preact'; import PropTypes from 'prop-types'; import { FocusTrap } from '../../shared/components/focusTrap'; import { defaultChildrenPropTypes } from '../../common-prop-types'; import { Button } from '@crayons'; function getAdditionalClassNames({ size, className }) { let additionalClassNames = ''; if (size && size.length > 0 && size !== 'default') { additionalClassNames += ` crayons-modal--${size}`; } if (className && className.length > 0) { additionalClassNames += ` ${className}`; } return additionalClassNames; } const CloseIcon = () => ( Close ); /** * A modal component which can be presented with or without an overlay. * The modal is presented within a focus trap for accessibility purposes - please note that the selector used for the focusTrap must be unique on the given page, otherwise focus may be trapped on the wrong element. * * @param {Object} props * @param {Array} props.children The content to be displayed inside the Modal. Can be provided by composition (see example). * @param {string} props.size The desired modal size ('s', 'm' or 'default') * @param {string} props.className Optional additional classnames to apply to the modal container * @param {string} props.title The title to be displayed in the modal heading. If provided, a title bar with a close button will be displayed. * @param {boolean} props.overlay Whether or not to show a semi-opaque overlay behind the modal * @param {Function} props.onClose Callback for any function to be executed on close button click or Escape * @param {boolean} props.closeOnClickOutside Whether the modal should close if the user clicks outside of it * @param {string} props.focusTrapSelector The CSS selector for where to trap the user's focus. This should be unique to the page in which the modal is presented. * * @example *

Some modal content

*/ export const Modal = ({ children, size, className, title, overlay = true, onClose = () => {}, closeOnClickOutside = false, focusTrapSelector = '.crayons-modal', }) => { return (
{title && (

{title}

)}
{children}
{overlay && (
)}
); }; Modal.displayName = 'Modal'; Modal.defaultProps = { size: 'default', }; Modal.propTypes = { children: defaultChildrenPropTypes.isRequired, className: PropTypes.string, title: PropTypes.string.isRequired, overlay: PropTypes.bool, onClose: PropTypes.func, size: PropTypes.oneOf(['default', 's', 'm']).isRequired, focusTrapSelector: PropTypes.string, };