smart-masonry-grid 0.1.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Sounak Das
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,172 @@
1
+ # smart-masonry-grid
2
+
3
+ A zero-dependency, virtualized masonry grid layout library for vanilla JS and React.
4
+
5
+ - **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
9
+ - **TypeScript-first** — full type definitions included
10
+ - **Dual API** — vanilla `MasonryGrid` class + React components
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install smart-masonry-grid
16
+ ```
17
+
18
+ ## React
19
+
20
+ ### `<Masonry>` — renders all children
21
+
22
+ ```tsx
23
+ import { Masonry } from 'smart-masonry-grid/react';
24
+
25
+ function Gallery({ photos }) {
26
+ return (
27
+ <Masonry columns={{ sm: 2, md: 3, lg: 4 }} gap={16} animate>
28
+ {photos.map((photo) => (
29
+ <img key={photo.id} src={photo.src} alt={photo.alt} />
30
+ ))}
31
+ </Masonry>
32
+ );
33
+ }
34
+ ```
35
+
36
+ ### `<VirtualMasonry>` — renders only visible items
37
+
38
+ ```tsx
39
+ import { VirtualMasonry } from 'smart-masonry-grid/react';
40
+
41
+ function Gallery({ photos }) {
42
+ return (
43
+ <VirtualMasonry
44
+ totalItems={photos.length}
45
+ renderItem={(index) => (
46
+ <img src={photos[index].src} alt={photos[index].alt} />
47
+ )}
48
+ height={600}
49
+ columns={4}
50
+ gap={16}
51
+ animate
52
+ />
53
+ );
54
+ }
55
+ ```
56
+
57
+ ### `useMasonryGrid` hook — full control
58
+
59
+ ```tsx
60
+ import { useMasonryGrid } from 'smart-masonry-grid/react';
61
+
62
+ function CustomGrid({ items }) {
63
+ const { containerRef, layout, getItemStyle } = useMasonryGrid({
64
+ columns: 3,
65
+ gap: 16,
66
+ });
67
+
68
+ return (
69
+ <div ref={containerRef} style={{ position: 'relative', height: layout?.totalHeight }}>
70
+ {items.map((item, i) => (
71
+ <div key={item.id} style={getItemStyle(i)}>
72
+ {item.content}
73
+ </div>
74
+ ))}
75
+ </div>
76
+ );
77
+ }
78
+ ```
79
+
80
+ ## Vanilla JS
81
+
82
+ ```js
83
+ import { MasonryGrid } from 'smart-masonry-grid';
84
+
85
+ const grid = new MasonryGrid(document.getElementById('grid'), {
86
+ columns: { type: 'auto', minColumnWidth: 250 },
87
+ gap: 16,
88
+ });
89
+
90
+ // Dynamic operations
91
+ grid.append(newElement);
92
+ grid.prepend(newElement);
93
+ grid.remove(element);
94
+ grid.refresh();
95
+
96
+ // Events
97
+ grid.on('layout', (output) => console.log(output.totalHeight));
98
+ grid.on('resize', (width, cols) => console.log(width, cols));
99
+
100
+ // Cleanup
101
+ grid.destroy();
102
+ ```
103
+
104
+ ### Virtualized (vanilla)
105
+
106
+ ```js
107
+ const grid = new MasonryGrid(container, {
108
+ virtualize: true,
109
+ totalItems: 10000,
110
+ renderItem: (index) => {
111
+ const el = document.createElement('div');
112
+ el.textContent = `Item ${index}`;
113
+ return el;
114
+ },
115
+ });
116
+ ```
117
+
118
+ ## Column strategies
119
+
120
+ ```tsx
121
+ // Fixed column count
122
+ <Masonry columns={4} />
123
+
124
+ // Auto: fill based on minimum column width
125
+ <Masonry columns={{ type: 'auto', minColumnWidth: 250 }} />
126
+
127
+ // Named breakpoints (sm=640, md=768, lg=1024, xl=1280)
128
+ <Masonry columns={{ sm: 2, md: 3, lg: 4, xl: 5 }} />
129
+
130
+ // Custom pixel breakpoints
131
+ <Masonry columns={{ 480: 2, 768: 3, 1200: 4 }} />
132
+ ```
133
+
134
+ ## Props
135
+
136
+ ### `<Masonry>`
137
+
138
+ | Prop | Type | Default | Description |
139
+ |------|------|---------|-------------|
140
+ | `children` | `ReactNode` | — | Items to lay out |
141
+ | `columns` | `number \| ColumnStrategy \| NamedBreakpoints` | `auto, 250px` | Column configuration |
142
+ | `gap` | `number` | `16` | Gap between items (px) |
143
+ | `animate` | `boolean \| AnimationConfig` | `false` | Entry animations |
144
+ | `onLayout` | `(output: LayoutOutput) => void` | — | Layout callback |
145
+ | `onReachEnd` | `() => void` | — | Infinite scroll callback |
146
+ | `reachEndThreshold` | `number` | `200` | Pixels from bottom to trigger `onReachEnd` |
147
+
148
+ ### `<VirtualMasonry>`
149
+
150
+ All `<Masonry>` props except `children`, plus:
151
+
152
+ | 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) |
158
+ | `estimatedItemHeight` | `number` | `300` | Height estimate for unmeasured items |
159
+ | `placeholder` | `ReactElement` | — | Shown while item is unmeasured |
160
+
161
+ ## SSR
162
+
163
+ ```js
164
+ import { getSSRStyles } from 'smart-masonry-grid';
165
+
166
+ // Returns a CSS string for server-side rendering
167
+ const css = getSSRStyles();
168
+ ```
169
+
170
+ ## License
171
+
172
+ [MIT](LICENSE)