{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "hexagon-pattern",
  "title": "Hexagon Pattern",
  "description": "A hexagon background that fills its container. The hover effect is optional.",
  "dependencies": [
    "cn"
  ],
  "registryDependencies": [
    "@gnostic/merge-styles"
  ],
  "files": [
    {
      "path": "packages/ui/src/components/patterns/hexagon-pattern/hexagon-pattern.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\n\nimport { cn } from \"cn\"\nimport { mergeStyles } from \"../../../lib/merge-styles.ts\"\n\nimport { hexagonMetrics } from \"./hexagon-pattern.ts\"\nimport type { HexagonPatternProps } from \"./types.ts\"\n\n/**\n * Hexagon background.\n *\n * The field fills this element. A script counts the rows from the box size.\n * `hoverEffect` adds the hover color classes. Those classes are the whole effect.\n */\ntype Props = HTMLAttributes<\"div\"> & HexagonPatternProps\n\nconst {\n  hexagonSize = 75,\n  hexagonMargin = 3,\n  hoverEffect = false,\n  hexagonColor = \"var(--background)\",\n  gapColor,\n  hoverFill = \"var(--muted)\",\n  hoverStroke = \"color-mix(in oklch, var(--foreground) 18%, var(--background))\",\n  hexagonClass,\n  class: className,\n  style: styleProp,\n  ...props\n} = Astro.props\n\nconst metrics = hexagonMetrics(hexagonSize, hexagonMargin)\n\n/**\n * Hover classes. The docs switch reads this string and adds or removes it.\n * Keep the names here so the switch and `hoverEffect` stay the same.\n */\nconst hexagonHoverClasses =\n  \"hover:before:bg-[var(--hexagon-hover-stroke)] hover:before:duration-0 hover:after:bg-[var(--hexagon-hover-fill)] hover:after:duration-0\"\n\n/** Point-top hexagon. The inner layer uses the same shape. */\nconst hexagonClip =\n  \"[clip-path:polygon(50%_0%,_100%_25%,_100%_75%,_50%_100%,_0%_75%,_0%_25%)]\"\nconst hexagonInnerClip =\n  \"after:[clip-path:polygon(50%_0%,_100%_25%,_100%_75%,_50%_100%,_0%_75%,_0%_25%)]\"\n\nconst style = mergeStyles(\n  {\n    \"--hexagon-margin\": `${hexagonMargin}px`,\n    \"--hexagon-width\": `${metrics.hexagonWidth}px`,\n    \"--hexagon-height\": `${metrics.hexagonHeight}px`,\n    \"--hexagon-fill\": hexagonColor,\n    \"--hexagon-stroke\": hexagonColor,\n    \"--hexagon-hover-fill\": hoverFill,\n    \"--hexagon-hover-stroke\": hoverStroke,\n    ...(gapColor ? { \"background-color\": gapColor } : {}),\n  },\n  styleProp,\n)\n---\n\n<div\n  data-slot=\"hexagon-pattern\"\n  data-hover-effect={hoverEffect ? \"true\" : \"false\"}\n  data-hover-class={hexagonHoverClasses}\n  data-hexagon-width={metrics.hexagonWidth}\n  data-row-spacing={metrics.rowSpacing}\n  data-row-margin-top={metrics.marginTop}\n  data-odd-row-margin-left={metrics.oddRowMarginLeft}\n  data-even-row-margin-left={metrics.evenRowMarginLeft}\n  class={cn(\n    \"relative isolate h-full w-full overflow-hidden bg-muted\",\n    className,\n  )}\n  style={style}\n  {...props}\n>\n  <div\n    aria-hidden=\"true\"\n    data-hexagon-grid\n    class=\"absolute inset-0 -z-10 overflow-hidden\"\n  ></div>\n  {/*\n    One cell. The script clones it into the grid.\n    The hover classes are on this cell only when `hoverEffect` is true.\n  */}\n  <template data-hexagon-template>\n    <div\n      data-slot=\"hexagon\"\n      class={cn(\n        \"relative shrink-0\",\n        hexagonClip,\n        \"before:absolute before:inset-0 before:bg-[var(--hexagon-stroke)] before:transition-colors before:duration-1000 before:content-[''] motion-reduce:before:transition-none\",\n        \"after:absolute after:inset-[var(--hexagon-margin)] after:bg-[var(--hexagon-fill)] after:content-['']\",\n        hexagonInnerClip,\n        hoverEffect && hexagonHoverClasses,\n        hexagonClass,\n      )}\n      style=\"width: var(--hexagon-width); height: var(--hexagon-height); margin-left: var(--hexagon-margin)\"\n    ></div>\n  </template>\n  <slot />\n</div>\n\n<script>\n  import {\n    createHexagonPattern,\n    type HexagonPatternController,\n  } from \"./hexagon-pattern.ts\"\n\n  const controllers: HexagonPatternController[] = []\n\n  /** Drop observers before a new page view or a repeat init. */\n  function cleanup() {\n    for (const controller of controllers) controller.destroy()\n    controllers.length = 0\n  }\n\n  /** Measure every hexagon pattern currently in the document. */\n  function init() {\n    cleanup()\n    document\n      .querySelectorAll<HTMLElement>('[data-slot=\"hexagon-pattern\"]')\n      .forEach((root) => {\n        controllers.push(createHexagonPattern(root))\n      })\n  }\n\n  init()\n  document.addEventListener(\"astro:page-load\", init)\n  document.addEventListener(\"astro:before-swap\", cleanup)\n</script>\n",
      "type": "registry:file",
      "target": "~/src/components/patterns/hexagon-pattern/hexagon-pattern.astro"
    },
    {
      "path": "packages/ui/src/components/patterns/hexagon-pattern/hexagon-pattern.ts",
      "content": "/** Sizes the script needs to fill the box with hexagon rows. */\nexport type HexagonMetrics = {\n    /** Cell width, in pixels. This matches `hexagonSize`. */\n    hexagonWidth: number\n    /** Cell height, in pixels. */\n    hexagonHeight: number\n    /** Distance from one row's top to the next row's top, in pixels. */\n    rowSpacing: number\n    /** Negative top margin that pulls each row up by the overlap, in pixels. */\n    marginTop: number\n    /** Left offset for a row that starts on a hexagon point, in pixels. */\n    oddRowMarginLeft: number\n    /** Left offset for a row that starts between two hexagons, in pixels. */\n    evenRowMarginLeft: number\n}\n\nexport type HexagonPatternController = {\n    /** Stop watching this pattern. Call this before the node leaves the page. */\n    destroy: () => void\n}\n\n/** Pull every row left so the clipped edge has no empty strip. */\nconst ROW_BLEED = 10\n\nconst active = new WeakMap<HTMLElement, () => void>()\n\n/**\n * Measure one hexagon tile.\n *\n * Rows overlap on the slanted edge. The margin term keeps that edge the\n * same thickness as the straight gap, so every border is `hexagonMargin`.\n *\n * @param hexagonSize - Hexagon width, in pixels.\n * @param hexagonMargin - Gap and border thickness, in pixels.\n * @returns Row and cell offsets for the grid script.\n */\nexport function hexagonMetrics(\n    hexagonSize: number,\n    hexagonMargin: number,\n): HexagonMetrics {\n    const hexagonWidth = hexagonSize\n    const hexagonHeight = hexagonSize * 1.1\n    const corner = hexagonHeight / 4\n    const side = Math.hypot(corner, hexagonWidth / 2)\n    const rowSpacing =\n        hexagonHeight -\n        corner +\n        (hexagonMargin * (2 * side - corner)) / hexagonWidth\n\n    return {\n        hexagonWidth,\n        hexagonHeight,\n        rowSpacing,\n        marginTop: rowSpacing - hexagonHeight,\n        oddRowMarginLeft: -(hexagonSize / 2),\n        evenRowMarginLeft: hexagonMargin / 2,\n    }\n}\n\n/**\n * Fill one pattern with hexagon rows and keep the count matched to the box.\n *\n * The template holds one cell. This function clones that cell, so a change\n * to the template class is copied the next time the grid is built.\n * A second call for the same root replaces the earlier observer.\n *\n * @param root - Element with `data-slot=\"hexagon-pattern\"`.\n * @returns Teardown for the resize observer.\n */\nexport function createHexagonPattern(\n    root: HTMLElement,\n): HexagonPatternController {\n    active.get(root)?.()\n\n    const grid = root.querySelector<HTMLElement>('[data-hexagon-grid]')\n    const template = root.querySelector<HTMLTemplateElement>(\n        'template[data-hexagon-template]',\n    )\n    const width = Number(root.dataset.hexagonWidth)\n    const rowSpacing = Number(root.dataset.rowSpacing)\n    const marginTop = Number(root.dataset.rowMarginTop)\n    const oddLeft = Number(root.dataset.oddRowMarginLeft)\n    const evenLeft = Number(root.dataset.evenRowMarginLeft)\n\n    let rows = -1\n    let columns = -1\n    let observer: ResizeObserver | undefined\n\n    /** Rebuild the rows when the box size needs a different cell count. */\n    const paint = () => {\n        const cell = template?.content.querySelector<HTMLElement>(\n            '[data-slot=\"hexagon\"]',\n        )\n        if (!grid || !cell) return\n        if (\n            !Number.isFinite(width) ||\n            width <= 0 ||\n            !Number.isFinite(rowSpacing) ||\n            rowSpacing <= 0\n        )\n            return\n\n        const box = root.getBoundingClientRect()\n        // A hidden box has no size yet. Wait for the next observation.\n        if (box.width === 0 || box.height === 0) return\n\n        const nextRows = Math.ceil(box.height / rowSpacing) + 1\n        const nextColumns = Math.ceil(box.width / width) + 1\n        if (nextRows === rows && nextColumns === columns) return\n        rows = nextRows\n        columns = nextColumns\n\n        const fragment = document.createDocumentFragment()\n        for (let rowIndex = 0; rowIndex < rows; rowIndex++) {\n            const row = document.createElement('div')\n            row.className = 'flex w-max'\n            row.style.marginTop = `${marginTop}px`\n            const even = (rowIndex + 1) % 2 === 0\n            row.style.marginLeft = `${(even ? evenLeft : oddLeft) - ROW_BLEED}px`\n            for (let column = 0; column < columns; column++)\n                row.append(cell.cloneNode(true))\n            fragment.append(row)\n        }\n        grid.replaceChildren(fragment)\n    }\n\n    paint()\n    if (typeof ResizeObserver !== 'undefined') {\n        observer = new ResizeObserver(paint)\n        observer.observe(root)\n    }\n\n    const destroy = () => {\n        observer?.disconnect()\n        rows = -1\n        columns = -1\n        active.delete(root)\n    }\n    active.set(root, destroy)\n    return { destroy }\n}\n",
      "type": "registry:file",
      "target": "~/src/components/patterns/hexagon-pattern/hexagon-pattern.ts"
    },
    {
      "path": "packages/ui/src/components/patterns/hexagon-pattern/index.ts",
      "content": "export { default as HexagonPattern } from './hexagon-pattern.astro'\nexport type { HexagonPatternProps } from './types.ts'\n",
      "type": "registry:file",
      "target": "~/src/components/patterns/hexagon-pattern/index.ts"
    },
    {
      "path": "packages/ui/src/components/patterns/hexagon-pattern/types.ts",
      "content": "/**\n * Hexagon field that fills its box.\n *\n * The component draws one cell for each tile. `hoverEffect` adds the\n * hover color classes. The gap color is the component background.\n */\nexport type HexagonPatternProps = {\n    /**\n     * Width of one hexagon, in pixels.\n     * The height is 1.1 times this width.\n     *\n     * @default 75\n     */\n    hexagonSize?: number\n    /**\n     * Space between hexagons, in pixels.\n     * This space is also the border thickness.\n     *\n     * @default 3\n     */\n    hexagonMargin?: number\n    /**\n     * Change each hexagon color when the pointer is over it.\n     *\n     * @default false\n     */\n    hoverEffect?: boolean\n    /**\n     * CSS color of each hexagon.\n     *\n     * @default \"var(--background)\"\n     */\n    hexagonColor?: string\n    /**\n     * CSS color of the space between hexagons.\n     * Leave this unset to use the `bg-muted` class.\n     * A caller `bg-*` class can replace that class.\n     */\n    gapColor?: string\n    /**\n     * CSS color of the hexagon fill while the pointer is over it.\n     * Used when `hoverEffect` is true.\n     *\n     * @default \"var(--muted)\"\n     */\n    hoverFill?: string\n    /**\n     * CSS color of the hexagon border while the pointer is over it.\n     * Used when `hoverEffect` is true.\n     *\n     * @default \"color-mix(in oklch, var(--foreground) 18%, var(--background))\"\n     */\n    hoverStroke?: string\n    /**\n     * Extra classes for each hexagon.\n     * Use this to replace a fill or a hover color.\n     */\n    hexagonClass?: string\n}\n",
      "type": "registry:file",
      "target": "~/src/components/patterns/hexagon-pattern/types.ts"
    }
  ],
  "meta": {
    "framework": "astro",
    "source": "@gnostic/ui",
    "testedDependencies": {
      "cn": "0.5.2"
    }
  },
  "docs": "https://ui.gnosticai.com/patterns/hexagon-pattern",
  "categories": [
    "patterns"
  ],
  "type": "registry:component"
}
