GridItem Component

The GridItem component represents an individual item within the grid. Each item has a position and size in grid units.

Import

import { GridItem } from '@appulsauce/svelte-grid';

Props

PropTypeDefaultDescription
idstringuuid.v4Unique identifier for the item. Used for collision detection.
xnumberrequiredX position (column) in grid units.
ynumberrequiredY position (row) in grid units.
wnumber1Width in grid units.
hnumber1Height in grid units.
min{ w: number, h: number }{ w: 1, h: 1 }Minimum size constraints.
max{ w: number, h: number } \| undefinedundefinedMaximum size constraints. If undefined, no max limit.
movablebooleantrueWhether the item can be dragged.
resizablebooleantrueWhether the item can be resized.
onchange(detail: LayoutChangeDetail) => voidundefinedCallback when this item changes.
onpreviewchange(detail: LayoutChangeDetail) => voidundefinedCallback when the preview position changes during drag.

Style Props

PropTypeDescription
classstringCSS class for the grid item container.
activeClassstringCSS class applied when item is being dragged or resized.
previewClassstringCSS class for the preview placeholder.
resizerClassstringCSS class for the resize handle.
stylestringInline styles for the grid item.

Bindable Props

All position and size props (x, y, w, h) support two-way binding:

<script>
	let item = $state({ x: 0, y: 0, w: 2, h: 2 });
</script>

<GridItem bind:x={item.x} bind:y={item.y} bind:w={item.w} bind:h={item.h}>
	{#snippet children()}
		<div>Position: {item.x}, {item.y}</div>
	{/snippet}
</GridItem>

Snippets

children

The main content of the grid item:

<GridItem x={0} y={0}>
	{#snippet children()}
		<div class="my-content">Hello World</div>
	{/snippet}
</GridItem>

moveHandle

Custom drag handle. Receives a moveStart function to initiate dragging:

<GridItem x={0} y={0}>
	{#snippet moveHandle({ moveStart })}
		<div class="drag-handle" onpointerdown={moveStart}>Drag me</div>
	{/snippet}

	{#snippet children()}
		<div>Content</div>
	{/snippet}
</GridItem>

resizeHandle

Custom resize handle. Receives a resizeStart function to initiate resizing:

<GridItem x={0} y={0}>
	{#snippet resizeHandle({ resizeStart })}
		<div class="resize-handle" onpointerdown={resizeStart}>⤡</div>
	{/snippet}

	{#snippet children()}
		<div>Content</div>
	{/snippet}
</GridItem>

Component Structure

Understanding the DOM structure helps with styling:

<!-- GridItem wrapper -->
<div class="{class}" class:activeClass={active}>
	<!-- Your content -->
	{@render children?.()}

	<!-- Resize handle (when resizable) -->
	<div class="{resizerClass}" />
</div>

<!-- Preview (shown during drag/resize) -->
{#if active}
	<div class="{previewClass}" />
{/if}

Example

<GridItem
	id="item-1"
	x={0}
	y={0}
	w={3}
	h={2}
	min={{ w: 2, h: 1 }}
	max={{ w: 6, h: 4 }}
	class="bg-blue-100"
	activeClass="opacity-50"
	previewClass="bg-blue-500/20 border-2 border-blue-500 border-dashed"
>
	{#snippet moveHandle({ moveStart })}
		<div class="cursor-move p-2 bg-blue-200" onpointerdown={moveStart}>⠿ Drag</div>
	{/snippet}

	{#snippet children()}
		<div class="p-4">Resizable content with custom drag handle</div>
	{/snippet}
</GridItem>