1.4.1 • Published 10 months ago

smooth-zoom v1.4.1

Weekly downloads
51
License
MIT
Repository
github
Last release
10 months ago

Smooth Zoom

License NPM Version Known Vulnerabilities NPM Downloads Dependencies

A light weight javascript library for zooming images like Medium, Google Photos and more.

Zoom example

👀 Demo

Install

# npm
npm i smooth-zoom
# pnpm
pnpm i smooth-zoom
# yarn
yarn add smooth-zoom

Usage

Import with script tag

<script src="node_modules/smooth-zoom/dist/zoom.min.js"></script>
<!-- or -->
<script src="https://cdn.jsdelivr.net/npm/smooth-zoom@latest/dist/zoom.min.js"></script>

Import as module

import Zoom from "smooth-zoom";

And calling it with any css-selector, HTMLElement (or Array of it), and NodeList will make your image zoomed on click.

// Css Selector
Zoom(".zoomable");

// HTMLElement || HTMLElement[]
Zoom(document.querySelector(".zoomable"));

Zoom([
    document.querySelector(".foo"),
    ...document.querySelectorAll(".zoomable"),
]);

// NodeList
Zoom(document.querySelectorAll(".zoomable"));

And you can simply use this with custom hook in react.

import { useRef } from "react";
import Zoom from "smooth-zoom";

export default function useZoom() {
    const zoom = useRef(Zoom());

    return {
        attach: zoom.current.attach,
        detach: zoom.current.detach,
    };
}

Add custom hook like above,

import { ImgHTMLAttributes, useEffect, useRef } from "react";
import { useZoom } from "@hooks";

export default function ZoomableImage(
    props: ImgHTMLAttributes<HTMLImageElement>
) {
    const { attach, detach } = useZoom();
    const imageRef = useRef<HTMLImageElement | null>(null);

    useEffect(() => {
        const { current: image } = imageRef;

        attach(image);

        return () => {
            detach(image);
        };
    }, []);

    return <img {...props} ref={imageRef} />;
}

And create a component like above.

Options

Zoom(".zoomable", {
    background: "auto",
    onTransitionEnd: (img) => {
        img.src = img.src.replace(/-[0-9]+\.jpg/, ".jpg");
    },
});
PropertyTypeDefaultDescription
backgroundstringrgba(0, 0, 0, 0.95)Image's background color.Use auto to get average color of the image
useMaximumSizebooleantrueFind the longest width through image's natural width and srcset attribute
onClick(img: HTMLImageElement) => voidundefinedFunction that fires on click. Note that img is original image element.
onTransitionEnd(img: HTMLImageElement) => voidundefinedFunction that fires after zoom animation. Note that img is cloned image element.

Methods

zoom(img: HTMLImageElement) => void

Zoom-in an image

const zoom = Zoom();

zoom.zoom(document.querySelector("img"));

attach(target: string | HTMLElement | NodeList | HTMLElement[]) => void

Make the target zoomed on click

const zoom = Zoom(".zoomable", {
    background: "rgba(0, 0, 0, .3)",
});

zoom.attach(".more-elements");

detach(target: string | HTMLElement | NodeList | HTMLElement[]) => void

Remove an event listener from the target

zoom.detach(".more-elements");
1.4.1

10 months ago

1.4.0

10 months ago

1.3.1

10 months ago

1.2.2

2 years ago

1.2.0

2 years ago

1.2.1

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.0.2

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago