Zoom
moco/zoomFreeComponent
Lightbox affordance for figures — Esc, click-outside, focus restore, and scroll lock included.
Live preview
live · rendered by the registry
Rendered by moco on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://mocoui.site/r/zoom.jsonSource
1"use client";23import * as React from "react";4import { createPortal } from "react-dom";5import "./zoom.css";67export interface ZoomProps {8 children: React.ReactNode;9 caption?: React.ReactNode;10}1112/** Lightbox affordance. Esc and click-outside close it. */13export function Zoom({ children, caption }: ZoomProps) {14 const [open, setOpen] = React.useState(false);15 const [mounted, setMounted] = React.useState(false);16 const opener = React.useRef<HTMLButtonElement>(null);1718 React.useEffect(() => setMounted(true), []);1920 React.useEffect(() => {21 if (!open) return;22 const onKey = (e: KeyboardEvent) => e.key === "Escape" && setOpen(false);23 document.addEventListener("keydown", onKey);24 const prev = document.body.style.overflow;25 document.body.style.overflow = "hidden";26 return () => {27 document.removeEventListener("keydown", onKey);28 document.body.style.overflow = prev;29 opener.current?.focus();30 };31 }, [open]);3233 return (34 <>35 <button36 ref={opener}37 type="button"38 className="moco-plate-zoom"39 aria-label="Enlarge this figure"40 onClick={() => setOpen(true)}41 >42 ⤢43 </button>44 {children}45 {mounted && open46 ? createPortal(47 <div48 className="moco-lightbox"49 role="dialog"50 aria-modal="true"51 aria-label="Enlarged figure"52 onClick={(e) => e.target === e.currentTarget && setOpen(false)}53 >54 <div className="moco-lightbox-inner">55 {children}56 {caption ? <figcaption className="moco-cap">{caption}</figcaption> : null}57 <button58 type="button"59 className="moco-btn-sm"60 style={{ marginTop: "1rem" }}61 onClick={() => setOpen(false)}62 >63 Close (Esc)64 </button>65 </div>66 </div>,67 document.body,68 )69 : null}70 </>71 );72}
What it pulls in
Other registry items
Files it writes
More from moco
All 20 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Codecode | moco | Components | Free | 1 dep · 3 files | |
| Comparecompare | moco | Components | Free | no deps · 2 files | |
| Count Upcountup | moco | Components | Free | no deps · 2 files | |
| Covercover | moco | Components | Free | no deps · 2 files | |
| Diagram Kitdiagram | moco | Components | Free | no deps · 2 files | |
| Dot Griddotgrid | moco | Components | Free | no deps · 2 files | |
| Figurefigure | moco | Components | Free | no deps · 2 files | |
| Hookshooks | moco | Components | Free | no deps |
