cuire / svelte-grid-extended

A draggable and resizable grid layout, for Svelte
https://svelte-grid-extended.vercel.app
MIT License
74 stars 8 forks source link

👹 Add `gridController.compress()` method and `autoCompress` prop #69

Closed cuire closed 5 months ago

cuire commented 5 months ago

Closes #57

Breaking

Grid with collision = "compress" now auto compress its when items are changed programmaticly. If you want to disable this behavior, you can set autoCompress prop to false.

Added gridController.compress() method

gridController.compress() can be used to compress items on grids with non compress collision strategy.

<script lang="ts">
    import Grid, { GridItem, type GridController } from 'svelte-grid-extended';

    let items = [
        { id: '1', x: 0, y: 0, w: 2, h: 5 },
        { id: '2', x: 2, y: 2, w: 2, h: 2 }
    ];

    let gridController: GridController;

    function compressItems() {
        gridController.compress();
    }

    const itemSize = { height: 40 };
</script>

<button class="btn" on:click={compressItems}>Compress Items</button>

<Grid {itemSize} cols={10} collision="push" bind:controller={gridController}>
    {#each items as item (item.id)}
        <GridItem id={item.id} bind:x={item.x} bind:y={item.y} bind:w={item.w} bind:h={item.h}>
            <div class="item">{item.id.slice(0, 5)}</div>
        </GridItem>
    {/each}
</Grid>