React-responsive-modal:打开模态时更改背景颜色

Ale*_*ex 3 javascript css jsx reactjs react-modal

我使用react-responsive-modal 在我的 react 应用程序中打开一些模态。当我打开模态时,有一个叠加效果使模态后面的背景变暗。有没有办法使背景变暗 100% 或为背景设置任何颜色,这样我就看不到打开模态之前存在的东西,直到我再次关闭模态?

ModalComponent在 my 中为模态创建了一个新组件,MainComponent当我单击按钮时会呈现该组件:

ModalComponent

render() {
 return (
  <div className="childDiv">
    <Modal
      open={open}
      onClose={this.onCloseModal}
      center
      classNames={{
        transitionEnter: styles.transitionEnter,
        transitionEnterActive: styles.transitionEnterActive,
        transitionExit: styles.transitionExitActive,
        transitionExitActive: styles.transitionExitActive
      }}
      animationDuration={1000}
    >
   ...
Run Code Online (Sandbox Code Playgroud)

主要组件:

<div>
    <div className="outter" onClick={this.openModal.bind(this)}>
//Open Modal when clicking on this div
      <p className="arrival">Ankunft am Ziel: ~ {this.props.arrival} Uhr</p>
      <p className="price">max. {this.props.price} €</p>
      {this.state.open && (
        <BookingModalNew
          open={this.state.open}
          triggerCloseModal={this.closeModal.bind(this)}
          destination={this.props.destination}
          arrival={this.props.arrival}
          price={this.props.price}
        />
      )}
//Whole Stuff should not be visible while Modal is opened
Run Code Online (Sandbox Code Playgroud)

And*_*L64 10

只需将一个具有您样式的对象分配overlay给一个变量,例如bg在您的渲染中,然后只需使用styles道具在您的 Modal 中引用该对象,如下所示:

render() {

 const bg = {
   overlay: {
     background: "#FFFF00"
   }
 };

 return (
  <div className="childDiv">
    <Modal open={open} onClose={this.onCloseModal} center styles={bg} }}>
        <p>Your Modal Content</p>
    </Modal>
  </div>
 )
Run Code Online (Sandbox Code Playgroud)

}


但是等等。当我们可以像这样直接编写样式时,为什么还要创建一个额外的对象:

<Modal open={open} onClose={this.onCloseModal} center styles={background: "#FFFF00"}>
    <p>Your Modal Content</p>
</Modal>
Run Code Online (Sandbox Code Playgroud)

即使上面的方法看起来和我的代码做同样的事情,也不会起作用,这是因为你不能直接在react-responsive-modal. 您需要先将样式放在一个对象中,然后将styles道具引用到该对象。


但是,您可以styles通过执行以下操作在道具本身内创建对象:

<Modal open={open} onClose={this.onCloseModal} center styles={{ overlay: { background: "#FFFF00" } }}>
    <p>Your Modal Content</p>
</Modal>
Run Code Online (Sandbox Code Playgroud)

但是建议你在外面定义对象,然后在stylesprop内部引用它,如上图所示。