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 🙏

© 2026 – Pkg Stats / Ryan Hefner

lite-image-preview

v1.1.0

Published

Lightweight image and SVG preview dialog with smooth pinch zoom, crisp SVG scaling, and mobile-friendly gesture handling.

Readme

lite-image-preview

A lightweight, dependency-free image preview dialog for the web.

It supports smooth pan and zoom for raster images and crisp SVG zooming with viewBox, while keeping the API small and easy to use. Touch gestures are handled separately from pointer gestures to improve mobile compatibility.


Features

  • Dependency-free runtime
  • Smooth pan and zoom for images
  • Crisp SVG preview using viewBox-based scaling
  • Mobile-friendly touch handling
  • Pointer support for mouse, pen, and non-touch input
  • Built-in modal preview dialog
  • Reset-to-fit action in the toolbar
  • Small public API, with advanced customization hooks

Installation

pnpm i lite-image-preview

or

npm i lite-image-preview

Quick start

Preview an image from a URL

import { previewImage } from 'lite-image-preview'

const close = await previewImage('/assets/photo.jpg')

// Close the dialog later
close?.()

Preview an existing SVG element

import { previewSvg } from 'lite-image-preview'

const svg = document.querySelector('svg')
if (svg) {
  const close = await previewSvg(svg)
  // close?.()
}

Basic usage

previewImage() and previewSvg() both open a modal preview dialog.

The returned promise resolves to a close handle once the preview is ready.

import { previewImage } from 'lite-image-preview'

const close = await previewImage('https://example.com/image.png')

if (close) {
  // You can close programmatically later.
  close()
}

If the image fails to load, previewImage() throws an error.


API overview

previewImage(url, dispose?, options?)

Opens a preview dialog for a raster image.

  • url — Image URL.
  • dispose? — Optional callback invoked after the dialog fully closes.
  • options? — Optional preview configuration (see ImagePreviewOptions).

Returns:

  • Promise<PreviewCloseHandle>

previewSvg(svg, dispose?)

Opens a preview dialog for an existing SVG element.

  • svg — The SVG element to preview.
  • dispose? — Optional callback invoked after the dialog fully closes.

Returns:

  • Promise<PreviewCloseHandle>

createPreview(content, initAdapter, dispose?)

Advanced API for custom preview content.

  • content — The DOM element to mount into the preview stage.
  • initAdapter — Adapter factory used to implement zoom, pan, reset, and cleanup behavior.
  • dispose? — Optional callback invoked after the dialog fully closes.

Returns:

  • PreviewCloseHandle

Example: basic image preview

import { previewImage } from 'lite-image-preview'

async function openPhoto() {
  const close = await previewImage('/assets/photo.jpg')

  if (!close) {
    console.warn('Image failed to load')
    return
  }

  // close after 3 seconds
  setTimeout(() => close(), 3000)
}

Example: basic SVG preview

import { previewSvg } from 'lite-image-preview'

const svg = document.querySelector('svg')

if (svg) {
  const close = await previewSvg(svg)

  // close?.()
}

Example: custom preview content

The package also exposes createPreview() and PreviewAdapter for advanced use cases.

import { createPreview, type PreviewAdapter } from 'lite-image-preview'

const el = document.createElement('div')
el.textContent = 'Custom content'
el.style.width = '400px'
el.style.height = '240px'
el.style.background = '#ddd'

const close = createPreview(el, (stage): PreviewAdapter => {
  // Example adapter: no-op transform behavior.
  return {
    fitToStage() {},
    panBy() {},
    zoomAt() {},
    beginPinch() {},
    updatePinch() {},
    zoomWithWheel(e) {
      e.preventDefault()
    },
    destroy() {},
    resetStyle() {},
  }
})

// close()

Keyboard and interaction behavior

  • Click the close button to exit the dialog.
  • Click Reset to return to the initial fitted view.
  • Mouse wheel zooms around the pointer position.
  • Drag to pan.
  • Pinch with two fingers to zoom on touch devices.

How the preview behaves

Raster images

Raster images use a transform-based renderer.

  • Initial state is fitted and centered in the stage.
  • Dragging pans the image.
  • Wheel and pinch gestures zoom around the gesture center.
  • Reset returns to the initial fitted state.

SVG

SVG uses viewBox-based scaling instead of CSS scaling.

This keeps zoomed SVG content crisp instead of rasterizing it too early.

  • Initial state is fitted and centered.
  • Dragging updates the viewBox position.
  • Wheel and pinch gestures update the viewBox size and position.
  • Reset returns to the initial fitted viewBox.

Public types

The package exports the main types so advanced consumers can build custom adapters.

  • PreviewController
  • PreviewAdapter
  • PreviewAdapterFactory
  • Point
  • TransformState
  • ViewBox
  • PreviewCloseHandle
  • ImagePreviewOptions

ImagePreviewOptions includes:

  • minScale?: number — minimum zoom scale (default: 0.1)
  • maxScale?: number — maximum zoom scale (default: 8)
  • fitPadding?: number — padding around the image when fitting to stage (default: 0)
  • fitMaxScale?: number — maximum scale when fitting to stage (default: 1)

Advanced customization

If you need to preview a different kind of element, use createPreview() plus a custom adapter.

A custom adapter can decide how to implement:

  • initial fitting
  • panning
  • zooming around a point
  • pinch gesture updates
  • wheel zoom
  • cleanup
  • style restoration

This design lets the container stay simple while each rendering backend keeps its own optimal strategy.


Notes on browser support

This package is designed for modern browsers.

It uses:

  • <dialog>
  • Pointer Events for non-touch input
  • Touch Events for mobile pinch handling
  • requestAnimationFrame
  • CSS transforms and SVG viewBox

If you need to support old browsers, consider using polyfills.


Development notes

  • No runtime dependencies are required.
  • SVG preview is intentionally implemented with viewBox so zoomed content stays sharp.
  • Touch gestures are handled separately from pointer gestures to improve mobile browser behavior.

Note: We might add EventTarget support so that a 'close' event can be dispatched when the user closed the dialog, providing more customability.


License

Unlicense