Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | 1x 5x 5x 5x 5x 5x 5x 5x 2x 2x 1x 1x 1x 2x 5x 5x 5x 3x 3x 3x 3x 5x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x | import {Children, cloneElement, forwardRef, useCallback, useEffect, useRef, useState} from 'react' import {createPortal} from 'react-dom' import cx from 'classnames' import PropTypes from 'prop-types' import useMergeRefs from '@s-ui/react-hooks/lib/useMergeRefs' import {Close} from './Close/index.js' import MoleculeModalContent from './Content/index.js' import {HeaderRender} from './HeaderRender/index.js' import {MODAL_SIZES, SUPPORTED_KEYS, toggleWindowScroll} from './config.js' import {suitClass} from './helpers.js' const MoleculeModal = forwardRef( ( { children, closeOnEscKeyDown = false, closeOnOutsideClick = false, isPageScrollable = true, enableContentScroll = false, fitContent = false, floatingIconClose = false, size, header, iconClose = false, isClosing, isOpen = false, onAnimationEnd, onClose = () => {}, portalContainerId = 'modal-react-portal', usePortal = true, withoutIndentation = false, isContentless, withAnimation = true, isOverflowVisible = false }, forwardedRef ) => { const wrapperRef = useRef() const ref = useMergeRefs(wrapperRef, forwardedRef) const blockScrollPage = () => (document.body.style.overflow = 'hidden') const enableScrollPage = () => (document.body.style.overflow = 'auto') const [isClientReady, setIsClientReady] = useState(false) const shoudlCloseOnOutsideClick = useRef() const getContainer = () => { let containerDOMEl = document.getElementById(portalContainerId) if (!containerDOMEl) { containerDOMEl = document.createElement('div') containerDOMEl.id = portalContainerId document.body.appendChild(containerDOMEl) } return containerDOMEl } const closeModal = useCallback( ev => { ev && ev.stopPropagation() toggleWindowScroll(false) if (!isPageScrollable) enableScrollPage() onClose() }, [isPageScrollable, onClose] ) const onKeyDown = useCallback( ev => { if (isOpen === false || closeOnEscKeyDown === false) return if (SUPPORTED_KEYS.includes(ev.key)) { closeModal(ev) ev.preventDefault() } }, [isOpen, closeOnEscKeyDown, closeModal] ) useEffect(() => { shoudlCloseOnOutsideClick.current = false Iif (isOpen && !isPageScrollable) blockScrollPage() else Iif (!isOpen && !isPageScrollable) enableScrollPage() Iif (!isPageScrollable) return () => enableScrollPage() }, [isOpen, isPageScrollable]) useEffect(() => { Eif (usePortal) setIsClientReady(true) }, [usePortal]) useEffect(() => { document.removeEventListener('keydown', onKeyDown) document.addEventListener('keydown', onKeyDown) return () => { toggleWindowScroll(false) document.removeEventListener('keydown', onKeyDown) } }, [onKeyDown]) const handleOutsideMouseDown = ev => { if (closeOnOutsideClick && ev.target === wrapperRef.current) { shoudlCloseOnOutsideClick.current = true } else { shoudlCloseOnOutsideClick.current = false } } const handleOutsideMouseUp = ev => { if (closeOnOutsideClick && shoudlCloseOnOutsideClick.current && ev.target === wrapperRef.current) { closeModal(ev) } } const renderChildren = () => Children.toArray(children).map(child => cloneElement(child, { onClose: closeModal }) ) const renderModal = () => { const wrapperClassName = cx(suitClass(), { 'is-static': !withAnimation, 'is-MoleculeModal-open': isOpen, [suitClass({element: 'out'})]: isClosing && !isOpen }) const dialogClassName = cx(suitClass({element: 'dialog'}), { [suitClass({element: 'dialog--out'})]: isClosing && !isOpen, [suitClass({element: 'dialog--fit'})]: fitContent, [suitClass({element: `dialog--size-${size}`})]: !!size, [suitClass({element: 'dialog--visible-overflow'})]: isOverflowVisible }) return ( <div className={wrapperClassName} ref={ref} onAnimationEnd={onAnimationEnd} onMouseDown={handleOutsideMouseDown} onMouseUp={handleOutsideMouseUp} draggable="false" > <div className={dialogClassName}> {(iconClose || header) && ( <HeaderRender close={iconClose && <Close icon={iconClose} onClick={closeModal} floating={floatingIconClose} />} header={header} floatingIconClose={floatingIconClose} /> )} {isContentless ? ( renderChildren() ) : ( <MoleculeModalContent enableContentScroll={enableContentScroll} withoutIndentation={withoutIndentation} isOverflowVisible={isOverflowVisible} > {renderChildren()} </MoleculeModalContent> )} </div> </div> ) } const modalElement = renderModal() Eif (usePortal) { return isClientReady ? createPortal(modalElement, getContainer()) : null } // temporary fix to avoid executing this on SSR // we should move to functions this and create as a function if (isClientReady) { toggleWindowScroll(isOpen) } return modalElement } ) MoleculeModal.propTypes = { /** * true if you want close the modal by clicking outside the modal itself, otherwise, false */ closeOnOutsideClick: PropTypes.bool, /** * true if you want to let the ESC key to close the modal, otherwise, false */ closeOnEscKeyDown: PropTypes.bool, /** * The content of the modal itself */ children: PropTypes.node, /** * false to prevent scroll body */ isPageScrollable: PropTypes.node, /** * true to prevent scroll in content */ enableContentScroll: PropTypes.bool, /** * true if you want a modal that fit contents in mobile devices, otherwise, false */ fitContent: PropTypes.bool, /** * Max width of the modal to be used */ size: PropTypes.oneOf(Object.values(MODAL_SIZES)), /** * true if you want a modal without content indentation */ withoutIndentation: PropTypes.bool, /** * content of the modal's header */ header: PropTypes.node, /** * customitzable close icon */ iconClose: PropTypes.node, /** * If is true, iconClose will be floating over layers */ floatingIconClose: PropTypes.bool, /** * prop to mark if the modal is currently open or not */ isOpen: PropTypes.bool, /** * onClose function handler */ onClose: PropTypes.func, /** * A boolean to identify if the modal is being closed or not used by the HoC WithAnimation */ isClosing: PropTypes.bool, /** * The callback function to be called when the animation ends, defined by the HoC WithAnimation */ onAnimationEnd: PropTypes.func, /** * Defines whether children will be wrapped with content or not */ isContentless: PropTypes.bool, /** * Container id element to be used to render the portal. If not available, it will be created for you. */ portalContainerId: PropTypes.string, /** * Determines if modal will be rendered using a React Portal. */ usePortal: PropTypes.bool, /** * Define the modal hash, for url update / read */ hash: PropTypes.string, // eslint-disable-line react/no-unused-prop-types /** * The function that manages when the modal open. It'll be executed for open * MoleculeModalWithURLState on pop state changes */ openModalTrigger: PropTypes.func, // eslint-disable-line react/no-unused-prop-types /** * Determines if modal has open/close animation */ withAnimation: PropTypes.bool, /** * Determines if the modal overflow is visible or not */ isOverflowVisible: PropTypes.bool } MoleculeModal.displayName = 'MoleculeModal' export default MoleculeModal |