Hulian UIv0.93.5413 components0 components
  • Start
  • Components
  • Blocks
  • Pages
  • Demos
  • Changelog
ChineseEnglish
  • ComponentsComponents
  • ThemeTheme

Containers

  • Layout
  • AdminLayout
  • SettingsLayout
  • ScrollArea
  • Viewport
  • Resizable
  • AspectRatio
  • FitScreen
  • PreviewSandbox
  • Masonry
  • Container
  • AccentScope

Arrangement

  • Stack
  • Grid
  • Spacer
  • Divider
  • Separator

Masonry

masonry

Distributes items deterministically across responsive columns without hydration-order changes.

Usage

Basic usage

Input data source and renderItem, sort by source order round-robin, 3 columns of waterfall flow.

#1
#4
#7
#10
#2
#5
#8
#11
#3
#6
#9
#12
tsx
<Masonry
  items={photos}
  columns={3}
  gap={16}
  renderItem={(photo) => <img src={photo.url} alt={photo.alt} />}
/>

Customize the number of columns

columns passes the number to fix the number of columns, and gap controls the inter-column and intra-column spacing (pixels).

#1
#3
#5
#7
#9
#11
#2
#4
#6
#8
#10
#12
tsx
<Masonry items={photos} columns={2} gap={16} renderItem={(p) => <Card {...p} />} />

Responsive columns

columns passes {base, sm, md, lg}: SSR/the first frame uses base, and switches according to the window breakpoint after mounting.

#1
#2
#3
#4
#5
#6
#7
#8
#9
#10
#11
#12
tsx
<Masonry
  items={photos}
  columns={{ base: 1, sm: 2, md: 3, lg: 4 }}
  gap={16}
  renderItem={(photo) => <img src={photo.url} alt={photo.alt} />}
/>

When to use

Use Masonry to arrange unequal-height cards (photos, activity items, generated assets) into staggered columns while preserving SSR safety and source order. It distributes items deterministically rather than using native CSS columns, avoiding hydration mismatches and visual reordering. Use AspectRatio for one fixed-ratio item, or CSS Grid for a regular equal-height layout.

Import

ts
import { Masonry } from "@hulianui/ui"

Props

Masonry<T> generic component.

NameTypeDefaultDescription
items*T[]-Source items, distributed round-robin in source order.
columnsnumber | { base?: number; sm?: number; md?: number; lg?: number }3Fixed count when numeric, or responsive counts by breakpoint. base is used for SSR and the first client frame; matchMedia selects a breakpoint after mount.
gapnumber16Gap between columns and between items within each column (px).
classNamestring-The root container class name.

Slots

SlotTypeDescription
renderItem*(item: T, index: number) => ReactNodeRenders one item; the returned node is wrapped in a column cell.

Example

tsx
// Fixed 3 columns
<Masonry items={tiles} columns={3} gap={16} renderItem={(t) => <Tile tile={t} />} />
tsx
// Responsive columns; base is used during SSR and on the first client frame
<Masonry
  items={photos}
  columns={{ base: 1, sm: 2, md: 3, lg: 4 }}
  gap={16}
  renderItem={(photo) => <img src={photo.url} alt={photo.alt} />}
/>

Usage guidelines

  • Responsive columns use base for SSR and the first client frame. After mounting, matchMedia switches to the current breakpoint count. This intentionally prevents hydration mismatches, so a wide viewport may briefly begin with the base count.
  • Round-robin does not fill the shortest column. The deterministic rule is item[i] → column i % count, which preserves order and SSR output but can leave column heights uneven. Do not use Masonry when exact shortest-column packing is required.
  • The sm, md, and lg breakpoints are 640, 768, and 1024 px, matching Tailwind defaults. The largest matching breakpoint wins; otherwise the component falls back to base.

Related

Layout · AdminLayout · ScrollArea · Viewport · Resizable · AspectRatio

Playground

#1
#4
#7
#10
#2
#5
#8
#11
#3
#6
#9
#12
<Masonry
  items={photos}
  columns={{ base: 1, sm: 2, md: 3, lg: 4 }}
  gap={16}
  renderItem={(photo) => <img src={photo.url} alt={photo.alt} />}
/>
PreviousPreviewSandboxLayoutNextStack

On this page

  • Usage
  • Documentation
    • When to use
    • Import
    • Props
    • Slots
    • Example
    • Usage guidelines
    • Related
  • Playground