New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

masonry-grid-component

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

masonry-grid-component

A Pinterest-style grid layout component built on top of @zapperwing/pinterest-view

latest
npmnpm
Version
3.0.5
Version published
Maintainers
1
Created
Source

MasonryGrid Component

Pinterest-style grid layout component that automatically arranges child elements in an optimized grid pattern. Built on top of @zapperwing/pinterest-view, this component provides virtualization support for grids with a large number of cards.

Installation

npm install masonry-grid-component

Version Compatibility

This component is built on top of @zapperwing/pinterest-view v2.0.0+ and includes support for:

  • Layout Freezing: Lock the current layout in place to prevent reflow
  • Enhanced Ref API: Full ref-forwarding with both callback and object refs
  • Improved Performance: Better virtualization and layout optimization

Basic Usage

import { MasonryGrid } from 'masonry-grid-component';

function Gallery() {
  return (
    <MasonryGrid columnWidth={300} gutter={15}>
      <div>Item 1</div>
      <div>Item 2</div>
      <div>Item 3</div>
    </MasonryGrid>
  );
}

With Percentage-based Columns

<MasonryGrid columnWidth="33.33%" gutter={15}>
  {/* items */}
</MasonryGrid>

With Image Monitoring

<MasonryGrid 
  columnWidth={300} 
  gutter={15}
  monitorImagesLoaded={true}
>
  <img src="image1.jpg" />
  <img src="image2.jpg" />
</MasonryGrid>

With Manual Layout Updates

import { MasonryGrid, MasonryGridRef } from 'masonry-grid-component';
import { useRef } from 'react';

function DynamicGallery() {
  const gridRef = useRef<MasonryGridRef>(null);

  const handleContentChange = () => {
    gridRef.current?.updateLayout();
  };

  return (
    <MasonryGrid ref={gridRef} columnWidth={300} gutter={15}>
      {/* Dynamic content */}
    </MasonryGrid>
  );
}

With Layout Freezing (v2.0.0+)

import { MasonryGrid, MasonryGridRef } from 'masonry-grid-component';
import { useRef } from 'react';

function GalleryWithFreeze() {
  const gridRef = useRef<MasonryGridRef>(null);

  const handleFreeze = () => {
    gridRef.current?.freeze(); // Locks current layout
  };

  const handleUnfreeze = () => {
    gridRef.current?.unfreeze(); // Allows normal reflow
  };

  return (
    <div>
      <button onClick={handleFreeze}>Freeze Layout</button>
      <button onClick={handleUnfreeze}>Unfreeze Layout</button>
      <MasonryGrid ref={gridRef} columnWidth={300} gutter={15}>
        {/* Grid items */}
      </MasonryGrid>
    </div>
  );
}

With Custom Scroll Container

import { MasonryGrid } from 'masonry-grid-component';
import { useRef } from 'react';

function ScrollableGallery() {
  const scrollContainerRef = useRef<HTMLDivElement>(null);

  return (
    <div 
      ref={scrollContainerRef}
      style={{ height: '600px', overflow: 'auto' }}
    >
      <MasonryGrid
        columnWidth={300}
        gutter={15}
        virtualized={true}
        scrollContainer={scrollContainerRef.current}
      >
        {/* Grid items */}
      </MasonryGrid>
    </div>
  );
}

Props

PropTypeDefaultDescription
columnWidthnumber | string150Width of each column. Can be pixels or percentage
gutternumber5Spacing between items in pixels
monitorImagesLoadedbooleanfalseWhether to monitor and reflow when images load
onLayout(layout: { height: number }) => void-Callback function when layout changes
classNamestring-Additional CSS class for the grid container
styleReact.CSSProperties-Additional inline styles
idstring-Unique identifier for the component
childrenReactNode-Grid items to be rendered
virtualizedbooleanfalseEnable virtualization for better performance with large lists
virtualizationBuffernumber800Buffer size for virtualization in pixels
debugbooleanfalseEnable debug logging
rtlbooleanfalseEnable right-to-left layout
scrollContainerHTMLElement-Custom scroll container for virtualization (defaults to window)

Ref Methods

When using a ref with the MasonryGrid component, you can access these methods:

MethodDescription
updateLayout()Manually triggers a layout recalculation
clearCache()Clears the height cache and triggers a layout update
freeze()Freezes the current layout in place (locks positions)
unfreeze()Clears the frozen state so new items will re-flow normally

Component Structure

Core Components

  • MasonryGrid: The main component wrapped with forwardRef for ref forwarding
  • StackGrid: The underlying grid implementation from @zapperwing/pinterest-view

Key Features Implementation

Virtualization

The component uses virtualized rendering through the StackGrid component, making it efficient even with thousands of items:

<StackGrid
  virtualized={true}
  columnWidth={columnWidth}
  gutterWidth={gutter}
  gutterHeight={gutter}
>
  {children}
</StackGrid>

Layout Updates

Exposes an imperative handle for manual layout updates and layout freezing:

useImperativeHandle(ref, () => ({
  updateLayout: () => {
    if (gridRef.current?.updateLayout) {
      gridRef.current.updateLayout();
    } else if (gridRef.current?.layout) {
      gridRef.current.layout();
    }
  },
  clearCache: () => {
    // Trigger a layout update as a fallback
    if (gridRef.current?.updateLayout) {
      gridRef.current.updateLayout();
    } else if (gridRef.current?.layout) {
      gridRef.current.layout();
    }
  },
  freeze: () => {
    if (gridRef.current?.freeze) {
      gridRef.current.freeze();
    }
  },
  unfreeze: () => {
    if (gridRef.current?.unfreeze) {
      gridRef.current.unfreeze();
    }
  },
}));

Best Practices

  • Performance

    • Use virtualization when dealing with large numbers of items
    • Enable monitorImagesLoaded only when necessary
    • Provide fixed dimensions for child items when possible
  • Responsive Design

    • Use percentage-based column widths for fluid layouts
    • Consider different column widths for different breakpoints
  • Image Loading

    • Enable monitorImagesLoaded when your grid contains images
    • The component will automatically reflow when images finish loading

Examples

Check the demo in the demo/ directory for more examples, including:

  • Virtualized rendering with many cards
  • Variable height items
  • Percentage-based layouts
  • Dynamic expandable cards

Testing

Run the component's tests:

npm test

FAQs

Package last updated on 10 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts