简体   繁体   English

z-index不适用于在react中创建的模态且模态未显示在顶部

[英]z-index not working on modal created in react and modal not appearing on top

I have created on modal using HTMl and CSS in react but, modal is not working as expected when deployed on environment as I have set z-index of modal to be higher 我已经在Hactl中使用HTMl和CSS在模态上创建了,但是,由于我将模态的z-index设置得更高,所以在环境中部署时模态无法按预期工作

But there are other component on page who has also z-index and as those component appears before modal component in html page order. 但是页面上还有其他组件也具有z-index,并且这些组件以html页面顺序出现在模式组件之前。

Model doesn't appear on top as expected. 模型未按预期显示在顶部。

IS there any way in react, so that we can override all existing z-index and modal will work as expected. 是否有任何反应的方式,以便我们可以覆盖所有现有的z-index,并且模式将按预期工作。 I am providing CSS code for modal overlay and modal 我为模态叠加和模态提供CSS代码

.modal-overlay{
    position: fixed;
    top: 0px;
    left: 0px;
    bottom: 0px;
    right: 0px;
    z-index: 5;
    background-color: rgba(0,0,0,0.3);
    overflow: scroll;
}
.modal{
    display: flex;
    flex-direction: column;
    flex-basis: auto;
    background-color: #fff;
    max-width: 375px;
    margin: 0px auto;
}

This method uses React Portal. 此方法使用React Portal。 U should have element with id="modal-root" in ur index html file. 您的索引HTML文件中的U元素应具有id =“ modal-root”。 Every time u call a modal, the modal will come inside modal-root, Hence there will be no problem of z-index as ur rendering the Modal at the topmost div 每次您调用模态时,模态都会进入模态根,因此不会出现z-index问题,因为您在最顶层的div渲染模态

Create a file Modal.js 创建一个文件Modal.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

const modalRoot = document.getElementById('modal-root');

class Modal extends Component {
  constructor(props) {
    super(props);
    this.el = document.createElement('div');
  }

  componentDidMount() {
    modalRoot.appendChild(this.el);
    console.log('Modal did mount');
  }

  componentWillUnmount() {
    console.log('Modal will unmount');
    modalRoot.removeChild(this.el);
  }
  render() {
    return ReactDOM.createPortal(
      this.props.children,
      this.el,
    );
  }
}

export default Modal;

Create actual modal code 创建实际的模态代码

import React from 'react';

const ImageContainer = props => (
  <div className="modal d-block full-screen-popup" tabIndex="-1" role="dialog">
    <div className="modal-dialog" role="document">
      <div className="modal-content">
        <div className="modal-header">
          <h6 className="modal-title text-center bold">Title</h6>
          <button type="button" className="close" onClick={props.onClose}>
            <span className="icon-close" />
          </button>
        </div>
        <div className="modal-body p-0">
          <div className="imageBody text-center">
            <img className="img-fluid" src={props.imgSrc} />
          </div>
        </div>
      </div>
    </div>
  </div>
);

export default ImageContainer;

Its CSS should be 它的CSS应该是

.full-screen-popup {
    background-color: rgba(0,0,0,.62);
}

.d-block {
    display: block!important;
}
.modal {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1050;
    display: none;
    outline: 0;
}

Then in any js file, Import the Modal, and pass the actual modal component. 然后在任何js文件中,导入模态,并传递实际的模态组件。

renderImageModal = () => {
    if (this.state.showImageModal) {
      return (
        <Modal>
          <ImageContainer onClose={this.handleImageModalClose} imgSrc={this.state.link} />
        </Modal>);
    }
    return null;
  }

 handleModalOpenClick = () => {

    this.setState({
      showImageModal: true,
    });
  }

handleImageModalClose = () => {
    this.setState({
      showImageModal: false,

    });
  }


    render(){ 
     return(
       <button onclick={this.handleModalOpenClick}>Open Modal</button>
        {this.renderImageModal()})
    }

The best way to create modal using ReactJS is to use Portals . 使用ReactJS创建模态的最佳方法是使用Portals

You can check the docs for portals here in the ReactJS docs or here the particular Modal example on codepen. 您可以查看文档的门户这里的ReactJS文档或在此特定的Modal例如在codepen。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM