Arvis Iljins
Arvis Iljins

Reputation: 405

whay when clicking on my modal, the model disappears

please help! Why when clicking on my modal, the model disappears. I expected only if click outside modal. Do I need to change the z-index or what? please check the example.

The real closing function is on the context page but it's working well. I try to change the z-index but it's still not working

export default class FeaturedPortfolio extends Component {
  static contextType = PortfolioContext;

  render() {
    let { loading, featuredPortfolio: portfolio } = this.context;
    portfolio = portfolio.map((portfolios) => {
      return <SinglePortfolio key={portfolios.id} portfolios={portfolios} />;
    });
    return (
      <PortfolioConsumer>
        {(value) => {
          const { modalPortfolioOpen } = value;
          if (!modalPortfolioOpen) {
            return null;
          } else {
            return (
              <motion.div
                initial="out"
                animate="in"
                exit="out"
                variants={pageVariant}
                transition={pageTransition}
              >
                <CloseSection
                  onClick={() => {
                    value.closePortfolioModal();
                  }}
                >
                  <Section>
                    <div className="row-title">
                      <Title title="portfolio" />
                    </div>
                    <ModalContainer>
                      <div className="featured-rooms-center">
                        {loading ? <Loading /> : portfolio}
                      </div>
                    </ModalContainer>

                    <button
                      className="btn-close"
                      onClick={() => {
                        value.closePortfolioModal();
                      }}
                    >
                      <i className="fas fa-times-circle" title="Close" />
                    </button>
                    <div className="button-row">
                      <Link to="/portfolios/">
                        <button className="btn-primary">See all</button>
                      </Link>
                    </div>
                  </Section>
                </CloseSection>
              </motion.div>
            );
          }
        }}
      </PortfolioConsumer>
    );
  }
}

const CloseSection = styled.div`
  color: rgba(2, 2, 34, 0.404);
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  z-index: 1;
`;
const Section = styled.div`
  position: fixed;
  padding: 0 0;
  z-index: 2;
  width: 90%;
  height: 60%;
  left: 5%;
  right: 5%;
  position: absolute;
  top: 20%;
  bottom: 5%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--mainDark);
  box-shadow: 0 0 40px -10px var(--mainAccent);
  border-radius: 12px;
  .row-title {
    margin-top: 0.5rem;
    justify-content: center;
    position: absolute;
    top: 0;
  }

Thanks for your help.

Upvotes: 0

Views: 313

Answers (1)

Arvis Iljins
Arvis Iljins

Reputation: 405

Finally, find a solution for my problem, so if in need a close modal when clicking outside my modal, I just need to create may close section separated from modal content and put lover z-index.

<CloseSection
onClick={() => {
value.closePortfolioModal();
}}
/>
  const CloseSection = styled.div`
  color: rgba(2, 2, 34, 0.404);
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  z-index: 1;
;`

Upvotes: 1

Related Questions