{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "lightbox",
  "type": "registry:ui",
  "title": "Lightbox، Gallery",
  "description": "گالری تصاویر",
  "dependencies": [
    "@kashi-ui/persian@^2.26.0"
  ],
  "registryDependencies": [
    "https://kashi.sepantapouya.com/r/cx.json",
    "https://kashi.sepantapouya.com/r/dnd.json",
    "https://kashi.sepantapouya.com/r/kashi-provider.json",
    "https://kashi.sepantapouya.com/r/modal.json"
  ],
  "files": [
    {
      "path": "registry/kashi/Lightbox.tsx",
      "type": "registry:ui",
      "target": "components/kashi/Lightbox.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useId, useRef, useState, type CSSProperties, type KeyboardEvent } from \"react\";\nimport { cx } from \"./cx\";\nimport { isRtl } from \"./dnd\";\nimport { useKashiLocale, useMessages } from \"./KashiProvider\";\nimport { faLightbox } from \"@kashi-ui/persian\";\nimport { useModalDialog } from \"./modal\";\n\nexport interface LightboxImage {\n  src: string;\n  /** متن جایگزین؛ برای صفحه‌خوان و برچسب دکمهٔ تصویر کوچک */\n  alt: string;\n  /** زیرنویس زیر تصویر بزرگ */\n  caption?: string;\n  /** تصویر کوچک گالری؛ بدون آن همان src */\n  thumb?: string;\n  width?: number;\n  height?: number;\n}\n\nexport interface LightboxProps {\n  images: LightboxImage[];\n  /** اندیس تصویر باز؛ null یعنی بسته */\n  index: number | null;\n  onIndexChange: (index: number | null) => void;\n  /** نوار تصویرهای کوچک پایین @default true */\n  thumbnails?: boolean;\n  /** از آخر به اول برگردد @default false */\n  loop?: boolean;\n  className?: string;\n}\n\nconst svg = (d: string, w = 1.8) => (\n  <svg viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth={w} strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\"><path d={d} /></svg>\n);\n\n/**\n * نمایش تمام‌صفحهٔ تصویر روی <dialog> بومی: فلش‌ها (در راست‌به‌چپ، چپ یعنی بعدی)، Home و End، کشیدن انگشت،\n * بزرگ‌نمایی با کلیک یا دوبار ضربه، و برگشت فوکوس به تصویر کوچک پس از بستن.\n */\nexport function Lightbox({ images, index, onIndexChange, thumbnails = true, loop = false, className }: LightboxProps) {\n  const M = useMessages({ lightbox: faLightbox });\n  const { n } = useKashiLocale();\n  const L = M.lightbox;\n  const ref = useRef<HTMLDialogElement>(null);\n  const open = index !== null;\n  const { restoreFocus } = useModalDialog(ref, open);\n  const [zoom, setZoom] = useState<{ x: number; y: number } | null>(null);\n  const [dir, setDir] = useState<1 | -1>(1);\n  const swipe = useRef<{ x: number; y: number; id: number } | null>(null);\n  const [dx, setDx] = useState(0);\n  const titleId = useId();\n  const last = useRef(0);\n  if (index !== null) last.current = index;\n  const i = index ?? last.current;\n  const img = images[i];\n  const count = images.length;\n\n  const go = (to: number) => {\n    if (!count) return;\n    const next = loop ? (to + count) % count : Math.max(0, Math.min(count - 1, to));\n    if (next === i) return;\n    setDir(next > i ? 1 : -1);\n    setZoom(null);\n    onIndexChange(next);\n  };\n\n  // پیش‌بارگذاری تصویرهای کناری\n  useEffect(() => {\n    if (!open) return;\n    for (const j of [i - 1, i + 1]) {\n      const s = images[(j + count) % count]?.src;\n      if (s && typeof Image === \"function\") new Image().src = s;\n    }\n  }, [open, i, images, count]);\n\n  useEffect(() => {\n    if (!open) setZoom(null);\n  }, [open]);\n\n  // تصویر کوچک فعلی در نوار دیده شود\n  useEffect(() => {\n    if (!open) return;\n    ref.current?.querySelector<HTMLElement>(`[data-thumb=\"${i}\"]`)?.scrollIntoView?.({ block: \"nearest\", inline: \"center\" });\n  }, [open, i]);\n\n  const onKey = (e: KeyboardEvent) => {\n    const rtl = isRtl(ref.current);\n    if (e.key === \"ArrowRight\") go(i + (rtl ? -1 : 1));\n    else if (e.key === \"ArrowLeft\") go(i + (rtl ? 1 : -1));\n    else if (e.key === \"Home\") go(0);\n    else if (e.key === \"End\") go(count - 1);\n    else return;\n    e.preventDefault();\n  };\n\n  const canPrev = loop || i > 0;\n  const canNext = loop || i < count - 1;\n\n  return (\n    <dialog\n      ref={ref}\n      className={cx(\"k-lightbox\", className)}\n      aria-labelledby={titleId}\n      onKeyDown={onKey}\n      onClose={() => {\n        if (open) onIndexChange(null);\n        restoreFocus();\n      }}\n      onCancel={(e) => {\n        e.preventDefault();\n        if (zoom) setZoom(null);\n        else onIndexChange(null);\n      }}\n      onClick={(e) => {\n        if (e.target === e.currentTarget || (e.target as HTMLElement).classList.contains(\"k-lightbox__stage\")) onIndexChange(null);\n      }}\n    >\n      <h2 className=\"k-sr-only\" id={titleId}>{L.label}</h2>\n      <div className=\"k-lightbox__bar\">\n        <span className=\"k-lightbox__counter\" aria-live=\"polite\">{L.counter(n(i + 1), n(count))}</span>\n        <button type=\"button\" className=\"k-lightbox__btn\" aria-label={zoom ? L.zoomOut : L.zoomIn} aria-pressed={!!zoom} onClick={() => setZoom(zoom ? null : { x: 50, y: 50 })}>\n          {zoom ? svg(\"M8.5 14a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11ZM17 17l-4.2-4.2M6 8.5h5\") : svg(\"M8.5 14a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11ZM17 17l-4.2-4.2M6 8.5h5M8.5 6v5\")}\n        </button>\n        <button type=\"button\" className=\"k-lightbox__btn\" aria-label={L.close} onClick={() => onIndexChange(null)} autoFocus>\n          {svg(\"M5 5l10 10M15 5 5 15\")}\n        </button>\n      </div>\n      <div\n        className=\"k-lightbox__stage\"\n        onPointerDown={(e) => {\n          if (zoom || (e.target as HTMLElement).closest(\"button\")) return;\n          swipe.current = { x: e.clientX, y: e.clientY, id: e.pointerId };\n        }}\n        onPointerMove={(e) => swipe.current?.id === e.pointerId && setDx(e.clientX - swipe.current.x)}\n        onPointerUp={(e) => {\n          const s = swipe.current;\n          swipe.current = null;\n          setDx(0);\n          if (!s || s.id !== e.pointerId) return;\n          const d = e.clientX - s.x;\n          if (Math.abs(d) > 60 && Math.abs(d) > Math.abs(e.clientY - s.y)) {\n            // کشیدن به سمت شروع خط یعنی بعدی\n            const rtl = isRtl(ref.current);\n            go(i + ((d < 0) !== rtl ? 1 : -1));\n          }\n        }}\n        onPointerCancel={() => {\n          swipe.current = null;\n          setDx(0);\n        }}\n      >\n        {img && (\n          <figure className=\"k-lightbox__figure\" key={i} data-dir={dir > 0 ? \"next\" : \"prev\"} style={dx ? ({ translate: `${dx}px 0`, transition: \"none\" } as CSSProperties) : undefined}>\n            <img\n              className=\"k-lightbox__img\"\n              src={img.src}\n              alt={img.alt}\n              width={img.width}\n              height={img.height}\n              draggable={false}\n              data-zoom={zoom ? \"\" : undefined}\n              style={zoom ? ({ transformOrigin: `${zoom.x}% ${zoom.y}%` } as CSSProperties) : undefined}\n              onClick={(e) => {\n                if (Math.abs(dx) > 4) return;\n                const r = e.currentTarget.getBoundingClientRect();\n                setZoom(zoom ? null : { x: ((e.clientX - r.left) / r.width) * 100, y: ((e.clientY - r.top) / r.height) * 100 });\n              }}\n              onPointerMove={(e) => {\n                if (!zoom || e.pointerType === \"touch\") return;\n                const r = e.currentTarget.getBoundingClientRect();\n                setZoom({ x: ((e.clientX - r.left) / r.width) * 100, y: ((e.clientY - r.top) / r.height) * 100 });\n              }}\n            />\n            {img.caption && <figcaption className=\"k-lightbox__caption\" dir=\"auto\">{img.caption}</figcaption>}\n          </figure>\n        )}\n        {count > 1 && (\n          <>\n            <button type=\"button\" className=\"k-lightbox__nav k-lightbox__nav--prev\" aria-label={L.prev} disabled={!canPrev} onClick={() => go(i - 1)}>\n              {svg(\"m8 5 5 5-5 5\")}\n            </button>\n            <button type=\"button\" className=\"k-lightbox__nav k-lightbox__nav--next\" aria-label={L.next} disabled={!canNext} onClick={() => go(i + 1)}>\n              {svg(\"m12 5-5 5 5 5\")}\n            </button>\n          </>\n        )}\n      </div>\n      {thumbnails && count > 1 && (\n        <div className=\"k-lightbox__thumbs\">\n          {images.map((t, j) => (\n            <button key={j} type=\"button\" className=\"k-lightbox__thumb\" data-thumb={j} aria-label={t.alt} aria-current={j === i ? \"true\" : undefined} onClick={() => go(j)}>\n              <img src={t.thumb ?? t.src} alt=\"\" loading=\"lazy\" draggable={false} />\n            </button>\n          ))}\n        </div>\n      )}\n    </dialog>\n  );\n}\n\nexport interface GalleryProps {\n  images: LightboxImage[];\n  /** تعداد ستون؛ بدون آن خودکار بر اساس عرض */\n  columns?: number;\n  /** نسبت تصویرهای کوچک @default \"1 / 1\" */\n  aspect?: string;\n  /** ستون‌های بیشتر از این تعداد با «+n» جمع می‌شوند */\n  max?: number;\n  loop?: boolean;\n  \"aria-label\"?: string;\n  className?: string;\n}\n\n/** شبکهٔ تصویرهای کوچک که هر کدام با کلیک در نمایش تمام‌صفحه باز می‌شود */\nexport function Gallery({ images, columns, aspect = \"1 / 1\", max, loop, \"aria-label\": ariaLabel, className }: GalleryProps) {\n  const M = useMessages({ lightbox: faLightbox });\n  const { n } = useKashiLocale();\n  const [open, setOpen] = useState<number | null>(null);\n  const shown = max && images.length > max ? images.slice(0, max) : images;\n  const rest = images.length - shown.length;\n  return (\n    <>\n      <ul className={cx(\"k-gallery\", className)} aria-label={ariaLabel ?? M.lightbox.gallery} style={{ ...(columns ? { \"--k-gallery-cols\": columns } : {}), \"--k-gallery-aspect\": aspect } as CSSProperties}>\n        {shown.map((img, j) => (\n          <li key={j}>\n            <button type=\"button\" className=\"k-gallery__item\" aria-label={rest > 0 && j === shown.length - 1 ? `${M.lightbox.open(img.alt)} +${n(rest)}` : M.lightbox.open(img.alt)} onClick={() => setOpen(j)}>\n              <img src={img.thumb ?? img.src} alt=\"\" loading=\"lazy\" width={img.width} height={img.height} />\n              {rest > 0 && j === shown.length - 1 && <span className=\"k-gallery__more\" aria-hidden=\"true\">+{n(rest)}</span>}\n            </button>\n          </li>\n        ))}\n      </ul>\n      <Lightbox images={images} index={open} onIndexChange={setOpen} loop={loop} />\n    </>\n  );\n}\n"
    }
  ]
}