SunUI
创始会员$79 终身更新,1月11日截止领取名额
免费点按

灯箱相册

图片相册:缩略图会长成全屏大图,关闭时缩回原位;切换时旧图缩回自己的缩略图、新图从它的缩略图长出来。

适合使用的场景

  • 商品照片相册和评价晒图
  • 型录和用户照片墙

注意事项

  • 每张图都要写真实的 alt,它就是对话框的名称
  • renderThumb 自定义的缩略图没有放大动画

交给你的 agent 安装

一次复制就包含安装命令、使用时机、props 与设计规则。粘贴到 Claude Code、Cursor 或 Codex 即可。

用 CLI 安装

使用 shadcn CLI。源码会复制到你的项目中,每一行都由你掌控。

$ npx shadcn@latest add https://sunui.synvize.com/r/lightbox.json

或手动安装

  1. 1. 安装依赖。
    $ npm install clsx tailwind-merge framer-motion
  2. 2. 如果还没有,在 lib/utils.ts 添加 cn 辅助函数。没有用 shadcn/ui?再加上 sunui-theme.css 提供颜色变量(做法)。然后复制组件代码。
    lightbox.tsx
    "use client";
    
    import { AnimatePresence, LayoutGroup, motion, useReducedMotion, type PanInfo } from "framer-motion";
    import { useCallback, useEffect, useId, useRef, useState, type ReactNode } from "react";
    import { createPortal } from "react-dom";
    
    import { cn } from "@/lib/utils";
    
    const springs = {
      snappy: { type: "spring", stiffness: 520, damping: 34 },
      gentle: { type: "spring", stiffness: 240, damping: 30 },
    } as const;
    
    export interface LightboxImage {
      id: string;
      src: string;
      alt: string;
      /** Caption below the large image */
      caption?: ReactNode;
      /** The thumbnail can use a smaller file; when omitted, src is used */
      thumbSrc?: string;
    }
    
    export interface LightboxProps {
      images: LightboxImage[];
      /** Class for the thumbnail grid, e.g. "grid-cols-3 gap-2" */
      gridClassName?: string;
      /** Custom thumbnail; draw the image inside an element that will be enlarged */
      renderThumb?: (image: LightboxImage, index: number) => ReactNode;
      /** Aspect ratio of the thumbnail */
      aspect?: string;
      /** fixed: covers the whole window; contained: covers only the nearest relative container */
      position?: "fixed" | "contained";
      /** Controlled: which image is open; null means closed */
      index?: number | null;
      onIndexChange?: (index: number | null) => void;
      labels?: { close?: string; previous?: string; next?: string; open?: (alt: string) => string };
      className?: string;
    }
    
    function Icon({ d }: { d: string }) {
      return (
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" className="size-5" aria-hidden="true">
          <path d={d} />
        </svg>
      );
    }
    
    export function Lightbox({
      images,
      gridClassName = "grid-cols-3 gap-2",
      renderThumb,
      aspect = "4 / 5",
      position = "fixed",
      index: indexProp,
      onIndexChange,
      labels = {},
      className,
    }: LightboxProps) {
      const reduce = useReducedMotion();
      const group = useId();
      const [inner, setInner] = useState<number | null>(null);
      const [mounted, setMounted] = useState(false);
      const index = indexProp !== undefined ? indexProp : inner;
      const current = index === null ? null : images[index];
      const closeRef = useRef<HTMLButtonElement>(null);
      const thumbRefs = useRef<(HTMLButtonElement | null)[]>([]);
      const lastIndex = useRef<number | null>(null);
      const L = { close: "Close", previous: "Previous image", next: "Next image", open: (alt: string) => `Enlarge: ${alt}`, ...labels };
    
      useEffect(() => setMounted(true), []);
    
      const set = useCallback(
        (next: number | null) => {
          if (indexProp === undefined) setInner(next);
          onIndexChange?.(next);
        },
        [indexProp, onIndexChange],
      );
    
      const step = useCallback(
        (delta: number) => {
          if (index === null) return;
          set((index + delta + images.length) % images.length);
        },
        [index, images.length, set],
      );
    
      useEffect(() => {
        if (index === null) {
          if (lastIndex.current !== null) thumbRefs.current[lastIndex.current]?.focus();
          lastIndex.current = null;
          return;
        }
        lastIndex.current = index;
      }, [index]);
    
      const isOpen = index !== null;
      useEffect(() => {
        if (!isOpen) return;
        closeRef.current?.focus();
        const onKey = (e: KeyboardEvent) => {
          if (e.key === "Escape") set(null);
          if (e.key === "ArrowRight") step(1);
          if (e.key === "ArrowLeft") step(-1);
        };
        document.addEventListener("keydown", onKey);
        const prev = document.body.style.overflow;
        if (position === "fixed") document.body.style.overflow = "hidden";
        return () => {
          document.removeEventListener("keydown", onKey);
          document.body.style.overflow = prev;
        };
      }, [isOpen, position, set, step]);
    
      const onDragEnd = (_: unknown, info: PanInfo) => {
        if (Math.abs(info.offset.x) > 60 || Math.abs(info.velocity.x) > 500) step(info.offset.x < 0 ? 1 : -1);
        else if (info.offset.y > 100) set(null);
      };
    
      const layoutT = reduce ? { duration: 0 } : springs.gentle;
    
      const overlay = (
        <AnimatePresence>
          {current && index !== null && (
            <div role="dialog" aria-modal="true" aria-label={current.alt} className={cn("inset-0 z-50 flex flex-col", position === "fixed" ? "fixed" : "absolute")}>
              <motion.div
                className="absolute inset-0 bg-foreground/90"
                initial={{ opacity: 0 }}
                animate={{ opacity: 1, transition: { duration: 0.2 } }}
                exit={{ opacity: 0, transition: { duration: 0.18 } }}
                onClick={() => set(null)}
              />
              <div className="relative flex items-center justify-between p-3 text-background">
                <span className="px-2 text-xs tabular-nums opacity-70">
                  {index + 1} / {images.length}
                </span>
                <button ref={closeRef} type="button" aria-label={L.close} onClick={() => set(null)} className="grid size-10 place-items-center rounded-md outline-offset-2 hover:bg-white/10 focus-visible:outline-2 motion-safe:active:scale-[0.97] transition-[color,background-color,border-color,opacity,scale]">
                  <Icon d="M6 6l12 12M18 6L6 18" />
                </button>
              </div>
              <div className="pointer-events-none relative flex min-h-0 flex-1 items-center justify-center px-12 sm:px-16">
                {/* When switching, the old image shrinks back into its own thumbnail and the new one grows from its thumbnail, so the spatial relationship always stays clear */}
                <AnimatePresence initial={false} mode="popLayout">
                  <motion.img
                    key={current.id}
                    layoutId={`${group}-${current.id}`}
                    src={current.src}
                    alt={current.alt}
                    draggable={false}
                    drag={reduce ? false : true}
                    dragSnapToOrigin
                    dragElastic={0.4}
                    onDragEnd={onDragEnd}
                    transition={layoutT}
                    className="pointer-events-auto max-h-full max-w-full cursor-grab touch-none select-none rounded-md object-contain active:cursor-grabbing"
                  />
                </AnimatePresence>
                {images.length > 1 && (
                  <>
                    <button type="button" aria-label={L.previous} onClick={() => step(-1)} className="pointer-events-auto absolute left-2 grid size-10 place-items-center rounded-full text-background outline-offset-2 hover:bg-white/10 focus-visible:outline-2 motion-safe:active:scale-[0.97] transition-[color,background-color,border-color,opacity,scale]">
                      <Icon d="M15 6l-6 6 6 6" />
                    </button>
                    <button type="button" aria-label={L.next} onClick={() => step(1)} className="pointer-events-auto absolute right-2 grid size-10 place-items-center rounded-full text-background outline-offset-2 hover:bg-white/10 focus-visible:outline-2 motion-safe:active:scale-[0.97] transition-[color,background-color,border-color,opacity,scale]">
                      <Icon d="M9 6l6 6-6 6" />
                    </button>
                  </>
                )}
              </div>
              <div className="relative min-h-12 px-4 py-3 text-center text-sm text-background/80">{current.caption}</div>
            </div>
          )}
        </AnimatePresence>
      );
    
      return (
        <LayoutGroup id={group}>
          <ul className={cn("grid", gridClassName, className)}>
            {images.map((image, i) => (
              <li key={image.id}>
                <button
                  ref={(el) => {
                    thumbRefs.current[i] = el;
                  }}
                  type="button"
                  aria-label={L.open(image.alt)}
                  onClick={() => set(i)}
                  className="block w-full overflow-hidden rounded-md outline-offset-2 focus-visible:outline-2 motion-safe:active:scale-[0.97] transition-[color,background-color,border-color,opacity,scale]"
                  style={{ aspectRatio: aspect }}
                >
                  {renderThumb ? (
                    renderThumb(image, i)
                  ) : (
                    <motion.img
                      layoutId={`${group}-${image.id}`}
                      src={image.thumbSrc ?? image.src}
                      alt=""
                      transition={layoutT}
                      className="size-full object-cover transition-opacity hover:opacity-90"
                      style={{ visibility: index === i ? "hidden" : "visible" }}
                    />
                  )}
                </button>
              </li>
            ))}
          </ul>
          {position === "fixed" ? (mounted ? createPortal(overlay, document.body) : null) : overlay}
        </LayoutGroup>
      );
    }