{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "geo-tiles",
  "type": "registry:component",
  "title": "Geo Tiles",
  "description": "A geo-map layer of raster map tiles from an address template the project gives (\"…/{z}/{x}/{y}.png\"), placed under the other layers and following the zoom, with the attribution the tile provider asks for. It has no default address: nothing is fetched unless the project names a server.",
  "categories": [
    "viz"
  ],
  "dependencies": [],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/cn",
    "@rivier-ui/geo-map"
  ],
  "files": [
    {
      "path": "registry/viz/geo-tiles.tsx",
      "type": "registry:component",
      "target": "components/viz/geo-tiles.tsx",
      "content": "\"use client\";\n/**\n * @name geo-tiles\n * @category viz\n * @description A geo-map layer of raster map tiles from an address template the project gives (\"…/{z}/{x}/{y}.png\"), placed under the other layers and following the zoom, with the attribution the tile provider asks for. It has no default address: nothing is fetched unless the project names a server.\n * @when A geo-map needs a picture of streets or terrain under the data and the project has a tile server it may use (its own, or a provider whose terms it accepts). Use projection=\"mercator\" (tiles are drawn in Web Mercator). Tiles come from a third party, so put the map in layout/map-frame with a MapFrameNotice and nothing is requested before the person asks. Without tiles, draw shapes with GeoFeatures.\n * @example\n * <GeoMap label=\"Delivery area\" fit={area} projection=\"mercator\">\n *   <GeoTiles url=\"https://tiles.example.org/{z}/{x}/{y}.png\" attribution=\"© Example contributors\" />\n *   <GeoFeatures data={area} variant=\"line\" />\n * </GeoMap>\n */\nimport { useEffect, type ComponentProps, type ReactNode } from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { cn } from \"@/lib/cn\";\nimport { useGeoMap } from \"@/components/viz/geo-map\";\n\nexport type GeoTilesProps = Omit<ComponentProps<\"g\">, \"children\"> & {\n  /** The tile address, with {z}, {x} and {y}, and {s} for a subdomain. No default: the project names its server. */\n  url: string;\n  /** What the provider asks to be shown: words, or a link. Required: tiles are someone's work. */\n  attribution: ReactNode;\n  /** Subdomains {s} rotates through: [\"a\", \"b\", \"c\"]. */\n  subdomains?: readonly string[];\n  /** The tile zooms the server has. Default 0 to 19. */\n  minZoom?: number;\n  maxZoom?: number;\n  /** The tiles' size in pixels at their own zoom. Default 256. */\n  tileSize?: number;\n  /** How strongly the tiles show, 0 to 1. Default 1; lower it so data layers read over a busy map. */\n  opacity?: number;\n};\n\n/** The edge of the Web Mercator square, in degrees of latitude. */\nconst MAX_LATITUDE = 85.0511287798066;\n\nexport function GeoTiles({ url, attribution, subdomains, minZoom = 0, maxZoom = 19, tileSize = 256, opacity = 1, className, ...props }: GeoTilesProps) {\n  const { projection, width, height, overlay } = useGeoMap(\"GeoTiles\");\n  const rotation = projection.rotate();\n  const turned = rotation.some((angle) => Math.abs(angle) > 1e-6);\n  useEffect(() => {\n    if (turned) console.warn(\"GeoTiles: the map is rotated, and tiles are only drawn on an unrotated Mercator projection.\");\n  }, [turned]);\n  const left = projection([-180, 0]);\n  const top = projection([0, MAX_LATITUDE]);\n  const right = projection([180, 0]);\n  if (!left || !top || !right || turned) return null;\n  const world = right[0] - left[0];\n  if (!(world > 0)) return null;\n  const zoom = Math.max(minZoom, Math.min(maxZoom, Math.round(Math.log2(world / tileSize))));\n  const count = 2 ** zoom;\n  const size = world / count;\n  const tiles: { key: string; href: string; x: number; y: number }[] = [];\n  const firstColumn = Math.floor((0 - left[0]) / size);\n  const lastColumn = Math.floor((width - left[0]) / size);\n  const firstRow = Math.max(0, Math.floor((0 - top[1]) / size));\n  const lastRow = Math.min(count - 1, Math.floor((height - top[1]) / size));\n  for (let column = firstColumn; column <= lastColumn; column++) {\n    for (let row = firstRow; row <= lastRow; row++) {\n      // Columns wrap round the world; rows do not.\n      const x = ((column % count) + count) % count;\n      const sub = subdomains && subdomains.length > 0 ? (subdomains[(x + row) % subdomains.length] ?? \"\") : \"\";\n      const href = url.replaceAll(\"{z}\", String(zoom)).replaceAll(\"{x}\", String(x)).replaceAll(\"{y}\", String(row)).replaceAll(\"{s}\", sub);\n      tiles.push({ key: `${zoom}/${column}/${row}`, href, x: left[0] + column * size, y: top[1] + row * size });\n    }\n  }\n  return (\n    <>\n      <g data-slot=\"geo-tiles\" aria-hidden=\"true\" opacity={opacity} className={cn(\"pointer-events-none\", className)} {...props}>\n        {tiles.map((tile) => (\n          // A pixel more each way, so no hairline of background shows between tiles.\n          <image key={tile.key} href={tile.href} x={tile.x - 0.5} y={tile.y - 0.5} width={size + 1} height={size + 1} preserveAspectRatio=\"none\" />\n        ))}\n      </g>\n      {overlay\n        ? createPortal(\n            <div\n              data-slot=\"geo-tiles-attribution\"\n              className=\"pointer-events-auto absolute bottom-0 end-0 max-w-[70%] truncate rounded-ss-control bg-background/85 px-1.5 py-0.5 text-xs text-muted-foreground [&_a]:underline\"\n            >\n              {attribution}\n            </div>,\n            overlay,\n          )\n        : null}\n    </>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "viz",
    "when": "A geo-map needs a picture of streets or terrain under the data and the project has a tile server it may use (its own, or a provider whose terms it accepts). Use projection=\"mercator\" (tiles are drawn in Web Mercator). Tiles come from a third party, so put the map in layout/map-frame with a MapFrameNotice and nothing is requested before the person asks. Without tiles, draw shapes with GeoFeatures.",
    "example": "<GeoMap label=\"Delivery area\" fit={area} projection=\"mercator\">\n  <GeoTiles url=\"https://tiles.example.org/{z}/{x}/{y}.png\" attribution=\"© Example contributors\" />\n  <GeoFeatures data={area} variant=\"line\" />\n</GeoMap>",
    "client": true
  }
}
