High-performance masonry layouts for React
npm i masonicA 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.
O(log n + m) search performance .<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.<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.Check out the demo on CodeSandbox
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>
);| 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. |
| 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. |
| 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. |
<Masonry> layout when switching between routes within the same component.
window.isScrolling prop to cellsAn 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
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>
);No open issues yet, or sync has not completed.