Svelte-Grid-Extended is a draggable, resizable and responsive grid layout. The package is created as extended verison of svelte-grid.
With NPM:
npm install svelte-grid-extended
With Yarn:
yarn add svelte-grid-extended
With pnpm:
pnpm add svelte-grid-extended
- Description
- Installation
- Table of Contents
- Usage
- Basic
- Static grid
- Grid without bounds
- Styling
- Disable interactions
- Collision Behavior
- Custom move/resize handle
- Two way binding
- API Documentation
- Grid props
- GridItem props 8000
- Style related props:
- Events
- Grid Controller
- Methods
- π License
β¨ repl
<script lang="ts">
import Grid, { GridItem } from 'svelte-grid-extended';
</script>
<Grid cols={10} rows={10}>
<GridItem x={1} y={0} class="item">Hey</GridItem>
<GridItem x={3} y={3} w={4} class="item">Hoy</GridItem>
</Grid>
When cols
or rows
and itemsSize
are set, grid becomes static and ignores the size of the container.
It can be set to both dimensions or just one.
Both: β¨ repl
<script lang="ts">
import Grid, { GridItem } from 'svelte-grid-extended';
const itemSize = { width: 100, height: 40 };
</script>
<Grid {itemSize} cols={10} rows={10}>
<GridItem x={1} y={0} class="item">Hey</GridItem>
<GridItem x={3} y={3} w={4} class="item">Hoy</GridItem>
</Grid>
Only rows: β¨ repl
<script lang="ts">
import Grid, { GridItem } from 'svelte-grid-extended';
const itemSize = { height: 40 };
</script>
<Grid {itemSize} cols={10} rows={10}>
<GridItem x={1} y={0} class="item">Hey</GridItem>
<GridItem x={3} y={3} w={4} class="item">Hoy</GridItem>
</Grid>
When cols
or/and rows
set to 0, grid grows infinitly. The grid container adapts its width and height to fit all elements.
It can be set to both dimensions or just one.
β¨ repl
<script lang="ts">
import Grid, { GridItem } from 'svelte-grid-extended';
const itemSize = { width: 100, height: 40 };
</script>
<Grid {itemSize} cols={0} rows={0}>
<GridItem x={1} y={0} class="item">Hey</GridItem>
<GridItem x={3} y={3} w={4} class="item">Hoy</GridItem>
</Grid>
Grid can be styled with classes passed to various props. Check Style related props section for more info.
β¨ repl
<script lang="ts">
import Grid, { GridItem } from 'svelte-grid-extended';
</script>
<Grid class="grid-container" cols={10} rows={10}>
<GridItem
x={0}
y={0}
class="grid-item"
activeClass="grid-item-active"
previewClass="bg-green-500 rounded"
resizerClass=""
>
<div class="item">{item.id}</div>
</GridItem>
</Grid>
<style>
:global(.grid-container) {
opacity: 0.7;
}
:global(.grid-item) {
transition:
width 4s,
height 4s;
transition:
transform 4s,
opacity 4s;
}
:global(.grid-item-active) {
opacity: 0.1;
}
/* tailwind classes */
:global(.bg-red-500) {
background-color: rgb(202, 33, 33);
}
:global(.rounded) {
border-radius: 0.25rem;
}
</style>
To disable interactions, set readOnly
prop to true
. Or set movable
and/or resizable
to false
on specific item.
Read Only grid: β¨ repl
<script lang="ts">
import Grid, { GridItem } from 'svelte-grid-extended';
</script>
<Grid cols={10} rows={10} readOnly>
<GridItem x={1} y={0} class="item">Hey</GridItem>
<GridItem x={3} y={3} w={4} class="item">Hoy</GridItem>
</Grid>
Make item non-interactive: β¨ repl
<script lang="ts">
import Grid, { GridItem } from 'svelte-grid-extended';
</script>
<Grid cols={10} rows={10}>
<GridItem x={1} y={0} class="item" movable={false}>Hey</GridItem>
<GridItem x={3} y={3} w={4} class="item" resizable={false}>Hoy</GridItem>
</Grid>
The collision
prop controls how the grid handles collisions. There are three available options: none
, push
, and compress
.
Setting collision
prop to none
will ignore any collisions. This is the default behavior.
β¨ repl
<script lang="ts">
import Grid, { GridItem } from 'svelte-grid-extended';
const items = [
{ id: '0', x: 0, y: 0, w: 2, h: 5 },
{ id: '1', x: 2, y: 2, w: 2, h: 2 },
{ id: '2', x: 2, y: 0, w: 1, h: 2 },
{ id: '3', x: 3, y: 0, w: 2, h: 2 },
{ id: '4', x: 4, y: 2, w: 1, h: 3 },
{ id: '5', x: 8, y: 0, w: 2, h: 8 }
];
const itemSize = { height: 40 };
</script>
<Grid {itemSize} cols={10} collision="none">
{#each items as item}
<GridItem x={item.x} y={item.y} w={item.w} h={item.h}>
<div class="item">{item.id}</div>
</GridItem>
{/each}
</Grid>
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.
β¨ repl
< 5D32 div class="highlight highlight-source-svelte notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="<script lang="ts"> import Grid, { GridItem } from 'svelte-grid-extended'; const items = [ { id: '0', x: 0, y: 0, w: 2, h: 5 }, { id: '1', x: 2, y: 2, w: 2, h: 2 }, { id: '2', x: 2, y: 0, w: 1, h: 2 }, { id: '3', x: 3, y: 0, w: 2, h: 2 }, { id: '4', x: 4, y: 2, w: 1, h: 3 }, { id: '5', x: 8, y: 0, w: 2, h: 8 } ]; const itemSize = { height: 40 }; </script> <Grid {itemSize} cols={10} collision="push"> {#each items as item} <GridItem x={item.x} y={item.y} w={item.w} h={item.h}> <div class="item">{item.id}</div> </GridItem> {/each} </Grid>"><script lang="ts"> import Grid, { GridItem } from 'svelte-grid-extended'; const items = [ { id: '0', x: 0, y: 0, w: 2, h: 5 }, { id: '1', x: 2, y: 2, w: 2, h: 2 }, { id: '2', x: 2, y: 0, w: 1, h: 2 }, { id: '3', x: 3, y: 0, w: 2, h: 2 }, { id: '4', x: 4, y: 2, w: 1, h: 3 }, { id: '5', x: 8, y: 0, w: 2, h: 8 } ]; const itemSize = { height: 40 }; </script> <Grid {itemSize} cols={10} collision="push"> {#each items as item} <GridItem x={item.x} y={item.y} w={item.w} h={item.h}> <div class="item">{item.id}</div> </GridItem> {/each} </Grid>