import React, { useRef, useState, useEffect } from 'react'; import { default as BootstrapModal } from 'react-bootstrap/Modal'; import { SubCustomDragLayer } from '../SubCustomDragLayer'; import { SubContainer } from '../SubContainer'; import { ConfigHandle } from '../ConfigHandle'; var tinycolor = require('tinycolor2'); export const Modal = function Modal({ id, component, height, containerProps, darkMode, properties, styles, exposedVariables, setExposedVariable, registerAction, fireEvent, dataCy, }) { const [showModal, setShowModal] = useState(false); const { hideOnEsc, hideCloseButton, hideTitleBar, loadingState, useDefaultButton, triggerButtonLabel } = properties; const { headerBackgroundColor, headerTextColor, bodyBackgroundColor, disabledState, visibility, triggerButtonBackgroundColor, triggerButtonTextColor, } = styles; const parentRef = useRef(null); const title = properties.title ?? ''; const size = properties.size ?? 'lg'; registerAction( 'open', async function () { setExposedVariable('show', true); setShowModal(true); }, [setShowModal] ); registerAction( 'close', async function () { setShowModal(false); setExposedVariable('show', false); }, [setShowModal] ); useEffect(() => { if (exposedVariables.show !== showModal) { setExposedVariable('show', showModal).then(() => fireEvent(showModal ? 'onOpen' : 'onClose')); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [showModal]); useEffect(() => { const canShowModal = exposedVariables.show ?? false; if (canShowModal !== showModal) { setShowModal(canShowModal); fireEvent(canShowModal ? 'onOpen' : 'onClose'); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [exposedVariables.show]); function hideModal() { setShowModal(false); } const customStyles = { modalBody: { height, backgroundColor: ['#fff', '#ffffffff'].includes(bodyBackgroundColor) && darkMode ? '#1F2837' : bodyBackgroundColor, overflowX: 'hidden', overflowY: 'auto', }, modalHeader: { backgroundColor: ['#fff', '#ffffffff'].includes(headerBackgroundColor) && darkMode ? '#1F2837' : headerBackgroundColor, color: ['#000', '#000000', '#000000ff'].includes(headerTextColor) && darkMode ? '#fff' : headerTextColor, }, buttonStyles: { backgroundColor: triggerButtonBackgroundColor, color: triggerButtonTextColor, width: '100%', display: visibility ? '' : 'none', '--tblr-btn-color-darker': tinycolor(triggerButtonBackgroundColor).darken(8).toString(), }, }; useEffect(() => { if (containerProps.mode === 'view') { const handleClickOutside = (event) => { const modalRef = parentRef.current.parentElement.parentElement.parentElement; if (modalRef && modalRef === event.target) { hideModal(); } }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; } }, [containerProps.mode, parentRef]); return (