简体   繁体   English

当我单击其中的任何位置时,如何阻止我的模态关闭

[英]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.

相关问题 当我单击除选定区域以外的任何位置时,模态关闭 - Modal closing when I click on anywhere except the selected region 如何停止单击按钮时关闭模态对话框? - How can I stop my modal dialog from closing on button click? 单击任何地方关闭菜单; 单击任何位置时如何使其停止打开 - click anywhere close menu; how do I make it stop opening when clicked anywhere 每次单击时如何阻止我的模态显示? - How do I to stop my Modal from showing every time I click on it? 渲染模式时如何停止 useEffect 运行? - How do I stop useEffect from running when rendering a modal? 在下拉菜单中单击时,如何防止它关闭? - How do I prevent my dropdown from closing when clicking inside it? 当我单击 X 或外部时,模态不会关闭 - Modal not closing when I click X or outside 当主容器上有data-dismiss =&#39;modal&#39;并且按下按钮以展开内部面板时,如何保持模式不关闭 - how can i keep a modal from closing when there is a data-dismiss='modal' on the main container and when a button is pressed to expand panels inside 如何防止fancybox关闭? 想要使用户单击“关闭”按钮,而不是在浏览器上的任何位置 - How can I prevent fancybox from closing? Want to make the user click the Close button instead of anywhere on the browser 关闭模式弹出框时,如何防止页面刷新&gt; - How do I prevent the page from refreshing when closing the modal popup box>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM