Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

377
Vistas
What would be the best way to show different size of the image popup preview in different screen size with react.js?

I want to click images to open a modal as image previews, so the modal has no margin and padding just shows one whole image preview.

But the problem I have is the image size...

Initially, after clicking the modal open button, I get the images' width and height and overwrite values for width and height.

But some images are 5000px width x 3000px height which is too big for a screen and I use this image preview for not only laptops but also iPad or bigger screens size.

How do you define the size of the image for different screen sizes?

Now the width and height values are fixed.

If the width is bigger than , I set width 800 px and height 500px.

If the height is bigger than width, I set width 500px and height 800px.

and style object-fit: cover but the chance is the image could be square...

const Attachment =() =>{
  const [width, setWidth] = React.useState(0);
  const [height, setHeight] = React.useState(0);

  const handleOpenModal = () => {
    setOpenModal(true);
    getMeta(attachmentCdnUrl);
  };

  function getMeta(url) {
    const img = new Image();
    img.addEventListener('load', function () {
      setWidth(800);
      setHeight(500);
      if (this.naturalWidth > this.naturalHeight) {
        setWidth(800);
        setHeight(500);
      } else {
        setHeight(800);
        setWidth(500);
      }
    });
    img.src = url;
  }
  return(
    ...
  
      <Modal
          open={openModal}
          onClose={handleCloseModal}
          aria-labelledby="child-modal-title"
          aria-describedby="child-modal-description"
        >
          <Box
            sx={{
              ...style,  // here to overwrite width and height
              width: width,
              height: height,
            }}
          >
            <img
              src={attachmentCdnUrl} // here to overwrite width and height
              style={{
                ...style,
                width: width,
                height: height,
              }}
            />
          </Box>
        </Modal>
    ...
  )
}

This code doesn't look exactly the same so just pasting this won't work.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda