smart-masonry-grid 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +145 -33
  2. package/package.json +9 -1
package/README.md CHANGED
@@ -1,13 +1,59 @@
1
- # smart-masonry-grid
2
-
3
- A zero-dependency, virtualized masonry grid layout library for vanilla JS and React.
1
+ <p align="center">
2
+ <h1 align="center">smart-masonry-grid</h1>
3
+ <p align="center">
4
+ A zero-dependency, virtualized masonry grid layout library for vanilla JS and React.
5
+ </p>
6
+ </p>
7
+
8
+ <p align="center">
9
+ <a href="https://www.npmjs.com/package/smart-masonry-grid"><img src="https://img.shields.io/npm/v/smart-masonry-grid?color=blue&label=npm" alt="npm version" /></a>
10
+ <a href="https://bundlephobia.com/package/smart-masonry-grid"><img src="https://img.shields.io/bundlephobia/minzip/smart-masonry-grid?color=green&label=size" alt="bundle size" /></a>
11
+ <a href="https://www.npmjs.com/package/smart-masonry-grid"><img src="https://img.shields.io/npm/dm/smart-masonry-grid?color=orange" alt="downloads" /></a>
12
+ <a href="https://github.com/LittleBoy9/smart-masonry-grid/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/smart-masonry-grid" alt="license" /></a>
13
+ <a href="https://github.com/LittleBoy9/smart-masonry-grid"><img src="https://img.shields.io/github/stars/LittleBoy9/smart-masonry-grid?style=social" alt="GitHub stars" /></a>
14
+ </p>
15
+
16
+ <p align="center">
17
+ <a href="https://littleboy9.github.io/smart-masonry-grid/"><b>Landing Page</b></a> &nbsp;&middot;&nbsp;
18
+ <a href="https://smart-masonry-grid-demo-1dkf4r5bx-iamsounak01s-projects.vercel.app/"><b>Live Demo</b></a> &nbsp;&middot;&nbsp;
19
+ <a href="https://github.com/LittleBoy9/smart-masonry-grid"><b>GitHub</b></a>
20
+ </p>
21
+
22
+ ---
23
+
24
+ ## Highlights
4
25
 
5
26
  - **Zero dependencies** — uses native `ResizeObserver` and `IntersectionObserver`
6
- - **Virtualization** — renders only visible items, handles 10,000+ items smoothly
7
- - **Responsive** — auto columns, fixed count, or breakpoint-based
8
- - **SSR-compatible** — CSS columns fallback with hydration support
27
+ - **Virtualization** — renders only visible items, handles **10,000+ items** smoothly
28
+ - **Responsive** — auto columns, fixed count, named breakpoints, or custom pixel breakpoints
29
+ - **SSR-compatible** — CSS columns fallback with hydration support for Next.js
9
30
  - **TypeScript-first** — full type definitions included
10
- - **Dual API** — vanilla `MasonryGrid` class + React components
31
+ - **Dual API** — vanilla `MasonryGrid` class + React components (`<Masonry>`, `<VirtualMasonry>`, `useMasonryGrid`)
32
+ - **Animations** — built-in entry animations with configurable duration, easing, and offset
33
+ - **Infinite scroll** — built-in `onReachEnd` callback
34
+ - **Tree-shakeable** — ESM + CommonJS, `sideEffects: false`
35
+
36
+ ---
37
+
38
+ ## Why smart-masonry-grid?
39
+
40
+ Most masonry libraries are **abandoned**, **React-only**, or **missing critical features**. Here's how `smart-masonry-grid` compares:
41
+
42
+ | Feature | smart-masonry-grid | masonry-layout | react-masonry-css | masonic | @mui/lab Masonry |
43
+ |---|:---:|:---:|:---:|:---:|:---:|
44
+ | Zero dependencies | **Yes** | No | Yes | No | No (heavy) |
45
+ | Virtualization (10K+ items) | **Yes** | No | No | Yes | No |
46
+ | SSR / Next.js support | **Yes** | No | No | Partial | Buggy |
47
+ | Vanilla JS + React | **Yes** | Vanilla only | React only | React only | React only |
48
+ | Built-in animations | **Yes** | No | No | No | No |
49
+ | Built-in infinite scroll | **Yes** | No | No | Yes | No |
50
+ | TypeScript (built-in) | **Yes** | No (@types) | Partial | Yes | Yes |
51
+ | Responsive breakpoints | **4 modes** | No | Basic | No | Basic |
52
+ | Actively maintained | **Yes** | Barely | No (4yr+) | Partial | Yes |
53
+
54
+ > **No other library** combines zero-dependency virtualization, SSR support, and multi-framework support in one package.
55
+
56
+ ---
11
57
 
12
58
  ## Install
13
59
 
@@ -15,9 +61,20 @@ A zero-dependency, virtualized masonry grid layout library for vanilla JS and Re
15
61
  npm install smart-masonry-grid
16
62
  ```
17
63
 
18
- ## React
64
+ ```bash
65
+ # or
66
+ yarn add smart-masonry-grid
67
+ # or
68
+ pnpm add smart-masonry-grid
69
+ ```
70
+
71
+ ---
19
72
 
20
- ### `<Masonry>` — renders all children
73
+ ## Quick Start
74
+
75
+ ### React
76
+
77
+ #### `<Masonry>` — renders all children
21
78
 
22
79
  ```tsx
23
80
  import { Masonry } from 'smart-masonry-grid/react';
@@ -33,7 +90,7 @@ function Gallery({ photos }) {
33
90
  }
34
91
  ```
35
92
 
36
- ### `<VirtualMasonry>` — renders only visible items
93
+ #### `<VirtualMasonry>` — for large datasets (1,000+ items)
37
94
 
38
95
  ```tsx
39
96
  import { VirtualMasonry } from 'smart-masonry-grid/react';
@@ -54,7 +111,7 @@ function Gallery({ photos }) {
54
111
  }
55
112
  ```
56
113
 
57
- ### `useMasonryGrid` hook — full control
114
+ #### `useMasonryGrid` hook — full control
58
115
 
59
116
  ```tsx
60
117
  import { useMasonryGrid } from 'smart-masonry-grid/react';
@@ -77,7 +134,7 @@ function CustomGrid({ items }) {
77
134
  }
78
135
  ```
79
136
 
80
- ## Vanilla JS
137
+ ### Vanilla JS
81
138
 
82
139
  ```js
83
140
  import { MasonryGrid } from 'smart-masonry-grid';
@@ -101,11 +158,10 @@ grid.on('resize', (width, cols) => console.log(width, cols));
101
158
  grid.destroy();
102
159
  ```
103
160
 
104
- ### Virtualized (vanilla)
161
+ #### Virtualized (vanilla)
105
162
 
106
163
  ```js
107
164
  const grid = new MasonryGrid(container, {
108
- virtualize: true,
109
165
  totalItems: 10000,
110
166
  renderItem: (index) => {
111
167
  const el = document.createElement('div');
@@ -115,48 +171,87 @@ const grid = new MasonryGrid(container, {
115
171
  });
116
172
  ```
117
173
 
118
- ## Column strategies
174
+ ---
175
+
176
+ ## Column Strategies
177
+
178
+ 4 flexible ways to configure columns:
119
179
 
120
180
  ```tsx
121
- // Fixed column count
181
+ // 1. Fixed column count
122
182
  <Masonry columns={4} />
123
183
 
124
- // Auto: fill based on minimum column width
184
+ // 2. Auto fill based on minimum column width
125
185
  <Masonry columns={{ type: 'auto', minColumnWidth: 250 }} />
126
186
 
127
- // Named breakpoints (sm=640, md=768, lg=1024, xl=1280)
187
+ // 3. Named breakpoints (sm=640, md=768, lg=1024, xl=1280)
128
188
  <Masonry columns={{ sm: 2, md: 3, lg: 4, xl: 5 }} />
129
189
 
130
- // Custom pixel breakpoints
190
+ // 4. Custom pixel breakpoints
131
191
  <Masonry columns={{ 480: 2, 768: 3, 1200: 4 }} />
132
192
  ```
133
193
 
134
- ## Props
194
+ ---
195
+
196
+ ## API Reference
135
197
 
136
- ### `<Masonry>`
198
+ ### `<Masonry>` Props
137
199
 
138
200
  | Prop | Type | Default | Description |
139
- |------|------|---------|-------------|
201
+ |---|---|---|---|
140
202
  | `children` | `ReactNode` | — | Items to lay out |
141
203
  | `columns` | `number \| ColumnStrategy \| NamedBreakpoints` | `auto, 250px` | Column configuration |
142
- | `gap` | `number` | `16` | Gap between items (px) |
204
+ | `gap` | `number` | `16` | Gap between items in pixels |
143
205
  | `animate` | `boolean \| AnimationConfig` | `false` | Entry animations |
144
- | `onLayout` | `(output: LayoutOutput) => void` | — | Layout callback |
145
- | `onReachEnd` | `() => void` | — | Infinite scroll callback |
206
+ | `onLayout` | `(output: LayoutOutput) => void` | — | Called after each layout computation |
207
+ | `onReachEnd` | `() => void` | — | Called when scrolled near the bottom (infinite scroll) |
146
208
  | `reachEndThreshold` | `number` | `200` | Pixels from bottom to trigger `onReachEnd` |
147
209
 
148
- ### `<VirtualMasonry>`
210
+ ### `<VirtualMasonry>` Props
149
211
 
150
212
  All `<Masonry>` props except `children`, plus:
151
213
 
152
214
  | Prop | Type | Default | Description |
153
- |------|------|---------|-------------|
154
- | `totalItems` | `number` | — | Total item count |
155
- | `renderItem` | `(index: number) => ReactElement` | — | Render function per item |
156
- | `height` | `number` | — | Scroll container height (px) |
157
- | `overscan` | `number` | `600` | Pre-render buffer (px) |
215
+ |---|---|---|---|
216
+ | `totalItems` | `number` | — | Total number of items |
217
+ | `renderItem` | `(index: number) => ReactElement` | — | Render function for each item |
218
+ | `height` | `number` | — | Scroll container height in pixels |
219
+ | `overscan` | `number` | `600` | Pre-render buffer in pixels |
158
220
  | `estimatedItemHeight` | `number` | `300` | Height estimate for unmeasured items |
159
- | `placeholder` | `ReactElement` | — | Shown while item is unmeasured |
221
+ | `placeholder` | `ReactElement` | — | Shown while an item is being measured |
222
+
223
+ ### `MasonryGrid` (Vanilla JS)
224
+
225
+ ```ts
226
+ const grid = new MasonryGrid(container: HTMLElement, options?: MasonryGridOptions);
227
+ ```
228
+
229
+ | Option | Type | Default | Description |
230
+ |---|---|---|---|
231
+ | `columns` | `number \| ColumnStrategy` | `auto, 250px` | Column configuration |
232
+ | `gap` | `number` | `16` | Gap between items in pixels |
233
+ | `virtualize` | `boolean` | `true` | Enable virtualization |
234
+ | `totalItems` | `number` | — | Total items (virtualized mode) |
235
+ | `renderItem` | `(index: number) => HTMLElement` | — | Item renderer (virtualized mode) |
236
+ | `ssrFallback` | `boolean` | `false` | Use CSS columns for SSR |
237
+ | `resizeDebounceMs` | `number` | `100` | Debounce delay for resize events |
238
+
239
+ **Methods:**
240
+
241
+ | Method | Description |
242
+ |---|---|
243
+ | `append(elements)` | Add elements to the end |
244
+ | `prepend(elements)` | Add elements to the beginning |
245
+ | `remove(id)` | Remove an item by ID |
246
+ | `refresh()` | Force a full relayout |
247
+ | `getLayout()` | Returns current `LayoutOutput` |
248
+ | `getColumnCount()` | Returns current column count |
249
+ | `on(event, callback)` | Subscribe to events |
250
+ | `destroy()` | Clean up observers and DOM |
251
+
252
+ **Events:** `layout`, `resize`, `scroll`, `itemResize`, `destroy`
253
+
254
+ ---
160
255
 
161
256
  ## SSR
162
257
 
@@ -167,6 +262,23 @@ import { getSSRStyles } from 'smart-masonry-grid';
167
262
  const css = getSSRStyles();
168
263
  ```
169
264
 
265
+ The library provides a CSS columns fallback for the initial server render, then smoothly transitions to the JS-powered masonry layout after hydration.
266
+
267
+ ---
268
+
269
+ ## Browser Support
270
+
271
+ Uses native browser APIs with excellent coverage:
272
+
273
+ | API | Support |
274
+ |---|---|
275
+ | `ResizeObserver` | 99%+ |
276
+ | `IntersectionObserver` | 98%+ |
277
+
278
+ No polyfills needed for modern browsers.
279
+
280
+ ---
281
+
170
282
  ## License
171
283
 
172
- [MIT](LICENSE)
284
+ [MIT](LICENSE) — Built by [Sounak Das](https://sounakdas.in)
package/package.json CHANGED
@@ -1,6 +1,14 @@
1
1
  {
2
2
  "name": "smart-masonry-grid",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
+ "homepage": "https://littleboy9.github.io/smart-masonry-grid/",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/LittleBoy9/smart-masonry-grid.git"
8
+ },
9
+ "bugs": {
10
+ "url": "https://github.com/LittleBoy9/smart-masonry-grid/issues"
11
+ },
4
12
  "description": "A zero-dependency, virtualized masonry grid layout library",
5
13
  "type": "module",
6
14
  "author": {