Image Zoom
Pinch-to-zoom and pan for a single image. Two fingers pinch, one finger drags once the image is bigger than its box, and a double-tap — or a double-click on a desktop — toggles between the minimum and maximum scale. Pinching past either end is rubber-banded and snaps back when you let go.
This piece is a wrapper, not a port: it renders the real <image-zoom> custom element from @magic-spells/image-zoom and binds attributes to it. The gesture state machine that keeps a pinch and a pan from stealing each other's fingers, the midpoint-anchored scaling, the translate clamping and the double-tap discriminator are all behavior, not markup — there is nothing there that a re-translation into Puzzle would improve, and plenty it would break.
On a trackpad or mouse, double-click to zoom in and double-click again to zoom out; drag to pan while zoomed. Pinch needs a touchscreen (or Chrome DevTools' device toolbar, where Shift-drag simulates one).
Installation
Copy the piece into your app — it compiles with your build and the code is yours:
puzzle add piece image-zoom
It has one runtime dependency — the web component it wraps:
npm install @magic-spells/image-zoom
Then merge its stylesheet into your app's style entry (app/styles/styles.css). It is required, not cosmetic: the host's overflow clip, the aspect ratio, the absolutely positioned image box the transform is written into, and the touch-action switch that hands a one-finger swipe back to page scroll while unzoomed all live in it.
/* app/styles/styles.css */ @import "tailwindcss"; @import "@magic-spells/image-zoom/css" layer(components);
layer(components) matters here. The sheet is plain element selectors, so an unlayered import would outrank every Tailwind utility on the host — the rounded-xl, the background and the aspect ratio would all silently do nothing. In the components layer the normal mental model holds: your utilities win, the component's defaults fill in the rest.
Requires the pieces.css tokens merged into your app styles (see Introduction).
Usage
import ImageZoom from '@/components/ui/ImageZoom.pzl';
<ImageZoom
class="w-full max-w-lg rounded-xl border border-border bg-surface-sunken aspect-video"
src={ photo }
alt="Mountain road winding below green cliffs"
label="Product photo, zoomable"/>The image is a prop, not a slot. The component's markup contract is "exactly one <img> child": it queries that element once at connect, caches it, and writes the pan/zoom transform straight into its inline style on every frame. A slotted image would let a caller put a dynamic style={ … } binding on it — which the patcher rewrites wholesale, wiping the transform — or hide it behind a conditional, leaving the cached reference pointing at a dead node. Config-first removes both failure modes, so src and alt are props and the piece writes the image itself.
Size the host, not the image: width, aspect ratio, rounding, background and border all go in class. The image inside is absolutely positioned and contain-fitted to whatever box you give it, and scale 1 means exactly that fit.
Constrained max scale, with live feedback
Scale 1.00 of 1.8 max
A gentle max is the right call for a photo that has no more detail to give — 1.8 here, so a double-tap shows the seeds and the skin without dissolving into pixels. Push past it and the rubber band resists, then snaps back on release.
The readout comes from @change, which fires on every transform update — once per frame during a gesture. Round it or debounce it before you put it into state, the way this example does. The Reset button uses @ready, which hands you the upgraded element so you can call reset() or zoomTo() yourself.
Inside a card
Pour-over set
Glazed stoneware, 400 ml carafe.
$68
The host is a block element that fills its container's width, so it drops into a card with no wrapper. Give it an aspect ratio — aspect-square here — and the card keeps its shape whatever the photo's own proportions are; the image contain-fits inside and the background shows through the letterbox. Put overflow-hidden on the card so the rounded corners clip the viewer's top edge.
Swapping the source
Flipping src works. The component measures the image's natural size and its contain-fit base scale once, at connect, and has no attribute for a new source — so the piece re-attaches the host element when src changes: one disconnect/connect pair on the same node, which is exactly the lifecycle the component already handles. Zoom resets to min on a swap, which is what a gallery wants anyway.
Reference
Props
srcImage URL. Required — the piece is an empty box without it. Changing it re-fits the viewer and resets the zoom.altAlternative text, always rendered. Pass '' for a decorative image rather than leaving it off.minMinimum scale multiplier, default 1 — the image contain-fits the host. A value above 1 starts the image cropped and pannable.maxMaximum scale multiplier, default 3. Also the target a double-tap zooms to.aspectCSS aspect-ratio for the host, written as --image-zoom-aspect (the sheet's default is 4 / 3). A Tailwind aspect-* utility does the same job and is the better choice when a token covers it.loading'lazy' | 'eager', forwarded to the image. Safe either way — the component fits on the image's load event, and its ResizeObserver catches an element that was hidden at connect.labelAccessible name for the zoom region. Sets role="group" and aria-label together; omit it and neither is written.classMerged onto the host. Width, aspect ratio, rounding, background and border all go here.styleAppended after the piece's own custom-property declarations.Callbacks
Value-first, like every piece in the registry. The element's own DOM events (image-zoom:change and friends) bubble, so you can also listen on an ancestor if that suits you better.
@change(scale, { translateX, translateY }) — any transform update. Fires per frame during a gesture; debounce anything expensive.@zoomStart(scale) — a pinch began, or a double-tap / reset() / zoomTo() is about to commit. The scale is the one being left.@zoomEnd(scale) — a gesture settled, or a programmatic change committed. Fires at commit time, not when the CSS transition ends.@ready(element) — the upgraded element, once its module has landed. The imperative escape hatch.Element API
Zoom lives in the element — it owns that state for the whole duration of a gesture, so there is no scale prop to fight it with. Take the element from @ready and drive it directly:
element.scaleCurrent scale multiplier.element.min / element.maxThe resolved bounds, after the attributes are parsed and ordered.element.translateX / translateYCurrent pan offset in pixels.element.reset(options)Animate back to min, recentered. Pass { animate: false } to jump.element.zoomTo(scale, options)Animate to a scale, anchored on the container center and clamped into [min, max].Custom properties
Set by the component's stylesheet. Most of them have a Tailwind utility that does the same job and wins over the sheet — reach for the utility first.
--image-zoom-aspectHost aspect-ratio, default 4 / 3. The aspect prop writes it; a Tailwind aspect-* utility overrides it.--image-zoom-bgLetterbox background behind a contain-fitted image, default black. A bg-* utility wins over it — bg-surface-sunken is the token choice.--image-zoom-radiusHost border radius, default 0. rounded-* wins over it.--image-zoom-cursor / -cursor-activeIdle and gesturing cursors, default grab / grabbing.--image-zoom-transition-durationSnap-back and double-tap animation duration, default 300ms.--image-zoom-transition-easingEasing for that animation, default a soft ease-out curve.Accessibility
- Gestures are pointer-only upstream — there is no keyboard zoom. Treat the viewer as an enhancement, never as the only way to see detail that matters.
- Write real alt text. The host is not focusable and adds no tab stop, so the image is what assistive technology has to work with.
- label names the zoom region (
role="group"plus an aria-label). Leave it off and neither is written — a group with no name is just noise in the tree.