1.0.2 • Published 3 months ago

zoomable-image v1.0.2

Weekly downloads
-
License
-
Repository
-
Last release
3 months ago

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 PropertyDefault ValueDefinition
--zi-backdrop-color#000000e4Color of the backdrop
--zi-button-colorblackText color of the button
--zi-button-focus-outline4px solid #9000a3Outline focus style of the button
--zi-button-outlinenoneOutline style of the button
--zi-button-position-rightDesktop: -16px. Mobile: -10pxRight position of the button
--zi-button-position-topDesktop: -16px. Mobile: -10pxTop position of the button
--zi-image-max-height92vhMaximum image height
--zi-image-max-width92vwMaximum image width
--zi-image-outlinenoneOutline 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.

1.0.2

3 months ago

1.0.1

4 months ago

1.0.0

4 months ago

0.0.18

5 months ago

0.0.13

5 months ago

0.0.14

5 months ago

0.0.15

5 months ago

0.0.16

5 months ago

0.0.17

5 months ago

0.0.11

5 months ago

0.0.12

5 months ago

0.0.10

5 months ago

0.0.9

5 months ago

0.0.8

5 months ago

0.0.7

5 months ago

0.0.6

5 months ago

0.0.5

5 months ago

0.0.4

5 months ago

0.0.3

5 months ago

0.0.1

5 months ago

0.0.0

5 months ago