{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "image-viewer",
  "title": "Image Viewer",
  "type": "registry:ui",
  "files": [
    {
      "path": "packages/registry-native/components/image-viewer/ImageViewer.tsx",
      "content": "import * as React from 'react'\nimport { Modal, Pressable, View, type ViewProps } from 'react-native'\nimport { Gesture, GestureDetector, GestureHandlerRootView } from 'react-native-gesture-handler'\nimport { useReducedMotion } from 'react-native-reanimated'\nimport { useSafeAreaInsets } from 'react-native-safe-area-context'\nimport { Button, type ButtonProps } from '@/components/ui/button'\nimport { Carousel, CarouselContent, CarouselItem } from '@/components/ui/carousel'\nimport { Text, type TextProps } from '@/components/ui/text'\nimport { ZoomView, type ZoomViewHandle } from '@/components/ui/zoom-view'\nimport { Overlay, OverlayClose, OverlayTrigger, useOverlay, type OverlayProps } from '@/lib/overlay'\nimport { PortalHost, PortalScope, useModalPortalScope } from '@/lib/portal'\nimport { cn } from '@/lib/utils'\n\nexport interface ImageViewerProps extends OverlayProps {\n  index?: number\n  defaultIndex?: number\n  onIndexChange?: (index: number) => void\n  disabled?: boolean\n}\n\ninterface ViewerState {\n  index: number\n  ids: string[]\n  disabled: boolean\n  scale: number\n  session: object\n  zooms: React.RefObject<Map<string, ZoomViewHandle>>\n  register: (ids: string[]) => void\n  go: (index: number) => void\n  setScale: (id: string, session: object, scale: number) => void\n}\nconst ViewerContext = React.createContext<ViewerState | null>(null)\nfunction useViewer() {\n  const value = React.useContext(ViewerContext)\n  if (!value) throw new Error('ImageViewer parts must be inside ImageViewer')\n  return value\n}\nfunction clampIndex(index: number, count: number) {\n  return Math.max(0, Math.min(Math.max(0, count - 1), Number.isFinite(index) ? Math.trunc(index) : 0))\n}\n\nfunction ViewerStateProvider({\n  index,\n  defaultIndex = 0,\n  onIndexChange,\n  disabled = false,\n  children,\n}: Pick<ImageViewerProps, 'index' | 'defaultIndex' | 'onIndexChange' | 'disabled' | 'children'>) {\n  const { open } = useOverlay()\n  const [internal, setInternal] = React.useState(defaultIndex)\n  const [ids, setIds] = React.useState<string[]>([])\n  const [zoom, setZoom] = React.useState<{ session: object; scale: number } | null>(null)\n  const zooms = React.useRef(new Map<string, ZoomViewHandle>())\n  const currentIndex = clampIndex(index ?? internal, ids.length)\n  const id = ids[currentIndex]\n  const session = React.useMemo(() => ({ open, id }), [open, id])\n  const active = React.useRef({ open, id, session })\n  active.current = { open, id, session }\n  const register = React.useCallback(\n    (next: string[]) => {\n      setIds((previous) =>\n        previous.length === next.length && previous.every((value, at) => value === next[at]) ? previous : next,\n      )\n      if (index === undefined) setInternal((value) => clampIndex(value, next.length))\n    },\n    [index],\n  )\n  const setScale = React.useCallback((key: string, owner: object, next: number) => {\n    if (!active.current.open || active.current.id !== key || active.current.session !== owner || !Number.isFinite(next))\n      return\n    setZoom({ session: owner, scale: Math.max(1, next) })\n  }, [])\n  const go = (next: number) => {\n    if (disabled || !ids.length) return\n    const value = clampIndex(next, ids.length)\n    if (value === currentIndex) return\n    if (index === undefined) setInternal(value)\n    onIndexChange?.(value)\n  }\n  return (\n    <ViewerContext.Provider\n      value={{\n        index: currentIndex,\n        ids,\n        disabled,\n        session,\n        zooms,\n        register,\n        go,\n        setScale,\n        scale: zoom?.session === session ? zoom.scale : 1,\n      }}\n    >\n      {children}\n    </ViewerContext.Provider>\n  )\n}\n\n/** Fullscreen media mechanics. The consumer owns every image, caption and control composition. */\nexport function ImageViewer({ open, defaultOpen, onOpenChange, ...props }: ImageViewerProps) {\n  return (\n    <Overlay open={open} defaultOpen={defaultOpen} onOpenChange={onOpenChange}>\n      <ViewerStateProvider {...props} />\n    </Overlay>\n  )\n}\n\nexport const ImageViewerTrigger = React.forwardRef<React.ComponentRef<typeof Pressable>, ButtonProps>((props, ref) => {\n  const viewer = useViewer()\n  return <OverlayTrigger {...props} ref={ref} disabled={viewer.disabled || props.disabled} />\n})\nImageViewerTrigger.displayName = 'ImageViewerTrigger'\nexport { OverlayClose as ImageViewerClose }\n\nexport interface ImageViewerContentProps extends ViewProps {\n  title: string\n  closeLabel?: string\n  className?: string\n}\nexport const ImageViewerContent = React.forwardRef<React.ComponentRef<typeof View>, ImageViewerContentProps>(\n  ({ title, closeLabel = 'Close', children, className, style, ...props }, ref) => {\n    const { open, setOpen } = useOverlay()\n    const insets = useSafeAreaInsets()\n    const reduced = useReducedMotion()\n    const scope = useModalPortalScope()\n    if (!open) return null\n    return (\n      <Modal\n        visible\n        animationType={reduced ? 'none' : 'fade'}\n        presentationStyle=\"fullScreen\"\n        supportedOrientations={['portrait', 'portrait-upside-down', 'landscape-left', 'landscape-right']}\n        statusBarTranslucent\n        navigationBarTranslucent\n        onRequestClose={() => {\n          if (!scope.dismissTopmost()) setOpen(false)\n        }}\n      >\n        <PortalScope scope={scope}>\n          <GestureHandlerRootView style={{ flex: 1 }}>\n            <View ref={scope.containerRef} style={{ flex: 1 }}>\n              <View\n                {...props}\n                ref={ref}\n                className={cn('flex-1 gap-[12px] bg-background', className)}\n                style={[\n                  {\n                    paddingTop: insets.top,\n                    paddingBottom: insets.bottom + 12,\n                    paddingLeft: insets.left + 16,\n                    paddingRight: insets.right + 16,\n                  },\n                  style,\n                ]}\n                accessibilityViewIsModal\n                onAccessibilityEscape={() => setOpen(false)}\n              >\n                <View className=\"flex-row items-center gap-[12px]\">\n                  <Text accessibilityRole=\"header\" className=\"min-w-0 flex-1 font-semibold\">\n                    {title}\n                  </Text>\n                  <OverlayClose variant=\"outline\">{closeLabel}</OverlayClose>\n                </View>\n                {children}\n              </View>\n              <PortalHost name={scope.hostName} />\n            </View>\n          </GestureHandlerRootView>\n        </PortalScope>\n      </Modal>\n    )\n  },\n)\nImageViewerContent.displayName = 'ImageViewerContent'\n\ninterface PageState {\n  id: string\n  index: number\n  height: number\n  scrollGesture: ReturnType<typeof Gesture.Native>\n}\nconst PageContext = React.createContext<PageState | null>(null)\n\nfunction itemElements(children: React.ReactNode): React.ReactElement<ImageViewerItemProps>[] {\n  return React.Children.toArray(children).flatMap((child) => {\n    if (React.isValidElement<{ children?: React.ReactNode }>(child) && child.type === React.Fragment)\n      return itemElements(child.props.children)\n    // Fast Refresh preserves mounted children while replacing the exported forwardRef object.\n    const isItem =\n      React.isValidElement(child) &&\n      (child.type === ImageViewerItem ||\n        (typeof child.type === 'object' && (child.type as { displayName?: string }).displayName === 'ImageViewerItem'))\n    if (!React.isValidElement<ImageViewerItemProps>(child) || !isItem)\n      throw new Error('ImageViewerItems accepts ImageViewerItem children or fragments')\n    if (!child.props.id) throw new Error('ImageViewerItem requires a stable nonempty id')\n    return [child]\n  })\n}\n\nexport const ImageViewerItems = React.forwardRef<React.ComponentRef<typeof View>, ViewProps & { className?: string }>(\n  ({ children, className, style, onLayout, ...props }, ref) => {\n    const viewer = useViewer()\n    const [height, setHeight] = React.useState(0)\n    const scrollGesture = React.useMemo(() => Gesture.Native(), [])\n    const items = itemElements(children)\n    const ids = items.map((item) => item.props.id)\n    if (new Set(ids).size !== ids.length) throw new Error('ImageViewerItem ids must be unique')\n    const { register } = viewer\n    const idsKey = JSON.stringify(ids)\n    React.useLayoutEffect(() => {\n      register(JSON.parse(idsKey) as string[])\n    }, [register, idsKey])\n    return (\n      <View\n        {...props}\n        ref={ref}\n        className={cn('flex-1 overflow-hidden', className)}\n        style={style}\n        onLayout={(event) => {\n          setHeight(Math.max(0, event.nativeEvent.layout.height))\n          onLayout?.(event)\n        }}\n      >\n        <Carousel index={viewer.index} onIndexChange={viewer.go} disabled={viewer.disabled} style={{ flex: 1 }}>\n          <GestureDetector gesture={scrollGesture}>\n            <CarouselContent style={{ height, flexGrow: 0 }} scrollEnabled={viewer.scale <= 1}>\n              {items.map((item, index) => (\n                <PageContext.Provider key={item.props.id} value={{ id: item.props.id, index, height, scrollGesture }}>\n                  {item}\n                </PageContext.Provider>\n              ))}\n            </CarouselContent>\n          </GestureDetector>\n        </Carousel>\n      </View>\n    )\n  },\n)\nImageViewerItems.displayName = 'ImageViewerItems'\n\nexport interface ImageViewerItemProps extends ViewProps {\n  id: string\n  maxScale?: number\n  className?: string\n}\nexport const ImageViewerItem = React.forwardRef<React.ComponentRef<typeof View>, ImageViewerItemProps>(\n  ({ id, maxScale = 4, children, className, style, ...props }, ref) => {\n    const viewer = useViewer()\n    const page = React.useContext(PageContext)\n    if (!page) throw new Error('ImageViewerItem must be inside ImageViewerItems')\n    const ownHandle = React.useRef<ZoomViewHandle | null>(null)\n    const { zooms } = viewer\n    const setZoomRef = React.useCallback(\n      (handle: ZoomViewHandle | null) => {\n        if (handle) zooms.current.set(id, handle)\n        else if (zooms.current.get(id) === ownHandle.current) zooms.current.delete(id)\n        ownHandle.current = handle\n      },\n      [id, zooms],\n    )\n    return (\n      <CarouselItem\n        {...props}\n        ref={ref}\n        className={cn('overflow-hidden', className)}\n        style={[{ height: page.height }, style]}\n      >\n        <ZoomView\n          key={`${id}:${viewer.index}`}\n          ref={setZoomRef}\n          scrollGesture={page.scrollGesture}\n          style={{ height: page.height }}\n          maxScale={maxScale}\n          disabled={viewer.disabled || page.index !== viewer.index}\n          onScaleChange={(scale) => viewer.setScale(id, viewer.session, scale)}\n        >\n          {children}\n        </ZoomView>\n      </CarouselItem>\n    )\n  },\n)\nImageViewerItem.displayName = 'ImageViewerItem'\n\ntype ViewerAction = 'previous' | 'next' | 'in' | 'out' | 'reset'\nfunction actionButton(name: string, action: ViewerAction, label: string) {\n  const Component = React.forwardRef<React.ComponentRef<typeof Pressable>, ButtonProps>(\n    ({ children = label, disabled, loading, onPress, ...props }, ref) => {\n      const viewer = useViewer()\n      const blocked =\n        viewer.disabled ||\n        disabled ||\n        loading ||\n        !viewer.ids.length ||\n        (action === 'previous' && viewer.index === 0) ||\n        (action === 'next' && viewer.index === viewer.ids.length - 1)\n      return (\n        <Button\n          variant=\"outline\"\n          {...props}\n          ref={ref}\n          disabled={blocked}\n          loading={loading}\n          onPress={(event) => {\n            if (blocked) return\n            onPress?.(event)\n            if (event.isDefaultPrevented()) return\n            if (action === 'previous' || action === 'next') viewer.go(viewer.index + (action === 'next' ? 1 : -1))\n            else {\n              const handle = viewer.zooms.current.get(viewer.ids[viewer.index])\n              if (action === 'in') handle?.zoomIn()\n              else if (action === 'out') handle?.zoomOut()\n              else handle?.reset()\n            }\n          }}\n        >\n          {children}\n        </Button>\n      )\n    },\n  )\n  Component.displayName = name\n  return Component\n}\nexport const ImageViewerPrevious = actionButton('ImageViewerPrevious', 'previous', 'Previous image')\nexport const ImageViewerNext = actionButton('ImageViewerNext', 'next', 'Next image')\nexport const ImageViewerZoomIn = actionButton('ImageViewerZoomIn', 'in', 'Zoom in')\nexport const ImageViewerZoomOut = actionButton('ImageViewerZoomOut', 'out', 'Zoom out')\nexport const ImageViewerResetZoom = actionButton('ImageViewerResetZoom', 'reset', 'Reset zoom')\nexport const ImageViewerStatus = React.forwardRef<React.ComponentRef<typeof Text>, TextProps>((props, ref) => {\n  const viewer = useViewer()\n  return (\n    <Text {...props} ref={ref}>\n      {props.children ?? (viewer.ids.length ? `${viewer.index + 1} / ${viewer.ids.length}` : 'No images')}\n    </Text>\n  )\n})\nImageViewerStatus.displayName = 'ImageViewerStatus'\n",
      "type": "registry:ui",
      "target": "~/components/ui/image-viewer/ImageViewer.tsx"
    },
    {
      "path": "packages/registry-native/components/image-viewer/index.ts",
      "content": "export * from './ImageViewer'\n",
      "type": "registry:ui",
      "target": "~/components/ui/image-viewer/index.ts"
    }
  ],
  "dependencies": [
    "react-native-gesture-handler",
    "react-native-reanimated",
    "react-native-safe-area-context"
  ],
  "devDependencies": [],
  "registryDependencies": [
    "https://native.uipkge.dev/r/native/button.json",
    "https://native.uipkge.dev/r/native/carousel.json",
    "https://native.uipkge.dev/r/native/text.json",
    "https://native.uipkge.dev/r/native/zoom-view.json",
    "https://native.uipkge.dev/r/native/overlay.json",
    "https://native.uipkge.dev/r/native/portal.json",
    "https://native.uipkge.dev/r/native/utils.json"
  ],
  "description": "Caller-composed fullscreen media with coordinated paging, zoom controls and modal dismissal.",
  "categories": [
    "media"
  ]
}