PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Views / Components / AdminUI / ModalDialog / index.tsx

index.tsx in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Views/Components/AdminUI/ModalDialog/index.tsx

100 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {MouseEventHandler, useCallback, useEffect} from 'react';
2 import {createPortal} from 'react-dom';
3 import {__} from '@wordpress/i18n';
4 import {ExitIcon} from '@givewp/components/AdminUI/Icons';
5 import './style.scss';
6
7 export interface ModalProps {
8 children: JSX.Element | JSX.Element[];
9 title: string;
10 isOpen?: boolean;
11 icon?: JSX.Element | JSX.Element[];
12 insertInto?: string;
13 handleClose?: MouseEventHandler;
14 showHeader?: boolean;
15 showCloseIcon?: boolean;
16 wrapperClassName?: string;
17 }
18
19 export default function Modal({
20 title,
21 icon,
22 children,
23 insertInto,
24 handleClose,
25 isOpen = true,
26 showHeader = true,
27 showCloseIcon = true,
28 wrapperClassName = '',
29 }: ModalProps) {
30 // ESC key closes modal
31 const closeModal = useCallback((e) => {
32 if (e.keyCode === 27 && typeof handleClose === 'function') {
33 handleClose(e);
34 }
35 }, []);
36
37 useEffect(() => {
38 document.addEventListener('keydown', closeModal, false);
39
40 return () => {
41 document.removeEventListener('keydown', closeModal, false);
42 };
43 }, []);
44
45 useEffect(() => {
46 if (isOpen) {
47 document.body.classList.add('modalDialog-open');
48 } else {
49 document.body.classList.remove('modalDialog-open');
50 }
51
52 return () => {
53 document.body.classList.remove('modalDialog-open');
54 };
55 }, [isOpen]);
56
57 if (!isOpen) return null;
58
59 return createPortal(
60 <div className={`givewp-modal-wrapper ${wrapperClassName}`}>
61 <div role="dialog" aria-label={title} className="givewp-modal-dialog">
62 <div className={`givewp-modal-dialog-content`}>
63 {showHeader ? (
64 <div className="givewp-modal-header">
65 {icon && <div className="givewp-modal-icon-header">{icon}</div>}
66 {title}
67 {showCloseIcon && handleClose && (
68 <button
69 aria-label={__('Close dialog', 'give')}
70 className="givewp-modal-close"
71 onClick={handleClose}
72 >
73 <ExitIcon aria-label={__('Close dialog icon', 'give')} />
74 </button>
75 )}
76 </div>
77 ) : (
78 <>
79 {showCloseIcon && handleClose && (
80 <button
81 aria-label={__('Close dialog', 'give')}
82 className="givewp-modal-close-headless"
83 onClick={handleClose}
84 >
85 <ExitIcon aria-label={__('Close dialog icon', 'give')} />
86 </button>
87 )}
88 {icon && <div className="givewp-modal-icon-center">{icon}</div>}
89 </>
90 )}
91
92 <div className="givewp-modal-content">{children}</div>
93 </div>
94 </div>
95 </div>,
96 insertInto ? document.querySelector(insertInto) : document.body
97 );
98 }
99
100