npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zoomable-image

v1.0.3

Published

Light and customizable JavaScript library for creating a zoom image modal.

Downloads

591

Readme

Zoomable Image 🔍🖼️

Light and customizable JavaScript library (~1Kb) for creating a zoom image modal.

With this library, we can easily create performant zoomed images on click or programmatically.

Features 🎯

  • 📱 Responsive: dynamically scale according to viewport.
  • 🧩 Typed: fully typed for enhanced development experience.
  • ⚖️ Lightweight: approximately 1 kb.
  • 🚀 Performant: optimized to be fast.
  • 🔎 Flexibility: select the images you want to be zoomable or do it programmatically.
  • 🌈 Accessible: designed and conceived to leave no one behind.
  • 🖱 Mouse, keyboard and gesture friendly: click out the image, click the close button, press ESC or scroll to close the zoom.
  • 🕵🏽 Customization: set your scroll offset, image sizes & close button styles.
  • 🌐 Framework agnostic: works with React, Vue, Svelte, Solid, MDX, etc.

Installation ⚙️

The module is available on the npm registry.

npm install zoomable-image

API 🤖

This library works with image and picture elements.

  • The alt attribute will be transferred to the zoomed image.

  • The src attribute will generate the zoomed image. When using a picture element, the library will utilize the currentSrc.

  • Within your markup, you have the option to include a data-zoomable-hd attribute with an alternative image src. This image will replace the original image src. This is useful when you want to show a better quality image in the zoomed one.

initZoomableImages(InitOptions)

Gzipped weight: 1.27 kB

This method will add click event listeners to the images in the document that contains the data-zoomable-image attribute.

Moreover, it will pre-fetch images with the attribute data-zoomable-hd upon mouseenter, touchstart, and focus events, enhancing the overall user experience.

It takes an object InitOptions as a parameter, which may contain the following keys:

  • scrollOffset Number: Optional. The scroll distance needed to close the modal. Default value: 150.
import 'zoomable-image/dist/style.css'
import { initZoomableImages } from 'zoomable-image'

// And when your document is ready...
initZoomableImages()

// Or with custom options..
initZoomableImages({
  scrollOffset: 300,
})

Your HTML code may be like this one:

<img
  data-zoomable-image
  data-zoomable-hd="../assets/high-definition.jpg"
  class="image"
  src="../assets/low-definition.webp"
  alt=""
/>

zoomImage(imageToZoom, ZoomOptions)

Gzipped weight: 1 kB

This method will zoom the image when used. It generates a dialog HTML element and appends it as the next sibling to the original image.

It takes 2 parameters:

  • image HTMLImageElement: Required. The HTML Image Element that will be zoomed.
  • ZoomOptions ZoomOptions: Optional. Options to configure the image zoom.
    • scrollOffset number: Optional. The scroll distance needed to close the modal. Default value: 150.
import 'zoomable-image/dist/style.css'
import { zoomImage } from 'zoomable-image'

const myImage = document.getElementById('image-to-zoom')

// I am opening a modal with this image
zoomImage(myImage)

// Or with custom options..
zoomImage(myImage, { scrollOffset: 300 })

Visual settings 🎨

For customizing the UI, you can create these custom properties and add them in your CSS root. They will be used instead of the default value.

| Custom Property | Default Value | Definition | | ---------------------------- | :-------------------------------: | ----------------------------------- | | --zi-backdrop-color | #000000e4 | Color of the backdrop | | --zi-button-color | black | Text color of the button | | --zi-button-focus-outline | 4px solid #9000a3 | Outline focus style of the button | | --zi-button-outline | none | Outline style of the button | | --zi-button-position-right | Desktop: -16px. Mobile: -10px | Right position of the button | | --zi-button-position-top | Desktop: -16px. Mobile: -10px | Top position of the button | | --zi-image-max-height | 92vh | Maximum image height | | --zi-image-max-width | 92vw | Maximum image width | | --zi-image-outline | none | Outline property style of the image |

[!NOTE] Why custom properties?


Deriving settings to CSS helps maintain a lighter bundle weight.

By adopting this approach, we invoke these methods just once and integrate the custom properties within the desired scope. Remember the new image will be created as the next sibling to the original image.

This method offers increased flexibility and responsiveness.

Personal recommendation 📌

To improve the Core Web Vitals of the page, I recommend deferring the styles of this library since they are non-critical.