You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A fast, lightweight, and responsive masonry grid layout library in vanilla JavaScript.
25
27
26
-
-**Lightweight**. ~1.4 kB (minified and brotlied). Zero dependencies.
27
-
-**Fast**. Direct DOM manipulation with optimized reflow algorithms.
28
-
-**Responsive**. Automatically adapts to container size changes using [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver).
29
-
-**TypeScript**-first.
28
+
-🪶 **Lightweight**. ~1.4 kB (minified and brotlied). Zero dependencies.
29
+
-⚡ **Fast**. Direct DOM manipulation with optimized reflow algorithms.
30
+
-📱 **Responsive**. Automatically adapts to container size changes using [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver).
31
+
-📘 **TypeScript**-first.
30
32
31
33
```html
32
34
<style>
@@ -66,7 +68,7 @@ A fast, lightweight, and responsive masonry grid layout library in vanilla JavaS
@@ -75,11 +77,11 @@ new BalancedMasonryGrid(document.querySelector('.masonry'));
75
77
## Install
76
78
77
79
```bash
78
-
pnpm add masonry-grid
80
+
pnpm add mason-grid
79
81
# or
80
-
npm i masonry-grid
82
+
npm i mason-grid
81
83
# or
82
-
yarn add masonry-grid
84
+
yarn add mason-grid
83
85
```
84
86
85
87
## API
@@ -93,7 +95,7 @@ The container must be CSS Grid and each item must have `--width` and `--height`
93
95
Standard masonry layout that stacks items by pulling them up to fill gaps.
94
96
95
97
```ts
96
-
import { MasonryGrid } from'masonry-grid'
98
+
import { MasonryGrid } from'mason-grid'
97
99
98
100
const grid =newMasonryGrid(container)
99
101
```
@@ -103,7 +105,7 @@ const grid = new MasonryGrid(container)
103
105
Balanced masonry layout that reorders items inside rows to minimize overall grid height.
104
106
105
107
```ts
106
-
import { BalancedMasonryGrid } from'masonry-grid'
108
+
import { BalancedMasonryGrid } from'mason-grid'
107
109
108
110
const grid =newBalancedMasonryGrid(container)
109
111
```
@@ -122,6 +124,32 @@ grid.destroy()
122
124
123
125
It uses ResizeObserver and MutationObserver to monitor changes to the container and its items. When a change is detected, it recalculates the layout using aspect ratios defined by `--width` and `--height` CSS variables on each item. Then it applies vertical translations using `transform: translateY()` to pull items up and fill gaps, while maintaining the natural order of items in the DOM. Because of we know the aspect ratios, and translate values are calculated in percentages, resizing the container without changing columns count does not require recalculating the layout.
<!-- ending div is added for internal calculations: -->
136
+
<div></div>
137
+
</div>
138
+
```
139
+
125
140
### BalancedMasonryGrid
126
141
127
142
Same as MasonryGrid, plus it reorders items within each row to minimize overall grid height. It does this by calculating the optimal order of items in each row based on their heights and adjusting their `order` CSS property accordingly without changing order of items in the DOM.
0 commit comments