Installation
Add the component
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/image-zoom.jsonFirst component? Set up your project first
Frameworks
The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.
"use client";
import * as React from "react";
import { motion, type Transition } from "motion/react";
import { cn } from "@/lib/utils";
import { useReduceMotion } from "../motion";
import { clamp, useCoarsePointer } from "./shared";
export type ImageZoomProps = React.ComponentProps<"div"> & {
children: React.ReactNode;
zoomScale?: number;
transition?: Transition;
zoomOnClick?: boolean;
zoomOnHover?: boolean;
disabled?: boolean;
width?: React.CSSProperties["width"];
height?: React.CSSProperties["height"];
};
export function ImageZoom({
children,
zoomScale = 3,
transition = { type: "spring", stiffness: 200, damping: 28 },
zoomOnClick = true,
zoomOnHover = true,
disabled = false,
width = "100%",
height = "100%",
className,
style,
onMouseEnter,
onMouseLeave,
onMouseMove,
onClick,
onTouchStart,
onTouchMove,
onTouchEnd,
...props
}: ImageZoomProps) {
const [zoomed, setZoomed] = React.useState(false);
const [origin, setOrigin] = React.useState("50% 50%");
const ref = React.useRef<HTMLDivElement>(null);
const coarse = useCoarsePointer();
const reduce = useReduceMotion();
const setOriginFromEvent = React.useCallback(
(event: React.MouseEvent | React.TouchEvent) => {
if (!ref.current) return;
const rect = ref.current.getBoundingClientRect();
let clientX = 0;
let clientY = 0;
if ("touches" in event && event.touches[0]) {
clientX = event.touches[0].clientX;
clientY = event.touches[0].clientY;
} else if ("clientX" in event) {
clientX = event.clientX;
clientY = event.clientY;
}
const x = clamp(clientX - rect.left, 0, rect.width);
const y = clamp(clientY - rect.top, 0, rect.height);
setOrigin(
`${rect.width ? (x / rect.width) * 100 : 50}% ${
rect.height ? (y / rect.height) * 100 : 50
}%`,
);
},
[],
);
const handleEnter = (event: React.MouseEvent<HTMLDivElement>) => {
onMouseEnter?.(event);
if (disabled || coarse || !zoomOnHover) return;
setZoomed(true);
};
const handleLeave = (event: React.MouseEvent<HTMLDivElement>) => {
onMouseLeave?.(event);
if (disabled || coarse || !zoomOnHover) return;
setZoomed(false);
};
const handleMove = (event: React.MouseEvent<HTMLDivElement>) => {
onMouseMove?.(event);
if (disabled || coarse) return;
setOriginFromEvent(event);
};
const handleClick = (event: React.MouseEvent<HTMLDivElement>) => {
onClick?.(event);
if (disabled || !zoomOnClick) return;
setOriginFromEvent(event);
setZoomed((current) => !current);
};
const handleTouchStart = (event: React.TouchEvent<HTMLDivElement>) => {
onTouchStart?.(event);
if (disabled) return;
setOriginFromEvent(event);
if (!zoomOnClick) setZoomed(true);
};
const handleTouchMove = (event: React.TouchEvent<HTMLDivElement>) => {
onTouchMove?.(event);
if (disabled) return;
setOriginFromEvent(event);
};
const handleTouchEnd = (event: React.TouchEvent<HTMLDivElement>) => {
onTouchEnd?.(event);
if (disabled) return;
if (!zoomOnClick) setZoomed(false);
};
return (
<div
ref={ref}
role="img"
data-slot="image-zoom"
className={cn("relative overflow-hidden", className)}
style={{
width,
height,
touchAction: "manipulation",
cursor: disabled ? "default" : zoomed ? "zoom-out" : "zoom-in",
...style,
}}
onMouseEnter={handleEnter}
onMouseLeave={handleLeave}
onMouseMove={handleMove}
onClick={handleClick}
onTouchStart={handleTouchStart}
onTouchMove={handleTouchMove}
onTouchEnd={handleTouchEnd}
{...props}
>
<motion.div
animate={{ scale: disabled || reduce ? 1 : zoomed ? zoomScale : 1 }}
transition={transition}
style={{
width: "100%",
height: "100%",
display: "flex",
alignItems: "center",
justifyContent: "center",
transformOrigin: origin,
willChange: "transform",
}}
>
{children}
</motion.div>
</div>
);
}
export type ImageProps<T extends React.ElementType = "img"> = {
objectFit?: React.CSSProperties["objectFit"];
as?: T;
} & Omit<React.ComponentProps<T>, "as">;
export function Image<T extends React.ElementType = "img">({
objectFit = "cover",
as,
...props
}: ImageProps<T>) {
const Component = (as ?? "img") as React.ElementType;
return (
<Component
draggable={false}
style={{
width: "100%",
height: "100%",
objectFit,
userSelect: "none",
pointerEvents: "none",
}}
{...props}
/>
);
}Install
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/image-zoom.jsonDependencies
motion lucide-reactThe registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.
Usage
Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.
import { ImageZoom } from "@/components/prequelui/effects/image-zoom";
export default function Demo() {
return (
<ImageZoom zoomScale={2.4} width={300} height={176} className="rounded-xl border">
<img src="/cover.jpg" alt="" className="h-full w-full object-cover" />
</ImageZoom>
);
}import { ImageZoom } from "@/components/prequelui/image-zoom";
export function Variant() {
return (
<ImageZoom tempo={1.2}>
…
</ImageZoom>
);
}Animation
The image is scaled inside a clipping container and its transform origin follows the pointer. Hovering magnifies, leaving returns to scale 1, and a click toggles the zoom for touch devices. The origin is capped to the container bounds so an edge never shows empty space.
- Default spring: stiffness 200, damping 28.
- Pointer position is converted to percentages, so the origin survives a resize.
- Coarse pointers use touch events and click-to-toggle instead of hover.
- Reduced motion keeps the pointer origin but removes the scale.
API Reference
Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.
ImageZoom
| Prop | Type | Default |
|---|---|---|
| zoomScale | number | 3 |
| zoomOnHover | boolean | true |
| zoomOnClick | boolean | true |
| disabled | boolean | false |
| width | CSSProperties['width'] | "100%" |
| height | CSSProperties['height'] | "100%" |
| transition | Transition | component spring or tween |
| className | string | — |