Skip to content
Primitives

Masonry

Variable-height grid via CSS columns — the organic cousin of the fixed-cell Bento Grid. Items of any height tile without gaps.

Motionnone — layout primitive

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { Masonry, masonryVariants } from "kineto/masonry";

Preview

1
2
3
4
5
6
7
8
9
tsx
import * as React from "react";
import { cva, type VariantProps } from "class-variance-authority";

import { cn } from "@/lib/cn";
import { gapClass, type Gap } from "@/lib/gap";

/* Variable-height masonry via CSS multi-columns — the organic cousin of the fixed-cell BentoGrid.
 * Direct children are kept whole (`break-inside-avoid`) and spaced by the same `gap` scale as the
 * other layout primitives, so items of any height tile without gaps. (CSS columns fill top-to-bottom
 * per column; for strict left-to-right reading order, prefer a Grid.) */

const masonryVariants = cva("w-full [&>*]:break-inside-avoid", {
  variants: {
    columns: {
      1: "columns-1",
      2: "columns-2",
      3: "columns-3",
      4: "columns-4",
      5: "columns-5",
    },
  },
  defaultVariants: {
    columns: 3,
  },
});

/** Bottom margin per child that matches the chosen column gap, so vertical rhythm equals horizontal. */
const itemGapClass: Record<Gap, string> = {
  none: "[&>*]:mb-0",
  xs: "[&>*]:mb-1",
  sm: "[&>*]:mb-2",
  md: "[&>*]:mb-4",
  lg: "[&>*]:mb-6",
  xl: "[&>*]:mb-8",
  "2xl": "[&>*]:mb-12",
};

export interface MasonryProps
  extends React.HTMLAttributes<HTMLDivElement>,
    VariantProps<typeof masonryVariants> {
  /** Space between items (column gap + matching row gap), from the shared scale. */
  gap?: Gap;
}

const Masonry = React.forwardRef<HTMLDivElement, MasonryProps>(
  ({ className, columns, gap = "md", ...props }, ref) => (
    <div
      ref={ref}
      className={cn(masonryVariants({ columns }), gapClass[gap], itemGapClass[gap], className)}
      {...props}
    />
  )
);
Masonry.displayName = "Masonry";

export { Masonry, masonryVariants };

src/components/ui/masonry.tsx

Props

PropTypeDefaultDescription
columns1 | 2 | 3 | 4 | 53Number of columns.
gap"none" | "xs" | "sm" | "md" | "lg" | "xl" | "2xl""md"Space between items, from the shared gap scale.