{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "gallery-masonry",
  "type": "registry:block",
  "title": "Gallery, masonry",
  "description": "Images in balanced columns with mixed frame shapes (wide, square, tall) and a caption under each. Use when the pictures are not all the same crop. Empty caption hides the line.",
  "categories": [
    "gallery"
  ],
  "dependencies": [
    "zod@^4.6.5"
  ],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/block-kit.json",
    "https://ui.klarhimmel.com/r/editing.json",
    "https://ui.klarhimmel.com/r/kit-c-proof.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/blocks/gallery-masonry/spec.ts",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/gallery-masonry/spec.ts",
      "content": "import { z } from 'zod';\nimport { defineSpec, media, t } from '../_shared/schema';\n\nconst schema = z.object({\n  eyebrow: t(60),\n  title: t(120),\n  description: t(300),\n  /** Columns at the widest container: '2' or '3'. Narrow containers fall back to fewer. */\n  columns: z.enum(['2', '3']),\n  items: z.array(z.object({\n    /** Frame shape: 'wide' 4:3, 'square' 1:1, 'tall' 3:4. Mix them; the columns balance themselves. */\n    shape: z.enum(['wide', 'square', 'tall']),\n    media,\n    caption: t(120),\n  })).min(2).max(12),\n});\n\n// Neutral alt instructions; the pictures themselves are the placeholder.\nconst PICTURES = [['p0', 'Show the subject of this picture, close'], ['p1', 'Show the subject of this picture, close'], ['p2', 'Show the subject of this picture, close'], ['p3', 'Show the subject of this picture, close'], ['p4', 'Show the subject of this picture, close'], ['p5', 'Show the subject of this picture, close'], ['p6', 'Show the subject of this picture, close'], ['p7', 'Show the subject of this picture, close']] as const;\nlet pictureNo = 0;\nconst shot = (shape: 'wide' | 'square' | 'tall', caption: string, _alt: string) => { const [, alt] = PICTURES[pictureNo++ % PICTURES.length]!; return { shape, media: { src: '', alt }, caption }; };\n\nexport const spec = defineSpec({\n  type: 'gallery-masonry',\n  aliases: ['gallery/masonry'],\n  name: 'Gallery, masonry',\n  category: 'gallery',\n  role: 'body',\n  description: 'Images in balanced columns with mixed frame shapes (wide, square, tall) and a caption under each. Use when the pictures are not all the same crop. Empty caption hides the line.',\n  schema,\n  defaults: {\n    eyebrow: 'Name the topic',\n    title: 'Invite them to look around the place',\n    description: 'Say in one line how the pictures were taken.',\n    columns: '3',\n    items: [\n      shot('square', 'Name what this shows', 'Show the subject'),\n      shot('wide', 'Name the subject', 'Show the subject'),\n      shot('square', 'Name the subject', 'Show the subject'),\n      shot('tall', 'Name what this shows, and when', 'Show the subject'),\n      shot('tall', 'Name what this shows, and when', 'Show the subject'),\n      shot('wide', 'Name what this shows', 'Show the subject'),\n      shot('square', 'Name the subject', 'Show the subject'),\n      shot('wide', 'Name what this shows', 'Show the subject'),\n    ],\n  },\n});\n"
    },
    {
      "path": "src/blocks/gallery-masonry/index.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/gallery-masonry/index.tsx",
      "content": "import { Media, withHeadingLevel } from '../../editing';\nimport { Container, Opt, Section } from '../_shared/kit';\nimport { Heading, useHeadingId } from '../_shared/c-proof-kit';\nimport { cn } from '../../lib/utils';\nimport { spec } from './spec';\nimport type { BlockProps } from '../types';\nimport type { z } from 'zod';\n\nexport { spec };\ntype Props = z.infer<typeof spec.schema>;\n\nconst RATIO = { wide: '4 / 3', square: '1 / 1', tall: '3 / 4' } as const;\nconst COLS = { '2': '@xl:columns-2', '3': '@xl:columns-2 @4xl:columns-3' } as const;\n\n/** Frame height as a share of column width, plus room for the caption line and the gap. */\nconst HEIGHT = { wide: 0.75, square: 1, tall: 4 / 3 } as const;\n\n/** Indexes that end a column when the items are split in order into k columns of the most even height. */\nfunction columnEnds(heights: number[], k: number): Set<number> {\n  const n = heights.length;\n  const ends = new Set<number>();\n  if (k < 2 || n <= k) return ends;\n  const sum = (from: number, to: number) => heights.slice(from, to).reduce((a, h) => a + h, 0);\n  // best[c][i]: lowest possible tallest column when the first i items fill c columns.\n  const best = Array.from({ length: k + 1 }, () => new Array<number>(n + 1).fill(Infinity));\n  const cut = Array.from({ length: k + 1 }, () => new Array<number>(n + 1).fill(0));\n  best[0]![0] = 0;\n  for (let c = 1; c <= k; c++)\n    for (let i = c; i <= n; i++)\n      for (let j = c - 1; j < i; j++) {\n        const cost = Math.max(best[c - 1]![j]!, sum(j, i));\n        if (cost < best[c]![i]!) { best[c]![i] = cost; cut[c]![i] = j; }\n      }\n  for (let c = k, i = n; c > 1; c--) { i = cut[c]![i]!; ends.add(i - 1); }\n  return ends;\n}\n\nfunction GalleryMasonry({ props }: BlockProps<Props>) {\n  const id = useHeadingId(props.title);\n  const heights = props.items.map((it) => HEIGHT[it.shape] + (it.caption ? 0.2 : 0.06));\n  const two = columnEnds(heights, 2);\n  const three = props.columns === '3' ? columnEnds(heights, 3) : new Set<number>();\n  return (\n    <Section labelledBy={id}>\n      <Container>\n        <Heading id={id} eyebrow={props.eyebrow} title={props.title} description={props.description} />\n        <ul className={`mt-14 columns-1 gap-5 ${COLS[props.columns]}`}>\n          {props.items.map((item, i) => (\n            <li key={i} className={cn('mb-5 break-inside-avoid', two.has(i) && (props.columns === '3' ? '@xl:@max-4xl:break-after-column' : '@xl:break-after-column'), three.has(i) && '@4xl:break-after-column')}>\n              <figure>\n                <Media path={['items', i, 'media']} value={item.media} ratio={RATIO[item.shape]} className=\"rounded-kh-lg border border-kh-line\" />\n                <Opt path={['items', i, 'caption']} value={item.caption} as=\"figcaption\" className=\"mt-3 text-kh-small text-kh-muted\" placeholder=\"Caption\" />\n              </figure>\n            </li>\n          ))}\n        </ul>\n      </Container>\n    </Section>\n  );\n}\n\nexport default withHeadingLevel(GalleryMasonry);\n"
    }
  ]
}
