/* eslint-env browser */ /* eslint jsx-a11y/click-events-have-key-events: 0 */ /* eslint jsx-a11y/no-noninteractive-element-interactions: 0 */ /* eslint jsx-a11y/no-static-element-interactions: 0 */ import React, { FC, useEffect } from 'react'; import ReactDOM from 'react-dom'; import AUButtonComponent from './AUButtonComponent'; import { AUDialogModalContext } from '../lib/context'; type Props = { lang?: string; domId: string; className?: string; children?: React.JSX.Element; closeButton?: boolean; closeButtonDisabled?: boolean; show: boolean; onClose?: () => void; }; const AUDialogModalComponent: FC = ({ lang = 'da', domId = 'modal-view1', className = 'modal-view', children, closeButton = true, closeButtonDisabled, show, onClose, }) => { useEffect(() => { const dialog = document.getElementById(domId) as HTMLDialogElement; if (dialog) { if (show) { dialog.showModal(); } else { dialog.close(); } } // Handler for click outside dialog function handleClickOutside(event: MouseEvent) { if (dialog && show && event.target === dialog) { if (typeof onClose === 'function') { dialog.close(); onClose(); } } } // Handler for ESC key function handleKeyDown(event: KeyboardEvent) { if (dialog && show && event.key === 'Escape') { if (typeof onClose === 'function') { dialog.close(); onClose(); } } } if (dialog && show) { dialog.addEventListener('mousedown', handleClickOutside); window.addEventListener('keydown', handleKeyDown); } return () => { if (dialog) { dialog.removeEventListener('mousedown', handleClickOutside); } window.removeEventListener('keydown', handleKeyDown); }; }, [show, domId, onClose]); const closeModal = () => { const dialog = document.getElementById(domId) as HTMLDialogElement; if (dialog) { dialog.close(); onClose?.(); } }; return ( ReactDOM.createPortal(
{ closeButton && ( { if (typeof onClose === 'function') { const dialog = document.getElementById(domId) as HTMLDialogElement; if (dialog) { dialog.close(); onClose(); } } }} /> ) }
{children}
, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion document.querySelector('body')!, ) ); }; AUDialogModalComponent.displayName = 'AUDialogModalComponent'; export default AUDialogModalComponent;