| 1 |
import { Button } from '@wordpress/components' |
| 2 |
import { Fragment, useRef, forwardRef } from '@wordpress/element' |
| 3 |
import { __ } from '@wordpress/i18n' |
| 4 |
import { Icon, close } from '@wordpress/icons' |
| 5 |
import { Dialog, Transition } from '@headlessui/react' |
| 6 |
import { useGlobalStore } from '@library/state/GlobalState' |
| 7 |
|
| 8 |
export const Modal = forwardRef( |
| 9 |
({ isOpen, heading, onClose, children }, initialFocus) => { |
| 10 |
const focusBackup = useRef(null) |
| 11 |
const defaultClose = useGlobalStore((state) => state.removeAllModals) |
| 12 |
onClose = onClose ?? defaultClose |
| 13 |
|
| 14 |
return ( |
| 15 |
<Transition |
| 16 |
appear |
| 17 |
show={isOpen} |
| 18 |
as={Fragment} |
| 19 |
className="extendify"> |
| 20 |
<Dialog |
| 21 |
initialFocus={initialFocus ?? focusBackup} |
| 22 |
onClose={onClose}> |
| 23 |
<div className="fixed inset-0 z-high flex"> |
| 24 |
<Transition.Child |
| 25 |
as={Fragment} |
| 26 |
enter="ease-out duration-200 transition" |
| 27 |
enterFrom="opacity-0" |
| 28 |
enterTo="opacity-100"> |
| 29 |
<Dialog.Overlay className="fixed inset-0 bg-black bg-opacity-40" /> |
| 30 |
</Transition.Child> |
| 31 |
<Transition.Child |
| 32 |
as={Fragment} |
| 33 |
enter="ease-out duration-300 translate transform" |
| 34 |
enterFrom="opacity-0 translate-y-4 sm:translate-y-5" |
| 35 |
enterTo="opacity-100 translate-y-0"> |
| 36 |
<div className="relative m-auto w-full"> |
| 37 |
<div className="relative m-auto w-full max-w-lg items-center justify-center rounded-sm bg-white shadow-modal"> |
| 38 |
{heading ? ( |
| 39 |
<div className="flex items-center justify-between border-b py-2 pl-6 pr-3 leading-none"> |
| 40 |
<span className="whitespace-nowrap text-base text-extendify-black"> |
| 41 |
{heading} |
| 42 |
</span> |
| 43 |
<CloseButton onClick={onClose} /> |
| 44 |
</div> |
| 45 |
) : ( |
| 46 |
<div className="absolute top-0 right-0 block px-4 py-4 "> |
| 47 |
<CloseButton |
| 48 |
ref={focusBackup} |
| 49 |
onClick={onClose} |
| 50 |
/> |
| 51 |
</div> |
| 52 |
)} |
| 53 |
<div>{children}</div> |
| 54 |
</div> |
| 55 |
</div> |
| 56 |
</Transition.Child> |
| 57 |
</div> |
| 58 |
</Dialog> |
| 59 |
</Transition> |
| 60 |
) |
| 61 |
}, |
| 62 |
) |
| 63 |
|
| 64 |
const CloseButton = forwardRef((props, focusRef) => { |
| 65 |
return ( |
| 66 |
<Button |
| 67 |
{...props} |
| 68 |
icon={<Icon icon={close} />} |
| 69 |
ref={focusRef} |
| 70 |
className="text-extendify-black opacity-75 hover:opacity-100" |
| 71 |
showTooltip={false} |
| 72 |
label={__('Close dialog', 'extendify')} |
| 73 |
/> |
| 74 |
) |
| 75 |
}) |
| 76 |
|