Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

masonic

> 前端框架
Open source

High-performance masonry layouts for React

1.4K stars0 likes1 views
WebsiteGitHub

About

High-performance masonry layouts for React



bash
npm i masonic

A performant and versatile virtualized masonry grid for React based on Brian Vaughn's react-virtualized and further inspired by react-window.

Using Masonic, you're not just getting a component. You're getting the implementation details, as well, meaning advanced usage requiring little code is possible.

Features

  • Easy to use It takes two minutes to start creating your own masonry grid with this component. For real, check out the demo on CodeSandbox.
  • Blazing™ fast This component can seamlessly render tens of thousands of grid cells without lag via its virtualization algorithm and underlying data structures. For example, it uses a red black interval tree to determine which cells to render, based upon the scroll position and size of the window the grid is rendered in. Interval trees have O(log n + m) search performance .
  • TypeScript Intellisense and type safety mean fewer bugs in your implementation.
  • Versatility All of <Masonry>'s implementation details (hooks, utilities) are exported, so you're not locked into to the default implementation. As you advance, it will be useful to have access to those internals. It's also possible to kick the virtualization out of the equation by providing an Infinity value to the overscanBy prop, though this would be a terrible idea for large lists.
  • Autosizing The default <Masonry> component will automatically resize itself and its items if the content of the grid cells changes or resizes. For example, when an image lazily loads this component will automatically do the work of recalculating the size of that grid cell. That said, you should try to premeasure things (including images) as often as possible in order to achieve the best user experience.

Quick Start

Check out the demo on CodeSandbox

javascript
import * as React from "react";
import { Masonry } from "masonic";

let i = 0;
const items = Array.from(Array(5000), () => ({ id: i++ }));

const EasyMasonryComponent = (props) => (
  <Masonry items={items} render={MasonryCard} />
);

const MasonryCard = ({ index, data: { id }, width }) => (
  

    <pre>ID: {id}</pre>
    

  </div>
);

Documentation

Components

Component Description
<Masonry> A "batteries included" masonry grid which includes all of the implementation details below. This component is the easiest way to get off and running in your app, before switching to more advanced implementations, if necessary. It will change its column count to fit its container's width and will decide how many rows to render based upon the height of the browser window.
<MasonryScroller> A heavily-optimized component that updates useMasonry() when the scroll position of the browser window changes. This bare-metal component is used by <Masonry> above.
<List> This is just a single-column <Masonry> component with no columnGutter prop, only rowGutter.

Hooks

Hook Description
useMasonry() This hook handles the render phases of the masonry layout and returns the grid as a React element.
usePositioner() This hook creates the grid cell positioner and cache required by useMasonry(). This is the meat of the grid's layout algorithm, determining which cells to render at a given scroll position, as well as where to place new items in the grid.
useResizeObserver() Creates a resize observer that forces updates to the grid when mutations are made to the grid cells affecting their size.
useContainerPosition() A hook for measuring the width of the grid container, as well as its distance from the top of the document. These values are necessary to correctly calculate the number/width of columns to render, as well as the number of rows to render.
useScroller() A hook for tracking whether the window is currently being scrolled and it's scroll position on the y-axis. These values are used for determining which grid cells to render and when to add styles to the grid container that maximize scroll performance.
useScrollToIndex() A hook that creates a callback for scrolling to a specific index in the "items" array.
useInfiniteLoader() A utility hook for seamlessly adding infinite scroll behavior to the useMasonry() hook. This hook invokes a callback each time the last rendered index surpasses the total number of items in your items array or the number defined in the totalItems option of this hook.

Utilities

Utility Description
createPositioner() Creates a cell positioner for the useMasonry() hook. The usePositioner() hook uses this utility under the hood.
createResizeObserver() Creates a resize observer that fires an updater callback whenever the height of one or many cells change. The useResizeObserver() hook is using this under the hood.

Recipes

  • Add infinite scrolling behavior to your Masonry component.
  • Cells don't resize once they're measured? Build a Masonry grid without the resize observer.
  • Reset <Masonry> layout when switching between routes within the same component.
    • Do the same with an advanced custom implementation using the usePositioner() hook.
  • Render a Masonry component relative to a scrollable HTML element rather than the browser window.
  • Add an isScrolling prop to cells

<Masonry>

An autosizing masonry grid that only renders items currently visible in the browser window. This component will change its column count to fit its container's width and will decide how many rows to render based upon the height of the browser window. To facilitate this, it uses useMasonry(), usePositioner(), useResizeObserver(), useContainerPosition(), and useScroller() under the hood.

This is the "batteries included" option. It's the easiest way to get off and running with your app and a great stepping stone to more advanced implementations, should you need them.

Check out an example on CodeSandbox

javascript
import * as React from "react";
import { Masonry } from "masonic";

let i = 0;
const items = Array.from(Array(5000), () => ({ id: i++ }));

const EasyMasonryComponent = (props) => (
  <Masonry items={items} render={MasonryCard} />
);

const MasonryCard = ({ index, data: { id }, width }) => (
  

    <pre>ID: {id}</pre>
    

  </div>
);

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScripthooksinfinite-scrolllistmasonry

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架