phanpy/src/components/modal.jsx

34 lines
810 B
React
Raw Normal View History

2022-12-10 12:14:48 +03:00
import './modal.css';
import { createPortal } from 'preact/compat';
2022-12-30 15:37:57 +03:00
import { useEffect, useRef } from 'preact/hooks';
2022-12-10 12:14:48 +03:00
const $modalContainer = document.getElementById('modal-container');
2022-12-16 08:27:04 +03:00
function Modal({ children, onClick, class: className }) {
2022-12-10 12:14:48 +03:00
if (!children) return null;
2022-12-30 15:37:57 +03:00
const modalRef = useRef();
useEffect(() => {
let timer = setTimeout(() => {
const focusElement = modalRef.current?.querySelector('[tabindex="-1"]');
if (focusElement) {
focusElement.focus();
}
}, 100);
return () => clearTimeout(timer);
}, []);
2022-12-10 12:14:48 +03:00
const Modal = (
2022-12-30 15:37:57 +03:00
<div ref={modalRef} className={className} onClick={onClick}>
2022-12-10 12:14:48 +03:00
{children}
</div>
);
return createPortal(Modal, $modalContainer);
// return createPortal(children, $modalContainer);
2022-12-16 08:27:04 +03:00
}
export default Modal;