UNPKG

wix-style-react

Version:
364 lines (363 loc) • 13.9 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _reactModal = _interopRequireDefault(require("react-modal")); var _defaultTo = _interopRequireDefault(require("lodash/defaultTo")); var _ModalSt = require("./Modal.st.css"); var _constants = require("./constants"); var _ZIndex = require("../ZIndex"); var _uniqueId = _interopRequireDefault(require("lodash/uniqueId")); var _context = require("../WixStyleReactProvider/context"); var _WixStyleReactProvider = _interopRequireDefault(require("../WixStyleReactProvider")); var _ariaLabels = require("../common/accessibility/ariaLabels"); var _CloseButton = _interopRequireDefault(require("../CloseButton")); var _IconButton = _interopRequireDefault(require("../IconButton")); var _wixUiIconsCommon = require("@wix/wix-ui-icons-common"); var _Tooltip = _interopRequireDefault(require("../Tooltip")); var _jsxFileName = "/home/builduser/work/a9c1ac8876d5057c/packages/wix-style-react/dist/cjs/Modal/Modal.js"; class Modal extends _react.default.PureComponent { constructor(props) { super(props); this.handleOverlayClick = event => { var { shouldCloseOnOverlayClick, onRequestClose } = this.props; if (shouldCloseOnOverlayClick && event.target.id === this.CHILDREN_WRAPPER_DIV_ID && onRequestClose) { onRequestClose(); } }; this.renderCloseButton = () => { return /*#__PURE__*/_react.default.createElement(_CloseButton.default, { dataHook: _constants.dataHooks.modalCloseButton, className: _ModalSt.classes.closeButton, onClick: this.props.onRequestClose, size: "large", skin: "light", "aria-label": _constants.labels.close, __self: this, __source: { fileName: _jsxFileName, lineNumber: 117, columnNumber: 7 } }); }; this.renderNavigationControls = () => { var DIRECTIONS = { prev: 'prev', next: 'next' }; var TOOLTIP_GAP = 9; var { showNavigationPreviousButton, showNavigationNextButton, onNavigationClickPrevious, onNavigationClickNext, navigationPreviousLabel, navigationNextLabel } = this.props; var buildButton = (dir, className) => { var isPrevious = dir === DIRECTIONS.prev; return /*#__PURE__*/_react.default.createElement(_IconButton.default, { className: className, skin: "inverted", size: "large", onClick: isPrevious ? onNavigationClickPrevious : onNavigationClickNext, dataHook: isPrevious ? _constants.dataHooks.navigationPrevButton : _constants.dataHooks.navigationNextButton, __self: this, __source: { fileName: _jsxFileName, lineNumber: 142, columnNumber: 9 } }, isPrevious ? /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.ArrowLeft, { __self: this, __source: { fileName: _jsxFileName, lineNumber: 155, columnNumber: 25 } }) : /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.ArrowRight, { __self: this, __source: { fileName: _jsxFileName, lineNumber: 155, columnNumber: 41 } })); }; return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, { __self: this, __source: { fileName: _jsxFileName, lineNumber: 160, columnNumber: 7 } }, showNavigationPreviousButton && (navigationPreviousLabel ? /*#__PURE__*/_react.default.createElement(_Tooltip.default, { className: _ModalSt.classes.navPrev, content: navigationPreviousLabel, moveBy: { y: TOOLTIP_GAP }, "aria-describedby": _constants.ariaDescribedBy.navigationPrev, dataHook: _constants.dataHooks.navigationPrevTooltip, __self: this, __source: { fileName: _jsxFileName, lineNumber: 163, columnNumber: 13 } }, buildButton(DIRECTIONS.prev)) : buildButton(DIRECTIONS.prev, _ModalSt.classes.navPrev)), showNavigationNextButton && (navigationNextLabel ? /*#__PURE__*/_react.default.createElement(_Tooltip.default, { className: _ModalSt.classes.navNext, content: navigationNextLabel, moveBy: { y: TOOLTIP_GAP }, "aria-describedby": _constants.ariaDescribedBy.navigationNext, dataHook: _constants.dataHooks.navigationNextTooltip, __self: this, __source: { fileName: _jsxFileName, lineNumber: 177, columnNumber: 13 } }, buildButton(DIRECTIONS.next)) : buildButton(DIRECTIONS.next, _ModalSt.classes.navNext))); }; this.CHILDREN_WRAPPER_DIV_ID = (0, _uniqueId.default)('wsr-modal'); } render() { var { dataHook, horizontalPosition, verticalPosition, height, scrollableContent, borderRadius, zIndex, scrollable, isOpen, shouldCloseOnOverlayClick, shouldDisplayCloseButton, onRequestClose, onAfterClose, onAfterOpen, contentLabel, closeTimeoutMS, children, appElement, overlayPosition, parentSelector, screen, showNavigationPreviousButton, showNavigationNextButton, onNavigationClickPrevious, onNavigationClickNext } = this.props; var { maxHeight } = this.props; var justifyContent = _constants.flexPositions[horizontalPosition]; var alignItems = _constants.flexPositions[verticalPosition]; maxHeight = scrollableContent && maxHeight === 'auto' ? '100vh' : maxHeight; var modalStyles = { overlay: { // Overriding defaults position: overlayPosition, top: 0, left: 0, right: 0, bottom: 0, zIndex: (0, _defaultTo.default)(zIndex, (0, _ZIndex.ZIndex)('Modal')), backgroundColor: null, // null disables the property, use css instead // Overriding defaults - END display: 'flex', justifyContent, alignItems, overflowY: scrollable ? 'auto' : 'hidden' }, content: { // Overriding defaults border: 'none', overflowY: scrollableContent ? 'auto' : 'initial', overflowX: scrollableContent ? 'hidden' : 'initial', height, maxHeight, width: '100%', // stylelint-disable-line WebkitOverflowScrolling: 'touch', outline: 'none', borderRadius, padding: '0px', // Overriding defaults - END backgroundColor: 'transparent', marginBottom: '0px', position: 'relative' } }; if (appElement) { _reactModal.default.setAppElement(appElement); } else { _reactModal.default.setAppElement('body'); } return /*#__PURE__*/_react.default.createElement("div", { "data-hook": dataHook, onKeyDown: e => { var arrows = { left: 'ArrowLeft', right: 'ArrowRight' }; if (e && e.key === arrows.left) { showNavigationPreviousButton && onNavigationClickPrevious && onNavigationClickPrevious(); } if (e && e.key === arrows.right) { showNavigationNextButton && onNavigationClickNext && onNavigationClickNext(); } }, __self: this, __source: { fileName: _jsxFileName, lineNumber: 271, columnNumber: 7 } }, /*#__PURE__*/_react.default.createElement(_context.WixStyleReactContext.Consumer, { __self: this, __source: { fileName: _jsxFileName, lineNumber: 290, columnNumber: 9 } }, _ref => { var { newColorsBranding } = _ref; return /*#__PURE__*/_react.default.createElement(_reactModal.default, { portalClassName: (0, _ModalSt.st)(_ModalSt.classes.root, { scrollable }, "portal portal-".concat(dataHook)), isOpen: isOpen, shouldCloseOnOverlayClick: shouldCloseOnOverlayClick, onRequestClose: onRequestClose, onAfterOpen: onAfterOpen, onAfterClose: onAfterClose, style: modalStyles, className: _ModalSt.classes.modal, closeTimeoutMS: closeTimeoutMS, parentSelector: parentSelector, ariaHideApp: false, contentLabel: contentLabel, aria: { labelledby: _ariaLabels.ariaLabels.labelledBy, describedby: _ariaLabels.ariaLabels.describedBy }, __self: this, __source: { fileName: _jsxFileName, lineNumber: 292, columnNumber: 13 } }, /*#__PURE__*/_react.default.createElement(_WixStyleReactProvider.default, { features: { newColorsBranding }, __self: this, __source: { fileName: _jsxFileName, lineNumber: 314, columnNumber: 15 } }, isOpen && shouldDisplayCloseButton && this.renderCloseButton(), /*#__PURE__*/_react.default.createElement("div", { "data-scrollable": scrollable || null, id: this.CHILDREN_WRAPPER_DIV_ID, className: (0, _ModalSt.st)(_ModalSt.classes.childrenContainer, { screen }), onClick: this.handleOverlayClick, __self: this, __source: { fileName: _jsxFileName, lineNumber: 316, columnNumber: 17 } }, (showNavigationPreviousButton || showNavigationNextButton) && this.renderNavigationControls(), children))); })); } } Modal.propTypes = { /** Applied as data-hook HTML attribute that can be used to create driver in testing */ dataHook: _propTypes.default.string, /** Controls if modal is open or closed */ isOpen: _propTypes.default.bool.isRequired, /** Border radius of modal */ borderRadius: _propTypes.default.number, /** a11y: The value of contentLabel is set as an aria-label on the modal element. This helps assistive technology, like screen readers, to add a label to an element that would otherwise be anonymous */ contentLabel: _propTypes.default.string, /** Renders modal content */ children: _propTypes.default.any, /** Controls z-index of the modal overlay */ zIndex: _propTypes.default.number, /** Enables to close modal when mouse clicked on overlay area */ shouldCloseOnOverlayClick: _propTypes.default.bool, /** Displays a close button on the top right corner of the overlay */ shouldDisplayCloseButton: _propTypes.default.bool, /** Callback that will be executed when the modal is requested to be closed, prior to actually closing */ onRequestClose: _propTypes.default.func, /** Callback that will be executed after the modal has been opened */ onAfterOpen: _propTypes.default.func, /** Callback that will be executed after the modal has been closed */ onAfterClose: _propTypes.default.func, /** Horizontal position of the modal */ horizontalPosition: _propTypes.default.oneOf(['start', 'center', 'end']), /** Vertical position of the modal */ verticalPosition: _propTypes.default.oneOf(['start', 'center', 'end']), /** Number indicating the milliseconds to wait before closing the modal */ closeTimeoutMS: _propTypes.default.number, /** Specifies if modal portal supports scroll */ scrollable: _propTypes.default.bool, /** Specifies if modal content should become scrollable when modal size will fit the window */ scrollableContent: _propTypes.default.bool, /** Sets the maximum height for a scrollable content */ maxHeight: _propTypes.default.string, /** Sets the height for modal's content container */ height: _propTypes.default.string, /** css position of the modal overlay */ overlayPosition: _propTypes.default.oneOf(['static', 'relative', 'absolute', 'fixed', 'sticky']), /** A function that returns a DOM element on which the modal should be appended to */ parentSelector: _propTypes.default.func, /** Selector specifying where to apply the aria-hidden attribute */ appElement: _propTypes.default.string, /** Specifies minimum spacing between full viewport and modal content */ screen: _propTypes.default.oneOf(['full', 'desktop', 'mobile']), /** Enable navigation previous buttons to the side of the content and listen on clicks by using onNavigationClickPrevious */ showNavigationPreviousButton: _propTypes.default.bool, /** Enable navigation next button to the side of the content and listen on clicks by using onNavigationClickNext */ showNavigationNextButton: _propTypes.default.bool, /** Callback that will be executed when the navigation control previous is clicked */ onNavigationClickPrevious: _propTypes.default.func, /** Callback that will be executed when the navigation control next is clicked */ onNavigationClickNext: _propTypes.default.func, /** Label used for navigation control previous button tooltip */ navigationPreviousLabel: _propTypes.default.string, /** Label used for navigation control next button tooltip */ navigationNextLabel: _propTypes.default.string }; Modal.defaultProps = { borderRadius: 0, shouldCloseOnOverlayClick: false, shouldDisplayCloseButton: false, horizontalPosition: 'center', verticalPosition: 'center', closeTimeoutMS: 500, scrollable: true, scrollableContent: false, height: '100%', maxHeight: 'auto', overlayPosition: 'fixed', screen: 'desktop' }; var _default = exports.default = Modal; //# sourceMappingURL=Modal.js.map