Skip to Content
docscn

Image zoom

An image that zooms in to fill the screen when clicked.

A gradient

Installation

npx shadcn@latest add @docscn/image-zoom

Usage

ImageZoom takes the props of Next.js' Image. To make every image in your content zoomable, render Markdown images with it in components/mdx.tsx:

components/mdx.tsx
import {
  ImageZoom,
  type ImageZoomProps,
} from "@/components/docs/components/image-zoom";

export function getMDXComponents(components?: MDXComponents) {
  return {
    ...defaultMdxComponents,
    img: (props) => <ImageZoom {...(props as ImageZoomProps)} />,
    ...components,
  } satisfies MDXComponents;
}

It's built on react-medium-image-zoom. Installing it adds the styles the zoomed image needs to your global stylesheet, with the page's --background behind the image.

Zoomable images inside Lightbox open in one viewer instead. Browse them with the arrow keys, by swiping, or with the side buttons.

The notebook layout in light modeThe notebook layout in dark mode

Wrap your page content to browse every image on the page:

app/docs/[[...slug]]/page.tsx
import { Lightbox } from "@/components/docs/components/image-zoom";

<DocsBody>
  <Lightbox>
    <MDX components={getMDXComponents()} />
  </Lightbox>
</DocsBody>;

The viewer shows an image's title as its caption, like "Light mode" in ![Notebook layout](/notebook.png "Light mode"). Pass caption to ImageZoom for rich content such as links. To browse a set of images on its own, wrap them in another Lightbox.

The viewer is a native <dialog> with shadcn/ui buttons, and morphs the image in and out with a view transition where the browser supports them.

Props

Prop

Type