我有一个使用样式组件创建的模式。它用覆盖物包裹。当我单击叠加层时,模态会自动关闭,但是当我单击模态的内容时,模态也会关闭。这个想法是仅当我们超出(box / content)时才关闭覆盖。我相信这与CSS有关。我们该如何解决?
为了能够重现该问题,我创建了一个沙箱:https : //codesandbox.io/s/zen-cache-q5w0n
您可以onClick
在ModalBody
组件上定义和使用event.stopPropagation
。
stopPropagation
根据mdn:
事件接口的stopPropagation()方法可防止当前事件在捕获和冒泡阶段中进一步传播。
调整后的Modal
组件版本:
export const Modal: React.FC<ModalProps> = (props) => {
return (
<div>
{props.isOpened && (
<Overlay hide={props.hideModal}>
<ModalContainer>
<ModalPosition>
<div className={"w-100"}>
<ModalBody onClick={e => e.stopPropagation()}>{props.children}</ModalBody>
</div>
</ModalPosition>
</ModalContainer>
</Overlay>
)}
</div>
);
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句