1.1.3 • Published 4 months ago

grid-rows-masonry v1.1.3

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

Grid Rows Masonry

Overview

code style: prettier

Grid Rows Masonry is a ponyfill for the CSS masonry layout, designed for browsers that don’t yet support it. It simulates the effect of grid-template-rows: masonry; by reordering child elements and applying negative top margins, ensuring each item neatly aligns under the one above it while preserving the grid’s row gap.

Installation

Install via npm:

npm install grid-rows-masonry

Usage

  1. Define a grid container with multiple columns:
<div id="my-grid" style="display: grid; grid-template-columns: repeat(3, 1fr);">
  ...
</div>
  1. Import the module and initialize it:
import { GridRowsMasonry } from "grid-rows-masonry";

const el = document.getElementById("my-grid");
const masonry = new GridRowsMasonry(el);
  1. To reset the layout, call the destroy() method:
masonry.destroy();

To use the module with React:

import { useEffect, useRef } from "react";
import { GridRowsMasonry } from "grid-rows-masonry";

const MyMasonryGrid = () => {
  const ref = useRef(null);

  useEffect(() => {
    if (ref.current) {
      const masonry = new GridRowsMasonry(ref.current);
      return () => masonry.destroy();
    }
  }, []);

  return (
    <div
      ref={ref}
      style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)" }}
    >
      ...
    </div>
  );
};

Example

Grid layout with the Grid Rows Masonry ponyfill applied.

A masonry layout of ten images of mid-century modern furniture

1.1.3

4 months ago

1.1.2

5 months ago

1.1.1

5 months ago

1.1.0

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago