Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
How to position a modal relative to a layout when it's rendered deep in the components tree?

I am trying to create a modal in React, but running into some problems.

Suppose I have the following structure:

React structure

<ComponentUsingModal>
    <Left>  
        <ButtonToActivateModal>
            ...
        </ButtonToActivateModal>
        <ModalComponent>
            ...
        </ModalComponent>
    </Left>
    <Right>
        ...
    </Right>
</ComponentUsingModal>

Code for modal

<div className='modal'>
    <div className='modal-content'>
    </div>
</div>

CSS

.modal {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    z-index: 2;
}

.modal-content {
    color: white;
    width: 313px;
    height: 332px;
    background-color: red;
}

Note: For the button, I am using the useState and useEffect hook to update a truthy value within the modal component to render it to the screen.

Here, the modal is centred. Now suppose that the centre content is split into two pieces such that there is a left and right side.

The goal is the have the modal positioned on the left side.

Using JavaScript, I was able to reposition the modal to the left side when the modal is rendered to the screen. However, since the modal was initially positioned at the centre, so you can visually see the modal moving from the centre to the left side (albeit rather quickly). It doesn't matter where the modal is initially positioned, there will be that janky movement. I thought about making all the colours transparent and pass the styling within the React component, but that seems too hack-y for my liking.

What other alternatives are there?

Note: I want the modal to be fixed in the same position regardless of the width and height of the screen.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Just put position: absolute to the modal and for the parent, put a position: relative (for all parent like <Left>, <Right> etc)

For more caution, put the modal component as an immediate child of the container component

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda