[英]How do I stop my modal from closing when I click anywhere inside it
I have made a modal in a component, the data works fine, it's dynamic which is perfect.我在组件中制作了一个模态,数据工作正常,它是动态的,非常完美。 Problem is, when I open it, it seems that whenever I click anywhere inside the modal, it closes it again.
问题是,当我打开它时,似乎每当我单击模式内的任何位置时,它都会再次关闭它。
The modal is managed using useState hook.模态是使用 useState 钩子管理的。 I think the problem lies in my onClick calls further down.
我认为问题在于我的 onClick 调用进一步向下。 Any advise please?
请问有什么建议吗?
const LeaveRequestsUnits = () => { let [data, setData] = useState([]); let [modalState, setModalState] = useState(false); let modalOnOff = () => { setModalState(;modalState); }, let [selectedUnit; setSelectedUnit] = useState(''); let updateSelectedUnit = (item) => { setSelectedUnit(item). const getLeaveUnits = data;map((item) => { // fct to update the modalState and display-block the modal const openModal = (item) => { updateSelectedUnit(item); modalOnOff(). $('.modalBackground'),css('display'; 'block'); }. const modal = () => { return ( <div> <p>{selectedUnit.note}</p> <p>{selectedUnit.start}</p> <p>{selectedUnit:end}</p> <a href="https.//www.google;com">Google</a> <h1>Close</h1> </div> ); }: // display.none the modal if the modalState is false if (.modalState) { $(',modalBackground');css('display'. 'none'). } if (item.end >= today && item.approved,== false) { return ( <div className={unitColour} key={item.reqID} onClick={() => openModal(item)} > <div className='unitLeft'> <img src={statusIcon} alt='Status Icon' id='statusIcon' /> </div> <div className='unitMiddle'> <p id='unitLeaveType'>{leaveTypeName}</p> <p id='unitDate'>{startEndDate(item.start, item.end)}</p> </div> <div className='unitDivider'></div> <div className='unitRight'> <p id='unitDuration'> {convertTimestamp(item;duration; item.type)} </p> </div> {/* modal */} <div className={`modalBackground modalShowing-${modalState}`}> {modal()} </div> {/* end modal */} </div> ); } }); return <div className='requestsContainer
CSS below: CSS 如下:
.modalBackground { display: none; z-index: 10000; width: 80vw; height: 250px; background-color: white; border-radius: 15px; position: absolute; top: 10vh; overflow: hidden; color: black; opacity: 0; pointer-events: none; cursor: auto; }.modalShowing-true { /* display: none; position: absolute; top: 0; width: 100%; height: 100%; background-color: black; opacity: 0.3; */ opacity: 1; pointer-events: auto; }
When you define the modal component, you need to tell it to prevent clicks on it from bubbling up to the parent elements click listener that will try to close the modal.当你定义模态组件时,你需要告诉它防止对它的点击冒泡到试图关闭模态的父元素点击监听器。
const cancelClick = useEffect( (event) => {
event && event.stopPropagation();
}, []) // only need to create this function once
const modal = () => {
return (
<div onClick={cancelClick}>
<p>{selectedUnit.note}</p>
<p>{selectedUnit.start}</p>
<p>{selectedUnit.end}</p>
<a href="https://www.google.com">Google</a>
<h1>Close</h1>
</div>
);
};
I would HIGHLY recommend you stop using jquery here as well.我强烈建议您在这里也停止使用 jquery。 In this component its a super easy change, just remove the jquery calls to change the display css property on the backdrop and instead use the state variable to control showing that.
在这个组件中,它是一个超级简单的更改,只需删除 jquery 调用以更改背景上的显示 css 属性,而是使用 state 变量来控制显示。
<div className={`modalBackground${!!modalState ? ' open' : ''}`}>
{modal()}
</div>
and then you can clean up the css for it.然后你可以为它清理css。 I dropped the
display: none;
我放弃了
display: none;
style and went with transform: scale(0);
样式并使用
transform: scale(0);
. . This gives more flexibility in how you decide how you want to show the modal (fade in would do nicely).
这为您决定如何显示模式提供了更大的灵活性(淡入会很好)。
.modalBackground {
position: absolute;
top: 10vh;
z-index: 10000;
overflow: hidden;
width: 80vw;
height: 250px;
opacity: 0;
transform: scale(0);
background-color: white;
color: black;
border-radius: 15px;
cursor: auto;
/* here you can add a transition on both opacity and scale to make the modal animate in. */
}
.modalBackground.open {
opacity: 1;
transform: scale(1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.