react-inner-image-zoom icon indicating copy to clipboard operation
react-inner-image-zoom copied to clipboard

Tailwindcss preflight breaks zoom position

Open HasanMothaffar opened this issue 1 year ago • 1 comments

When using this component with TailwindCSS and Preflight is enabled, the zoom functionality is kind of broken. I found out that the bug was due to the height: auto in preflight styles, which was preventing the hover img from taking up its full, original height.

Unfortunately, using all kinds of CSS reset declarations (inherit, revert, initial, unset) doesn't work. This is explained in https://stackoverflow.com/questions/44010645/remove-height-auto-using-css. Interestingly, the revert-layer css rule was able to fix this issue, but it's an experimental rule that seems to work only on Firefox at the moment of writing.

I came up with a solution using React, which I will share below.

Here's a picture without using this solution (mouse is in the middle):

image

After solution (mouse is in the middle):

image

Solution:

import InnerImageZoom from "react-inner-image-zoom";
import "react-inner-image-zoom/lib/InnerImageZoom/styles.min.css";
import { useEffect, useState } from "react";
import styles from "./ProductDetailsImage.module.css"

export const ProductDetailsImage = ({ imageSrc = "", alt = "" }) => {
    const [height, setHeight] = useState<number>();
    const zoomScale = 1.5;

    useEffect(() => {
        const img = new Image();
        img.src = imageSrc;

        img.onload = () => {
            setHeight(img.naturalHeight * zoomScale);
        }
    }, [imageSrc]);
    
    return (
        <div className="mb-4" style={{"--image-height": `${height}px`}}>
            <InnerImageZoom className={styles.productDetailsImage} src={imageSrc} zoomType="hover" zoomScale={zoomScale} imgAttributes={{ alt }}  />
        </div>
    );
};

And here's ProductDetailsModule.css:

.productDetailsImage :global(.iiz__zoom-img) {
    height: var(--image-height);
}

HasanMothaffar avatar Dec 13 '23 07:12 HasanMothaffar

Setting height: revert-layer; also worked for me on Chrome.

HuanRE avatar Apr 03 '24 20:04 HuanRE