index.js
172 lines
| 1 | import { useEffect, useCallback } from 'react'; |
| 2 | import PropTypes from 'prop-types'; |
| 3 | import classNames from 'classnames'; |
| 4 | import LoadingOverlay from '@givewp/components/LoadingOverlay'; |
| 5 | |
| 6 | import styles from './styles.module.scss'; |
| 7 | |
| 8 | const { __ } = wp.i18n; |
| 9 | |
| 10 | const Modal = ( { visible, type, children, isLoading, handleClose, ...rest } ) => { |
| 11 | const closeModal = useCallback( ( event ) => { |
| 12 | if ( event.keyCode === 27 && typeof handleClose === 'function' ) { |
| 13 | handleClose(); |
| 14 | } |
| 15 | }, [] ); |
| 16 | |
| 17 | useEffect( () => { |
| 18 | document.addEventListener( 'keydown', closeModal, false ); |
| 19 | |
| 20 | return () => { |
| 21 | document.removeEventListener( 'keydown', closeModal, false ); |
| 22 | }; |
| 23 | }, [] ); |
| 24 | |
| 25 | const handleOverlayClick = ( e ) => { |
| 26 | if ( e.target.classList.contains( styles.overlay ) && typeof handleClose === 'function' ) { |
| 27 | handleClose(); |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | const modalStyles = classNames( { |
| 32 | [ styles.modal ]: true, |
| 33 | [ styles.error ]: type === 'error' || type === 'failed', |
| 34 | [ styles.warning ]: type === 'warning', |
| 35 | [ styles.success ]: type === 'success', |
| 36 | [ styles.http ]: type === 'http', |
| 37 | } ); |
| 38 | |
| 39 | return ( |
| 40 | <div className={ classNames( { [ styles.overlay ]: visible } ) } onClick={ handleOverlayClick } { ...rest }> |
| 41 | <div className={ styles.container }> |
| 42 | <div className={ modalStyles }> |
| 43 | { isLoading && ( |
| 44 | <LoadingOverlay spinnerSize="small" /> |
| 45 | ) } |
| 46 | <div className={ styles.content }> |
| 47 | { children } |
| 48 | </div> |
| 49 | </div> |
| 50 | </div> |
| 51 | </div> |
| 52 | ); |
| 53 | }; |
| 54 | |
| 55 | Modal.Title = ( { children, ...rest } ) => { |
| 56 | return ( |
| 57 | <div className={ styles.title } { ...rest }> |
| 58 | { children } |
| 59 | </div> |
| 60 | ); |
| 61 | }; |
| 62 | |
| 63 | Modal.CloseIcon = ( { onClick, ...rest } ) => { |
| 64 | return ( |
| 65 | <div className={ styles.closeIconContainer }> |
| 66 | <div className={ styles.close } onClick={ onClick } { ...rest }> |
| 67 | <span className="dashicons dashicons-no" /> |
| 68 | </div> |
| 69 | </div> |
| 70 | ); |
| 71 | }; |
| 72 | |
| 73 | Modal.Section = ( { title, content, ...rest } ) => { |
| 74 | return ( |
| 75 | <div className={ styles.section } { ...rest }> |
| 76 | <strong>{ title }:</strong> |
| 77 | { content } |
| 78 | </div> |
| 79 | ); |
| 80 | }; |
| 81 | |
| 82 | Modal.Content = ( { children, align, ...rest } ) => { |
| 83 | const contentClasses = classNames( { |
| 84 | [ styles.innerContent ]: true, |
| 85 | [ styles.textCenter ]: align === 'center', |
| 86 | [ styles.textRight ]: align === 'right', |
| 87 | [ styles.textLeft ]: ! align || align === 'left', |
| 88 | } ); |
| 89 | |
| 90 | return ( |
| 91 | <div className={ contentClasses } { ...rest }> |
| 92 | { children } |
| 93 | </div> |
| 94 | ); |
| 95 | }; |
| 96 | |
| 97 | Modal.AdditionalContext = ( { type, context, ...rest } ) => { |
| 98 | const title = ( [ 'error', 'failed' ].includes( type ) ) ? __( 'Error details', 'give' ) : __( 'Additional context', 'give' ); |
| 99 | |
| 100 | return ( |
| 101 | <div className={ styles.section } { ...rest }> |
| 102 | <strong>{ title }:</strong> |
| 103 | <div className={ styles.errorDetailsContainer }> |
| 104 | <pre> |
| 105 | { ( context instanceof Object ) ? ( |
| 106 | Object.entries( context ).map( ( [ key, value ] ) => { |
| 107 | return ( |
| 108 | <div key={ key }> |
| 109 | <span>{ key }:</span> |
| 110 | { value } |
| 111 | </div> |
| 112 | ); |
| 113 | } ) |
| 114 | ) : context } |
| 115 | </pre> |
| 116 | </div> |
| 117 | </div> |
| 118 | ); |
| 119 | }; |
| 120 | |
| 121 | Modal.propTypes = { |
| 122 | // Is visible |
| 123 | visible: PropTypes.bool.isRequired, |
| 124 | // Is loading |
| 125 | isLoading: PropTypes.bool, |
| 126 | // Modal type |
| 127 | type: PropTypes.string, |
| 128 | // Collection of react DOM elements |
| 129 | children: PropTypes.object, |
| 130 | // Handle close callback |
| 131 | handleClose: PropTypes.func, |
| 132 | }; |
| 133 | |
| 134 | Modal.Title.propTypes = { |
| 135 | // Collection of react DOM elements |
| 136 | children: PropTypes.object, |
| 137 | }; |
| 138 | |
| 139 | Modal.CloseIcon.propTypes = { |
| 140 | // On click callback |
| 141 | onClick: PropTypes.func.isRequired, |
| 142 | }; |
| 143 | |
| 144 | Modal.Section.propTypes = { |
| 145 | // Section title |
| 146 | title: PropTypes.string.isRequired, |
| 147 | // Section content |
| 148 | content: PropTypes.string.isRequired, |
| 149 | }; |
| 150 | |
| 151 | Modal.Content.propTypes = { |
| 152 | // Collection of react DOM elements |
| 153 | children: PropTypes.object, |
| 154 | }; |
| 155 | |
| 156 | Modal.AdditionalContext.propTypes = { |
| 157 | // Log type |
| 158 | type: PropTypes.string.isRequired, |
| 159 | // String or Array of objects |
| 160 | context: PropTypes.any.isRequired, |
| 161 | }; |
| 162 | |
| 163 | Modal.defaultProps = { |
| 164 | visible: true, |
| 165 | isLoading: false, |
| 166 | type: 'notice', |
| 167 | children: {}, |
| 168 | handleClose: () => {}, |
| 169 | }; |
| 170 | |
| 171 | export default Modal; |
| 172 |