{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "portal",
  "title": "Portal",
  "type": "registry:lib",
  "files": [
    {
      "path": "packages/registry-native/bootstrap/portal/index.tsx",
      "content": "import * as React from 'react'\nimport { BackHandler, Platform, StyleSheet, View } from 'react-native'\nimport { FullWindowOverlay } from 'react-native-screens'\n\nexport { Portal, PortalHost } from '@rn-primitives/portal'\n\ninterface PopupScope {\n  hostName?: string\n  container?: React.ComponentRef<typeof View> | null\n  containerRef?: React.RefCallback<React.ComponentRef<typeof View>>\n  register: (id: symbol, root: symbol, dismiss: () => void, depth: number) => () => void\n  dismissTopmost: () => boolean\n}\n\ninterface PopupLayerValue {\n  hostName?: string\n  container?: React.ComponentRef<typeof View> | null\n  scope: PopupScope\n  depth: number\n  open?: boolean\n  root?: symbol\n}\n\nfunction createPopupScope(hostName?: string): PopupScope {\n  const entries = new Map<symbol, { dismiss: () => void; depth: number; order: number; root: symbol }>()\n  let order = 0\n  return {\n    hostName,\n    register(id, root, dismiss, depth) {\n      entries.set(id, { dismiss, depth, order: ++order, root })\n      return () => {\n        entries.delete(id)\n      }\n    },\n    dismissTopmost() {\n      let top: { dismiss: () => void; depth: number; order: number; root: symbol } | undefined\n      for (const entry of entries.values()) {\n        const rootOrder = entries.get(entry.root)?.order ?? entry.order\n        const topRootOrder = top ? (entries.get(top.root)?.order ?? top.order) : -1\n        if (\n          !top ||\n          rootOrder > topRootOrder ||\n          (rootOrder === topRootOrder &&\n            (entry.depth > top.depth || (entry.depth === top.depth && entry.order > top.order)))\n        )\n          top = entry\n      }\n      if (!top) return false\n      top.dismiss()\n      return true\n    },\n  }\n}\n\nconst rootScope = createPopupScope()\nconst PopupContext = React.createContext<PopupLayerValue>({ scope: rootScope, depth: 0 })\n\n/** Every native Modal owns a host because a root-window portal sits behind it on Android. */\nexport function useModalPortalScope() {\n  const id = React.useId()\n  const scope = React.useMemo(() => createPopupScope(`uipkge-modal-${id}`), [id])\n  const [container, setContainer] = React.useState<React.ComponentRef<typeof View> | null>(null)\n  return React.useMemo(\n    () => ({ ...scope, container, containerRef: Platform.OS === 'web' ? setContainer : undefined }),\n    [scope, container],\n  )\n}\n\nexport function PortalScope({ scope, children }: { scope: PopupScope; children: React.ReactNode }) {\n  const value = React.useMemo(() => ({ scope, hostName: scope.hostName, depth: 0 }), [scope])\n  return <PopupContext.Provider value={value}>{children}</PopupContext.Provider>\n}\n\n/** Restore popup ancestry after native portals move content to their host. */\nexport function PopupLayer({ layer, children }: { layer: PopupLayerValue; children: React.ReactNode }) {\n  React.useEffect(() => {\n    if (!layer.open || layer.hostName || Platform.OS !== 'android') return\n    // This boundary mounts at the portal host, after the headless Content's Back subscription.\n    const back = BackHandler.addEventListener('hardwareBackPress', () => layer.scope.dismissTopmost())\n    return () => back.remove()\n  }, [layer.open, layer.hostName, layer.scope])\n  return <PopupContext.Provider value={layer}>{children}</PopupContext.Provider>\n}\n\nexport function usePopupDismiss(open: boolean, dismiss: () => void): PopupLayerValue {\n  const parent = React.useContext(PopupContext)\n  const [id] = React.useState(() => Symbol())\n  const latest = React.useRef(dismiss)\n  latest.current = dismiss\n  const depth = parent.depth + 1\n  const scope = parent.scope\n  const root = parent.root ?? id\n  const native = Platform.OS !== 'web'\n  React.useEffect(() => {\n    if (!open || !native) return\n    const unregister = scope.register(id, root, () => latest.current(), depth)\n    return unregister\n  }, [open, native, scope, depth, id, root])\n  return React.useMemo(\n    () => ({ scope, depth, hostName: scope.hostName, container: scope.container, open, root }),\n    [scope, depth, open, root],\n  )\n}\n\n/** Keep native popup content above navigator-owned screens and headers on iOS. */\nexport function PortalWindow({ children, layer }: { children: React.ReactNode; layer?: PopupLayerValue }) {\n  const parent = React.useContext(PopupContext)\n  const content = layer ? <PopupLayer layer={layer}>{children}</PopupLayer> : children\n  // One native container keeps popup content and its backdrop in the same iOS hit-test hierarchy.\n  return Platform.OS === 'ios' && !(layer ?? parent).hostName ? (\n    <FullWindowOverlay>\n      <View collapsable={false} pointerEvents=\"box-none\" style={StyleSheet.absoluteFill}>\n        {content}\n      </View>\n    </FullWindowOverlay>\n  ) : (\n    <>{content}</>\n  )\n}\n",
      "type": "registry:lib",
      "target": "~/lib/portal.tsx"
    }
  ],
  "dependencies": [
    "@rn-primitives/portal",
    "react-native-screens"
  ],
  "devDependencies": [],
  "registryDependencies": [],
  "description": "Portal host and native window overlay for layered controls above navigator-owned screens."
}