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