{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "masonry-grid",
  "title": "Masonry Grid",
  "description": "A responsive masonry layout for cards, images, and other content.",
  "dependencies": [
    "@data-slot/core",
    "cn"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "packages/ui/src/components/ui/masonry-grid/index.ts",
      "content": "export { default as MasonryGrid } from './masonry-grid.astro'\nexport type { MasonryGridProps } from './types.ts'\nexport { createMasonryGrid } from './masonry-grid.ts'\nexport type { MasonryGridController } from './masonry-grid.ts'\n",
      "type": "registry:file",
      "target": "~/src/components/ui/masonry-grid/index.ts"
    },
    {
      "path": "packages/ui/src/components/ui/masonry-grid/types.ts",
      "content": "/** Layout options for content with different heights. */\nexport interface MasonryGridProps {\n    /**\n     * Minimum column count. The layout uses two columns from 30rem and four\n     * from 48rem, measured against the component width.\n     * @default 1\n     */\n    minCols?: 1 | 2 | 3 | 4\n    /**\n     * Space between items as a CSS length.\n     * @default '1rem'\n     */\n    gap?: string\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/masonry-grid/types.ts"
    },
    {
      "path": "packages/ui/src/components/ui/masonry-grid/masonry-grid.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\nimport { cn } from \"cn\"\nimport type { MasonryGridProps } from \"./types.ts\"\n\ntype Props = HTMLAttributes<\"div\"> & MasonryGridProps\n\nconst {\n  minCols = 1,\n  gap = \"1rem\",\n  class: className,\n  style,\n  ...props\n} = Astro.props\nconst columns = {\n  1: \"grid-cols-1 @min-[30rem]/masonry:grid-cols-2 @min-[48rem]/masonry:grid-cols-4\",\n  2: \"grid-cols-2 @min-[48rem]/masonry:grid-cols-4\",\n  3: \"grid-cols-3 @min-[48rem]/masonry:grid-cols-4\",\n  4: \"grid-cols-4\",\n}\nconst mergedStyle =\n  typeof style === \"string\"\n    ? `--masonry-gap:${gap};${style}`\n    : { \"--masonry-gap\": gap, ...style }\n---\n\n<div\n  data-slot=\"masonry-grid\"\n  class={cn(\"@container/masonry block min-w-0\", className)}\n  style={mergedStyle}\n  {...props}\n>\n  <div\n    data-masonry-items\n    class={cn(\n      \"relative grid items-start gap-(--masonry-gap) *:min-w-0 data-[ready]:*:absolute data-[ready]:*:w-(--masonry-width) data-[ready]:*:[inset-inline-start:var(--masonry-x)] data-[ready]:*:[top:var(--masonry-y)]\",\n      columns[minCols],\n    )}\n  >\n    <slot />\n  </div>\n</div>\n\n<script>\n  import { create } from \"./masonry-grid.ts\"\n\n  const controllers: ReturnType<typeof create> = []\n  const initialize = () => controllers.push(...create())\n  initialize()\n  document.addEventListener(\"astro:page-load\", initialize)\n  document.addEventListener(\"astro:before-swap\", () => {\n    for (const controller of controllers) controller.destroy()\n    controllers.length = 0\n  })\n</script>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/masonry-grid/masonry-grid.astro"
    },
    {
      "path": "packages/ui/src/components/ui/masonry-grid/masonry-grid.ts",
      "content": "import {\n    clearRootBinding,\n    getRootBinding,\n    getRoots,\n    hasRootBinding,\n    setRootBinding,\n} from '@data-slot/core'\n\nexport interface MasonryGridController {\n    destroy(): void\n}\n\n/** Place each item in the shortest column without changing DOM order. */\nexport function createMasonryGrid(root: HTMLElement): MasonryGridController {\n    const existing = getRootBinding<MasonryGridController>(\n        root,\n        '@gnostic/masonry-grid',\n    )\n    if (existing) return existing\n    const grid = root.querySelector<HTMLElement>(\n        ':scope > [data-masonry-items]',\n    )\n    let destroyed = false\n    let frame = 0\n    let items: HTMLElement[] = []\n    const resetItem = (item: HTMLElement) => {\n        item.style.removeProperty('--masonry-x')\n        item.style.removeProperty('--masonry-y')\n    }\n    const layout = () => {\n        frame = 0\n        if (destroyed || !grid || grid.clientWidth === 0) return\n        const styles = getComputedStyle(grid)\n        const tracks = styles.gridTemplateColumns.split(' ')\n        const width = Number.parseFloat(tracks[0] ?? '')\n        if (!Number.isFinite(width) || width <= 0) return\n        const gap = Number.parseFloat(styles.columnGap) || 0\n        const heights = Array<number>(tracks.length).fill(0)\n        grid.style.setProperty('--masonry-width', `${width}px`)\n        grid.dataset.ready = ''\n        const sizes = items.map((item) => item.getBoundingClientRect().height)\n        items.forEach((item, index) => {\n            if (getComputedStyle(item).display === 'none') return\n            const column = heights.indexOf(Math.min(...heights))\n            item.style.setProperty('--masonry-x', `${column * (width + gap)}px`)\n            item.style.setProperty('--masonry-y', `${heights[column]}px`)\n            heights[column] += sizes[index]! + gap\n        })\n        grid.style.height = `${Math.max(0, ...heights) > 0 ? Math.max(...heights) - gap : 0}px`\n    }\n    const schedule = () => {\n        if (!destroyed && !frame) frame = requestAnimationFrame(layout)\n    }\n    const resize = new ResizeObserver(schedule)\n    const observeItems = () => {\n        if (destroyed) return\n        for (const item of items) {\n            resize.unobserve(item)\n            resetItem(item)\n        }\n        items = Array.from(grid?.children ?? []).filter(\n            (child): child is HTMLElement => child instanceof HTMLElement,\n        )\n        for (const item of items) resize.observe(item)\n        schedule()\n    }\n    const mutation = new MutationObserver(observeItems)\n    if (grid) {\n        resize.observe(grid)\n        mutation.observe(grid, { childList: true })\n        observeItems()\n    }\n    const controller: MasonryGridController = {\n        destroy() {\n            if (destroyed) return\n            destroyed = true\n            cancelAnimationFrame(frame)\n            resize.disconnect()\n            mutation.disconnect()\n            if (grid) {\n                delete grid.dataset.ready\n                grid.style.removeProperty('height')\n                grid.style.removeProperty('--masonry-width')\n            }\n            items.forEach(resetItem)\n            items = []\n            clearRootBinding(root, '@gnostic/masonry-grid', controller)\n        },\n    }\n    return setRootBinding(root, '@gnostic/masonry-grid', controller)\n}\n\nexport function create(scope: ParentNode = document): MasonryGridController[] {\n    return getRoots<HTMLElement>(scope, 'masonry-grid')\n        .filter((root) => !hasRootBinding(root, '@gnostic/masonry-grid'))\n        .map(createMasonryGrid)\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/masonry-grid/masonry-grid.ts"
    }
  ],
  "meta": {
    "framework": "astro",
    "source": "@gnostic/ui",
    "testedDependencies": {
      "@data-slot/core": "^1.0.2",
      "cn": "0.5.2"
    }
  },
  "docs": "https://ui.gnosticai.com/components/masonry-grid",
  "categories": [
    "ui"
  ],
  "type": "registry:ui"
}
