1.0.3 • Published 5 months ago

masonry-gridify v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Masonry React Component

A lightweight, responsive, and customizable Masonry grid component for React.

Features

  • Auto-calculates columns based on container width.
  • Resizes dynamically when the window size changes.
  • Provides smooth transitions.
  • Uses ResizeObserver for real-time adjustments.

Installation

npm install masonry-gridify
// or
npm install --save masonry-gridify
// or
yarn add masonry-gridify

Usage

If height is not defined

It takes the clientHeight of each element even if height is defined or not. So no need to explicitly define the height.

If height is defined

import Masonry from "masonry-gridify";

const Example = () => {
  return (
    <Masonry gap={10} width={"250px"}>
      <div style={{ height: "150px", background: "red" }}></div>
      <div style={{ height: "200px", background: "blue" }}></div>
      <div style={{ height: "250px", background: "green" }}></div>
      <div style={{ height: "180px", background: "yellow" }}></div>
    </Masonry>
  );
};

export default Example;

Features

Number of Columns

import Masonry, { columns } from "masonry-gridify";

Gets the number of columns displayed in screen.

Default Transition

<Masonry gap={10} width={"250px"} defaultTransition={true}></Masonry>

Enables a default transition effect of 0.3s ease.

Custom Loading

<Masonry gap={10} width={"250px"} loading={<p>Loading...</p>}></Masonry>

Displays a custom loading indicator while the grid is being prepared.

Custom transition

<Masonry gap={10} width={"250px"} transition={"1s ease-in-out"}></Masonry>

Applies a custom transition style.


Props

Prop NameTypeRequiredDescription
gapnumberThe gap between the grid items (in pixels).
widthstringMinimum column width (supports px, rem, em, etc.).
childrenJSX.ElementContent elements to arrange.
loadingJSX.ElementA loading indicator while the masonry grid is being prepared.
transitionstringCustom transition style.
defaultTransitionbooleanEnables default transition of 0.3s ease.

How It Works

  1. Dynamic Column Calculation:

    • Calculates columns based on container width and column width.
    • Uses ResizeObserver and window resize events for responsiveness.
  2. Element Positioning:

    • Uses CSS transforms to adjust element positions.
    • Maintains vertical gaps between items.
    • Handles overlapping elements through margin adjustments.
  3. Performance:

    • Debounced resize handling (200ms delay).
    • RequestAnimationFrame for smooth transitions.
    • Cleanup of observers and event listeners.

Debugging Guide

Common Issues & Fixes

  1. No items appear

    • Ensure children are passed correctly.
    • Check for missing gap or width props.
  2. Elements are overlapping or positioned incorrectly

    • Try refreshing the page. If the issue persists, inspect the elements in DevTools.
  3. Layout Not Updating on Resize

    • Check if the ResizeObserver is working in your browser’s dev tools.
    • Try forcing an update by changing the window size.
  4. Flickering During Transitions

    Copy paste this code in your item. If the issue persists, you can always create a pull request.

    // Add this to your masonry items' CSS:
    .masonryItem {
      will-change: transform;
      backface-visibility: hidden;
    }

Contribution

If you find any issues or want to contribute, feel free to submit a pull request.

Happy Coding! 🚀

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago