{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "infinite-scroll",
  "type": "registry:ui",
  "title": "InfiniteScroll",
  "description": "بارگذاری پیوسته",
  "dependencies": [
    "@kashi-ui/persian@^2.26.0"
  ],
  "registryDependencies": [
    "https://kashi.sepantapouya.com/r/cx.json",
    "https://kashi.sepantapouya.com/r/kashi-provider.json"
  ],
  "files": [
    {
      "path": "registry/kashi/InfiniteScroll.tsx",
      "type": "registry:ui",
      "target": "components/kashi/InfiniteScroll.tsx",
      "content": "\"use client\";\n\nimport { Children, isValidElement, useEffect, useRef, useState, type CSSProperties, type ReactNode } from \"react\";\nimport { cx } from \"./cx\";\nimport { useKashiLocale, useMessages } from \"./KashiProvider\";\nimport { faInfinite } from \"@kashi-ui/persian\";\n\nexport interface InfiniteScrollProps {\n  children: ReactNode;\n  /** هنوز موردی برای بارگذاری مانده است */\n  hasMore: boolean;\n  /** در حال بارگذاری صفحهٔ بعد */\n  loading?: boolean;\n  /** خطای آخرین بارگذاری؛ دکمهٔ «تلاش دوباره» نشان داده می‌شود */\n  error?: ReactNode | boolean;\n  /** بارگذاری صفحهٔ بعد؛ وقتی پایین فهرست دیده شود صدا زده می‌شود */\n  onLoadMore: () => void;\n  /** تعداد موردهای فعلی؛ برای اعلام «n مورد دیگر بارگذاری شد» به صفحه‌خوان */\n  itemCount?: number;\n  /** «auto» با رسیدن به پایین، «button» با دکمهٔ «نمایش بیشتر» (بهتر برای پاورقی و کیبورد) @default \"auto\" */\n  mode?: \"auto\" | \"button\";\n  /** فاصلهٔ پیش‌بارگذاری پیش از رسیدن به پایین @default \"320px\" */\n  rootMargin?: string;\n  /** ارتفاع بیشینه؛ با آن خود کامپوننت اسکرول می‌خورد (وگرنه صفحه) */\n  maxHeight?: string;\n  /** متن پایان فهرست؛ null برای پنهان */\n  endMessage?: ReactNode;\n  /** جای‌نگه‌دار هنگام بارگذاری؛ پیش‌فرض سه اسکلت */\n  loader?: ReactNode;\n  /** نقش فهرست: \"feed\" برای خبرخوان (فرزندان باید <article> باشند)؛ پیش‌فرض feed وقتی همهٔ فرزندان article‌اند، وگرنه بدون نقش */\n  role?: \"feed\" | \"list\" | \"none\";\n  label?: string;\n  className?: string;\n  style?: CSSProperties;\n}\n\n/**\n * بارگذاری پیوسته: با رسیدن به پایین (IntersectionObserver) صفحهٔ بعد را می‌خواهد، یا با دکمهٔ «نمایش بیشتر».\n * aria-busy و اعلام تعداد موردهای تازه برای صفحه‌خوان، خطا با «تلاش دوباره»، و پیام پایان.\n */\nexport function InfiniteScroll({\n  children, hasMore, loading, error, onLoadMore, itemCount, mode = \"auto\", rootMargin = \"320px\", maxHeight, endMessage, loader,\n  role: roleProp, label, className, style,\n}: InfiniteScrollProps) {\n  const kids = Children.toArray(children);\n  const role = roleProp ?? (kids.length > 0 && kids.every((c) => isValidElement(c) && c.type === \"article\") ? \"feed\" : \"none\");\n  const M = useMessages({ infinite: faInfinite });\n  const { n } = useKashiLocale();\n  const L = M.infinite;\n  const root = useRef<HTMLDivElement>(null);\n  const sentinel = useRef<HTMLDivElement>(null);\n  const prevCount = useRef(itemCount);\n  const [live, setLive] = useState(\"\");\n  const call = useRef(onLoadMore);\n  call.current = onLoadMore;\n  const blocked = !hasMore || loading || !!error;\n\n  useEffect(() => {\n    if (mode !== \"auto\" || blocked || typeof IntersectionObserver === \"undefined\") return;\n    const el = sentinel.current;\n    if (!el) return;\n    const io = new IntersectionObserver((entries) => {\n      if (entries.some((e) => e.isIntersecting)) call.current();\n    }, { root: maxHeight ? root.current : null, rootMargin });\n    io.observe(el);\n    return () => io.disconnect();\n  }, [mode, blocked, rootMargin, maxHeight]);\n\n  useEffect(() => {\n    if (itemCount === undefined) return;\n    const before = prevCount.current ?? 0;\n    if (itemCount > before && before > 0) setLive(L.loaded(n(itemCount - before)));\n    prevCount.current = itemCount;\n  }, [itemCount]);\n\n  return (\n    <div\n      ref={root}\n      className={cx(\"k-infinite\", maxHeight && \"k-infinite--scroll\", className)}\n      style={maxHeight ? { maxBlockSize: maxHeight, ...style } : style}\n      tabIndex={maxHeight ? 0 : undefined}\n      role={maxHeight ? \"region\" : undefined}\n      aria-label={maxHeight ? label : undefined}\n    >\n      <div className=\"k-infinite__items\" role={role === \"none\" ? undefined : role} aria-label={role === \"none\" ? undefined : label} aria-busy={loading || undefined}>\n        {children}\n      </div>\n      {loading && (loader ?? (\n        <div className=\"k-infinite__loader\" aria-hidden=\"true\">\n          {[0, 1, 2].map((i) => <span key={i} className=\"k-skeleton k-infinite__sk\" />)}\n        </div>\n      ))}\n      {loading && <span className=\"k-sr-only\" role=\"status\">{L.loading}</span>}\n      {error && !loading && (\n        <div className=\"k-infinite__error\" role=\"alert\">\n          <span>{error === true ? L.error : error}</span>\n          <button type=\"button\" className=\"k-btn k-btn--secondary k-btn--sm\" onClick={() => call.current()}>{L.retry}</button>\n        </div>\n      )}\n      {hasMore && !loading && !error && mode === \"button\" && (\n        <div className=\"k-infinite__more\">\n          <button type=\"button\" className=\"k-btn k-btn--secondary\" onClick={() => call.current()}>{L.loadMore}</button>\n        </div>\n      )}\n      {!hasMore && !loading && endMessage !== null && <p className=\"k-infinite__end\">{endMessage ?? L.end}</p>}\n      <div ref={sentinel} className=\"k-infinite__sentinel\" aria-hidden=\"true\" />\n      <span className=\"k-sr-only\" aria-live=\"polite\">{live}</span>\n    </div>\n  );\n}\n"
    }
  ]
}