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

248
Visualizações
How do I expand div smoothly when new content is added

How do I make a div expand smoothly when content is added?

const [render, setRender] = useState(false)

    const showHide= () => {
        setRender(!render)
    }

  return (
    <div className="container">
      <h1>TEST CONTAINER</h1>
      {render && <Paragprah />}
      <button type="button" class="btn btn-primary" onClick={showHide}>Primary</button>
    </div>
  );
}

CSS

.container {
  margin-left: 30%;
  margin-top: 10%;
  width: 500px;
  height: auto;
  background-color: whitesmoke;
}

In the above, a component is being rendered when a button is clicked, when the new content is added to the div, it expands instantly. basically, I would like it to expand smoothly when the content is added. Here is a video as well by what I mean when it expands instantly.

Setting a fixed height wont work in my situation because the content that's being loaded is dynamic, its coming from an API and the length is different everytime. So setting a fixed height will make the content overflow sometimes. I need a way where the transition can occur smoothly and the height still be large enough to fit the content, which would probably require ```height: auto;`` to be present?

Link to Imgur video

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

0

Here I updated the code. Since the content that's being loaded has dynamic size then you have to track that content size using useRef hook and store it on a variable and export it with your component.

import { useRef, useEffect} from "react";

var ParagraphHeight;

function Paragraph() {
  const ref = useRef(null);

  useEffect(() => {
    //when you content is loaded store its height
    ParagraphHeight = ref.current.offsetHeight;
  }, []);

  return (
    <div ref={ref}>
      <p>Your content here for exemple...</p>
    </div>
  );
}

export { Paragraph, ParagraphHeight };

Now import your Paragraph component and put it inside a .wrapper div that has a default height of 0px (that means your Paragraph will be hidden) and when you click on the button the height of it will change to the ParagraphHeight

import { useState } from "react";
import { Paragraph, ParagraphHeight } from "./Paragraph.js";

export default function App() {
  const [isopned, setisopned] = useState(false);
  const [wrapperHeight, setWH] = useState(0);
  const showHide = () => {
    if (!isopned) setWH(ParagraphHeight);
    else setWH(0);
    setisopned(!isopned);
  };

  return (
    <div className="container">
      <h1>TEST CONTAINER</h1>
      <div className="wrapper" style={{ height: wrapperHeight }}>
        <Paragraph />
      </div>
      <button type="button" className="btn btn-primary" onClick={showHide}>
        Primary
      </button>
    </div>
  );
}

Finally don't forget to update your css, add overflow-y hidden and css transition to to expand your wrapper div smoothly.

.container {
  margin-left: 30%;
  margin-top: 10%;
  width: 500px;
  background-color: whitesmoke;
}

.wrapper {
  display: grid; /*to prevent some problem caused by overflow hidden*/
  transition: height 1s linear;
  background-color: wheat;
  overflow-y: hidden;
}

This is a live exemple demo

about 4 years ago · Juan Pablo Isaza Relatório

0

you can use the transition css property for a smooth transition.

transition: height 2s;

Follow this link for reference.

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