layout

Flip Grid

A layout component that smoothly animates its children when the grid structure changes.


Installation

Install the component

Run the following command to install the component and its dependencies:
npx @motion-core/cli add flip-grid

Import the component

Import the component into your Svelte file:
import { FlipGrid, FlipGridItem } from "$lib/motion-core";
import { FlipGrid, FlipGridItem } from "$lib/motion-core";

Usage

Demo 0
Demo 1
Demo 2
Demo 3
<script lang="ts">
	import { FlipGrid, FlipGridItem } from "motion-core";
	import { cn } from "$lib/utils/cn";

	let columns = $state(4);

	const images = [
		"/images/demos/sample-8.jpg",
		"/images/demos/sample-9.jpg",
		"/images/demos/sample-10.jpg",
		"/images/demos/sample-11.jpg",
	];
</script>

<div class="relative h-full min-h-96 w-full">
	<div class="h-full p-4">
		<FlipGrid class="w-full gap-4" {columns} duration={0.5} stagger={0.1}>
			{#each images as src, i (src)}
				<FlipGridItem
					id={`img-${i}`}
					class="relative aspect-square w-full overflow-hidden rounded-lg shadow-md"
				>
					<img {src} alt="Demo {i}" class="h-full w-full object-cover" />
				</FlipGridItem>
			{/each}
		</FlipGrid>
	</div>

	<div
		class="absolute bottom-4 left-1/2 flex w-fit -translate-x-1/2 justify-center gap-1 rounded-lg border border-border bg-background p-1 shadow-sm"
	>
		{#each [1, 2, 4] as col (col)}
			<button
				class={cn(
					"gap-1.5 rounded-md px-3 py-1 text-xs font-medium tracking-wide whitespace-nowrap uppercase transition-colors duration-150 ease-out",
					columns === col
						? "bg-accent dark:text-foreground light:text-card"
						: "text-foreground/70 hover:text-foreground",
				)}
				onclick={() => (columns = col)}
			>
				{col} Cols
			</button>
		{/each}
	</div>
</div>

Props

FlipGrid

PropTypeDefault
children
Snippet
columns
number undefined
class
string ""
style
string ""
duration
number 0.5
ease
string "power2.inOut"
stagger
number 0

FlipGridItem

PropTypeDefault
children
Snippet
id
string
class
string ""