Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tailwindcss preflight breaks zoom position #146

Open
HasanMothaffar opened this issue Dec 13, 2023 · 1 comment
Open

Tailwindcss preflight breaks zoom position #146

HasanMothaffar opened this issue Dec 13, 2023 · 1 comment

Comments

@HasanMothaffar
Copy link

HasanMothaffar commented Dec 13, 2023

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);
}
@HuanRE
Copy link

HuanRE commented Apr 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants