@appulsauce/svelte-grid 2.0.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) 2022 cuire
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,556 @@
1
+ # @appulsauce/svelte-grid
2
+
3
+ ##### Example
4
+
5
+ ```svelte
6
+ <script lang="ts">
7
+ import Grid, { GridItem, type GridController } from '@appulsauce/svelte-grid';
8
+
9
+ let items = $state([
10
+ { id: '1', x: 0, y: 0, w: 2, h: 5 },
11
+ { id: '2', x: 2, y: 2, w: 2, h: 2 }
12
+ ]);
13
+
14
+ let gridController: GridController;
15
+
16
+ function addNewItem() {
17
+ const w = Math.floor(Math.random() * 2) + 1;
18
+ const h = Math.floor(Math.random() * 5) + 1;
19
+ const newPosition = gridController.getFirstAvailablePosition(w, h);
20
+ items = newPosition
21
+ ? [...items, { id: crypto.randomUUID(), x: newPosition.x, y: newPosition.y, w, h }]
22
+ : items;
23
+ }
24
+
25
+ const itemSize = { height: 40 };
26
+ </script>
27
+
28
+ <button onclick={addNewItem}>Add New Item</button>
29
+
30
+ <Grid {itemSize} cols={10} collision="push" bind:controller={gridController}>
31
+ {#each items as { id, x, y, w, h } (id)}
32
+ <GridItem {id} bind:x bind:y bind:w bind:h>
33
+ {#snippet children()}
34
+ <div>{id}</div>
35
+ {/snippet}
36
+ </GridItem>
37
+ {/each}
38
+ </Grid>
39
+ ```
40
+ - [GridItem props](#griditem-props)
41
+ - [Style related props:](#style-related-props)
42
+ - [Events](#events)
43
+ - [Grid Controller](#grid-controller)
44
+ - [Methods](#methods)
45
+ - [getFirstAvailablePosition(w, h)](#getfirstavailablepositionw-h)
46
+ - [Example](#example)
47
+ - [License](#-license)
48
+
49
+ ## Usage
50
+
51
+ ### Basic
52
+
53
+ ```svelte
54
+ <script lang="ts">
55
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
56
+ </script>
57
+
58
+ <Grid cols={10} rows={10}>
59
+ <GridItem x={1} y={0} class="item">{#snippet children()}Hey{/snippet}</GridItem>
60
+ <GridItem x={3} y={3} w={4} class="item">{#snippet children()}Hoy{/snippet}</GridItem>
61
+ </Grid>
62
+ ```
63
+
64
+ ### Static grid
65
+
66
+ When `cols` or `rows` and `itemsSize` are set, grid becomes static and ignores the size of the container.
67
+
68
+ It can be set to both dimensions or just one.
69
+
70
+ Both:
71
+
72
+ ```svelte
73
+ <script lang="ts">
74
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
75
+
76
+ const itemSize = { width: 100, height: 40 };
77
+ </script>
78
+
79
+ <Grid {itemSize} cols={10} rows={10}>
80
+ <GridItem x={1} y={0} class="item">{#snippet children()}Hey{/snippet}</GridItem>
81
+ <GridItem x={3} y={3} w={4} class="item">{#snippet children()}Hoy{/snippet}</GridItem>
82
+ </Grid>
83
+ ```
84
+
85
+ Only rows:
86
+
87
+ ```svelte
88
+ <script lang="ts">
89
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
90
+
91
+ const itemSize = { height: 40 };
92
+ </script>
93
+
94
+ <Grid {itemSize} cols={10} rows={10}>
95
+ <GridItem x={1} y={0} class="item">{#snippet children()}Hey{/snippet}</GridItem>
96
+ <GridItem x={3} y={3} w={4} class="item">{#snippet children()}Hoy{/snippet}</GridItem>
97
+ </Grid>
98
+ ```
99
+
100
+ ### Grid without bounds
101
+
102
+ When `cols` or/and `rows` set to 0, grid grows infinitely. The grid container adapts its width and height to fit all elements.
103
+
104
+ It can be set to both dimensions or just one.
105
+
106
+ ```svelte
107
+ <script lang="ts">
108
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
109
+
110
+ const itemSize = { width: 100, height: 40 };
111
+ </script>
112
+
113
+ <Grid {itemSize} cols={0} rows={0}>
114
+ <GridItem x={1} y={0} class="item">{#snippet children()}Hey{/snippet}</GridItem>
115
+ <GridItem x={3} y={3} w={4} class="item">{#snippet children()}Hoy{/snippet}</GridItem>
116
+ </Grid>
117
+ ```
118
+
119
+ ### Styling
120
+
121
+ Grid can be styled with classes passed to various props. Check [Style related props](#style-related-props) section for more info.
122
+
123
+ ```svelte
124
+ <script lang="ts">
125
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
126
+ </script>
127
+
128
+ <Grid class="grid-container" cols={10} rows={10}>
129
+ <GridItem
130
+ x={0}
131
+ y={0}
132
+ class="grid-item"
133
+ activeClass="grid-item-active"
134
+ previewClass="bg-green-500 rounded"
135
+ resizerClass=""
136
+ >
137
+ {#snippet children()}
138
+ <div class="item">Content</div>
139
+ {/snippet}
140
+ </GridItem>
141
+ </Grid>
142
+
143
+ <style>
144
+ :global(.grid-container) {
145
+ opacity: 0.7;
146
+ }
147
+
148
+ :global(.grid-item) {
149
+ transition:
150
+ width 4s,
151
+ height 4s;
152
+ transition:
153
+ transform 4s,
154
+ opacity 4s;
155
+ }
156
+
157
+ :global(.grid-item-active) {
158
+ opacity: 0.1;
159
+ }
160
+
161
+ :global(.bg-green-500) {
162
+ background-color: rgb(34, 197, 94);
163
+ }
164
+
165
+ :global(.rounded) {
166
+ border-radius: 0.25rem;
167
+ }
168
+ </style>
169
+ ```
170
+
171
+ ### Disable interactions
172
+
173
+ To disable interactions, set `readOnly` prop to `true`. Or set `movable` and/or `resizable` to `false` on specific item.
174
+
175
+ Read Only grid:
176
+
177
+ ```svelte
178
+ <script lang="ts">
179
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
180
+ </script>
181
+
182
+ <Grid cols={10} rows={10} readOnly>
183
+ <GridItem x={1} y={0} class="item">{#snippet children()}Hey{/snippet}</GridItem>
184
+ <GridItem x={3} y={3} w={4} class="item">{#snippet children()}Hoy{/snippet}</GridItem>
185
+ </Grid>
186
+ ```
187
+
188
+ Make item non-interactive:
189
+
190
+ ```svelte
191
+ <script lang="ts">
192
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
193
+ </script>
194
+
195
+ <Grid cols={10} rows={10}>
196
+ <GridItem x={1} y={0} class="item" movable={false}>{#snippet children()}Hey{/snippet}</GridItem>
197
+ <GridItem x={3} y={3} w={4} class="item" resizable={false}>{#snippet children()}Hoy{/snippet}</GridItem>
198
+ </Grid>
199
+ ```
200
+
201
+ ### Collision Behavior
202
+
203
+ The `collision` prop controls how the grid handles collisions. There are three available options: `none`, `push`, and `compress`.
204
+
205
+ #### None
206
+
207
+ Setting `collision` prop to `none` will ignore any collisions. This is the default behavior.
208
+
209
+ ```svelte
210
+ <script lang="ts">
211
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
212
+
213
+ let items = $state([
214
+ { id: '0', x: 0, y: 0, w: 2, h: 5 },
215
+ { id: '1', x: 2, y: 2, w: 2, h: 2 },
216
+ { id: '2', x: 2, y: 0, w: 1, h: 2 },
217
+ { id: '3', x: 3, y: 0, w: 2, h: 2 },
218
+ { id: '4', x: 4, y: 2, w: 1, h: 3 },
219
+ { id: '5', x: 8, y: 0, w: 2, h: 8 }
220
+ ]);
221
+
222
+ const itemSize = { height: 40 };
223
+ </script>
224
+
225
+ <Grid {itemSize} cols={10} collision="none">
226
+ {#each items as item (item.id)}
227
+ <GridItem x={item.x} y={item.y} w={item.w} h={item.h}>
228
+ {#snippet children()}
229
+ <div class="item">{item.id}</div>
230
+ {/snippet}
231
+ </GridItem>
232
+ {/each}
233
+ </Grid>
234
+ ```
235
+
236
+ #### Push
237
+
238
+ Setting `collision` prop to `push` will cause grid items to move to the first available space when colliding. The grid will grow vertically as needed to accommodate all items.
239
+
240
+ ```svelte
241
+ <script lang="ts">
242
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
243
+
244
+ let items = $state([
245
+ { id: '0', x: 0, y: 0, w: 2, h: 5 },
246
+ { id: '1', x: 2, y: 2, w: 2, h: 2 },
247
+ { id: '2', x: 2, y: 0, w: 1, h: 2 },
248
+ { id: '3', x: 3, y: 0, w: 2, h: 2 },
249
+ { id: '4', x: 4, y: 2, w: 1, h: 3 },
250
+ { id: '5', x: 8, y: 0, w: 2, h: 8 }
251
+ ]);
252
+
253
+ const itemSize = { height: 40 };
254
+ </script>
255
+
256
+ <Grid {itemSize} cols={10} collision="push">
257
+ {#each items as item (item.id)}
258
+ <GridItem x={item.x} y={item.y} w={item.w} h={item.h}>
259
+ {#snippet children()}
260
+ <div class="item">{item.id}</div>
261
+ {/snippet}
262
+ </GridItem>
263
+ {/each}
264
+ </Grid>
265
+ ```
266
+
267
+ #### Compress
268
+
269
+ Setting `collision` prop to `compress` will compress items vertically towards the top into any available space when colliding. The grid will grow vertically as needed to accommodate all items.
270
+
271
+ ```svelte
272
+ <script lang="ts">
273
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
274
+
275
+ let items = $state([
276
+ { id: '0', x: 0, y: 0, w: 2, h: 5 },
277
+ { id: '1', x: 2, y: 2, w: 2, h: 2 },
278
+ { id: '2', x: 2, y: 0, w: 1, h: 2 },
279
+ { id: '3', x: 3, y: 0, w: 2, h: 2 },
280
+ { id: '4', x: 4, y: 2, w: 1, h: 3 },
281
+ { id: '5', x: 8, y: 0, w: 2, h: 8 }
282
+ ]);
283
+
284
+ const itemSize = { height: 40 };
285
+ </script>
286
+
287
+ <Grid {itemSize} cols={10} collision="compress">
288
+ {#each items as item (item.id)}
289
+ <GridItem x={item.x} y={item.y} w={item.w} h={item.h}>
290
+ {#snippet children()}
291
+ <div class="item">{item.id}</div>
292
+ {/snippet}
293
+ </GridItem>
294
+ {/each}
295
+ </Grid>
296
+ ```
297
+
298
+ > Setting `collision` to `push` or `compress` will set `rows` to `0` so `ItemSize.height` must be set.
299
+
300
+ ### Custom move/resize handle
301
+
302
+ You can customize the move and resize handles using snippets:
303
+
304
+ ```svelte
305
+ <script lang="ts">
306
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
307
+ </script>
308
+
309
+ <Grid cols={10} rows={10}>
310
+ <GridItem x={0} y={0}>
311
+ {#snippet moveHandle(moveStart)}
312
+ <div onpointerdown={moveStart}>MOVE</div>
313
+ {/snippet}
314
+
315
+ {#snippet resizeHandle(resizeStart)}
316
+ <div onpointerdown={resizeStart}>RESIZE</div>
317
+ {/snippet}
318
+
319
+ {#snippet children()}
320
+ <!-- content -->
321
+ <div>Content here</div>
322
+ {/snippet}
323
+ </GridItem>
324
+ </Grid>
325
+ ```
326
+
327
+ ### Two way binding
328
+
329
+ ```svelte
330
+ <script lang="ts">
331
+ import Grid, { GridItem } from '@appulsauce/svelte-grid';
332
+
333
+ let items = $state([
334
+ { x: 6, y: 0, w: 2, h: 2, data: { text: 'A' } },
335
+ { x: 6, y: 2, w: 2, h: 2, data: { text: 'B' } }
336
+ ]);
337
+
338
+ const itemsBackup = structuredClone($state.snapshot(items));
339
+
340
+ function resetGrid() {
341
+ items = structuredClone(itemsBackup);
342
+ }
343
+
344
+ const itemSize = { height: 40 };
345
+ </script>
346
+
347
+ <button onclick={resetGrid}>RESET</button>
348
+
349
+ <Grid cols={10} {itemSize}>
350
+ {#each items as item (item.data.text)}
351
+ <GridItem bind:x={item.x} bind:y={item.y} bind:w={item.w} bind:h={item.h}>
352
+ {#snippet children()}
353
+ {item.data.text}
354
+ {/snippet}
355
+ </GridItem>
356
+ {/each}
357
+ </Grid>
358
+ ```
359
+
360
+ ## API Documentation
361
+
362
+ ### Grid props
363
+
364
+ | prop | description | type | default |
365
+ | ------------ | ---------------------------------------------------------------------------------------------------------------------- | ----------------------------------- | ------- |
366
+ | cols | Grid columns count. If set to 0, grid will grow infinitly. Must be >= 0. | number | 0 |
367
+ | rows | Grid rows count. If set to 0, grid will grow infinitly. Must be >= 0. | number | 0 |
368
+ | itemSize | Size of the grid item. If not set, grid will calculate it based on container size. | { width?: number, height?: number } | {} |
369
+ | gap | Gap between grid items. | number | 10 |
370
+ | bounds | Should grid items be bounded by the grid container. | boolean | false |
371
+ | readonly | If true disables interaction with grid items. | boolean | false |
372
+ | collision | Collision behavior of grid items. [About](#collision-behavior) | none \| push \| compress | none |
373
+ | autoCompress | Auto compress the grid items when programmatically changing grid items. Only works with 'compress' collision strategy. | boolean | true |
374
+ | onchange | Callback when grid items are changed. | (detail: LayoutChangeDetail) => void | undefined |
375
+
376
+ > If `cols` or/and `rows` are set to 0, `itemSize.width` or/and `itemSize.height` must be set.
377
+
378
+ > Setting `collision` to `push` or `compress` will set `rows` to `0` so `ItemSize.height` must be set.
379
+
380
+ ### GridItem props
381
+
382
+ | prop | description | type | default |
383
+ | --------- | -------------------------------------------------------------------------------------------------- | ------------------------------------- | -------------- |
384
+ | id | Unique id of the item. Used to compare items during collision tests | string | uuid.v4 |
385
+ | x | X position of the item in grid units. | number | required |
386
+ | y | Y position of the item in grid units. | number | required |
387
+ | w | Width of the item in grid units. | number | 1 |
388
+ | h | Height of the item in grid units. | number | 1 |
389
+ | min | Minimum size of the item in Grid Units. | { w: number, h: number } | { w: 1, h: 1 } |
390
+ | max | Maximum size of the item in Grid Units. If not provided, the item will be able to grow infinitely. | { w: number, h: number } \| undefined | undefined |
391
+ | movable | If true, item can be moved by user. | boolean | true |
392
+ | resizable | If true, item can be resized by user. | boolean | true |
393
+ | onchange | Callback when item position/size changes. | (detail: LayoutChangeDetail) => void | undefined |
394
+ | onpreviewchange | Callback when preview position/size changes during drag. | (detail: LayoutChangeDetail) => void | undefined |
395
+
396
+ ### Style related props:
397
+
398
+ Component can be styled with css framework of your choice, global classes or `style` prop. To do so, you can use the following props:
399
+
400
+ - `<Grid class="..." />` - class name for grid container.
401
+ - `<Grid style="..." />` - inline style for grid container.
402
+ - `<GridItem class="..." />` - class name for grid item.
403
+ - `<GridItem activeClass="..." />` - class name that applies when item is currently being dragged or resized. By default, it is used to make active grid item transparent.
404
+ - `<GridItem previewClass="..." />` - class name for preview where item will be placed after interaction.
405
+ - `<GridItem resizerClass="..." />` - class name for item's resize handle.
406
+ - `<GridItem style="..." />` - inline style for grid item.
407
+
408
+ To understand how to use these props, look at `<Grid />` component simplified structure.
409
+
410
+ > `active` is a variable that indicates if grid item is currently being dragged or resized:
411
+
412
+ ```svelte
413
+ <!-- Grid -->
414
+ <div class={class}>
415
+ <!-- GridItem -->
416
+ <div class={itemClass} class:activeClass={active}>
417
+ {@render children?.()}
418
+ <!-- Resizer -->
419
+ <div class={resizerClass} />
420
+ <!-- Resizer -->
421
+ </div>
422
+
423
+ {#if active}
424
+ <!-- GridItemGhost (preview) -->
425
+ <div class={previewClass} />
426
+ {/if}
427
+
428
+ <!-- /GridItem -->
429
+ </div>
430
+ <!-- /Grid -->
431
+ ```
432
+
433
+
434
+ ##### Example
435
+
436
+ ```svelte
437
+ <script lang="ts">
438
+ import Grid, { GridItem, type GridController } from '@appulsauce/svelte-grid';
439
+
440
+ let items = $state([
441
+ { id: '1', x: 0, y: 0, w: 2, h: 5 },
442
+ { id: '2', x: 2, y: 2, w: 2, h: 2 }
443
+ ]);
444
+
445
+ let gridController: GridController;
446
+
447
+ function compressItems() {
448
+ gridController.compress();
449
+ }
450
+
451
+ const itemSize = { height: 40 };
452
+ </script>
453
+
454
+ <button class="btn" onclick={compressItems}>Compress Items</button>
455
+
456
+ <Grid {itemSize} cols={10} collision="push" bind:controller={gridController}>
457
+ {#each items as item (item.id)}
458
+ <GridItem id={item.id} bind:x={item.x} bind:y={item.y} bind:w={item.w} bind:h={item.h}>
459
+ {#snippet children()}
460
+ <div class="item">{item.id.slice(0, 5)}</div>
461
+ {/snippet}
462
+ </GridItem>
463
+ {/each}
464
+ </Grid>
465
+ ```
466
+ ### Methods
467
+
468
+ #### getFirstAvailablePosition(w, h)
469
+
470
+ Finds the first available position within the grid that can accommodate an item of the specified width (w) and height (h). This method is useful when dynamically adding new items to the grid, ensuring that they fit into the first available space that can hold them.
471
+
472
+ **Parameters:**
473
+
474
+ - `w` (number): Width of the item.
475
+ - `h` (number): Height of the item.
476
+
477
+ **Returns:**
478
+
479
+ - An object containing the `x` and `y` coordinates of the first available position, or `null` if no position is available.
480
+
481
+ ##### Example
482
+
483
+ ```svelte
484
+ <script lang="ts">
485
+ import Grid, { GridItem, type GridController } from '@appulsauce/svelte-grid';
486
+
487
+ let items = [
488
+ { id: '1', x: 0, y: 0, w: 2, h: 5 },
489
+ { id: '2', x: 2, y: 2, w: 2, h: 2 }
490
+ ];
491
+
492
+ let gridController: GridController;
493
+
494
+ function addNewItem() {
495
+ const w = Math.floor(Math.random() * 2) + 1;
496
+ const h = Math.floor(Math.random() * 5) + 1;
497
+ const newPosition = gridController.getFirstAvailablePosition(w, h);
498
+ items = newPosition
499
+ ? [...items, { id: crypto.randomUUID(), x: newPosition.x, y: newPosition.y, w, h }]
500
+ : items;
501
+ }
502
+
503
+ const itemSize = { height: 40 };
504
+ </script>
505
+
506
+ <button on:click={addNewItem}>Add New Item</button>
507
+
508
+ <Grid {itemSize} cols={10} collision="push" bind:controller={gridController}>
509
+ {#each items as { id, x, y, w, h } (id)}
510
+ <div transition:fade={{ duration: 300 }}>
511
+ <GridItem {id} bind:x bind:y bind:w bind:h>
512
+ <div>{id}</div>
513
+ </GridItem>
514
+ </div>
515
+ {/each}
516
+ </Grid>
517
+ ```
518
+
519
+ #### compress()
520
+
521
+ Compresses all items vertically towards the top into any available space.
522
+
523
+ ##### Example
524
+
525
+ ```svelte
526
+ <script lang="ts">
527
+ import Grid, { GridItem, type GridController } from '@appulsauce/svelte-grid';
528
+
529
+ let items = [
530
+ { id: '1', x: 0, y: 0, w: 2, h: 5 },
531
+ { id: '2', x: 2, y: 2, w: 2, h: 2 }
532
+ ];
533
+
534
+ let gridController: GridController;
535
+
536
+ function compressItems() {
537
+ gridController.compress();
538
+ }
539
+
540
+ const itemSize = { height: 40 };
541
+ </script>
542
+
543
+ <button class="btn" on:click={compressItems}>Compress Items</button>
544
+
545
+ <Grid {itemSize} cols={10} collision="push" bind:controller={gridController}>
546
+ {#each items as item (item.id)}
547
+ <GridItem id={item.id} bind:x={item.x} bind:y={item.y} bind:w={item.w} bind:h={item.h}>
548
+ <div class="item">{item.id.slice(0, 5)}</div>
549
+ </GridItem>
550
+ {/each}
551
+ </Grid>
552
+ ```
553
+
554
+ ## License
555
+
556
+ MIT