Motion Grid
svelte-animations/motion-gridFreeBlock
A frame-based 2D grid animation primitive with active/inactive cell states.
Install it
npx shadcn@latest add https://sv-animations.vercel.app/r/motion-grid.jsonSource
1<script lang="ts">2 import { motion } from "motion-sv";3 import { setMotionGridContext } from "./use-motion-grid-context.svelte";4 import type { MotionGridCellMotionProps, MotionGridProps } from "./types";56 type StyleValue = MotionGridCellMotionProps["style"];78 function mergeStyles(baseStyle: StyleValue, userStyle: StyleValue): StyleValue {9 return {10 ...(baseStyle ?? {}),11 ...(userStyle ?? {}),12 };13 }1415 function withRefProp<T extends Record<string, unknown>>(props: T) {16 return Object.defineProperty(props, "ref", {17 enumerable: true,18 configurable: true,19 get: () => ref,20 set: (value) => {21 ref = value as HTMLDivElement | null;22 },23 }) as T & { ref: HTMLDivElement | null };24 }2526 let {27 ref = $bindable(null),28 gridSize,29 frames,30 duration = 200,31 animate = true,32 child,33 children,34 ...restProps35 }: MotionGridProps = $props();3637 let index = $state(0);3839 let cols = $derived(gridSize[0]);40 let rows = $derived(gridSize[1]);4142 setMotionGridContext({43 animate: () => {44 return animate;45 },46 index: () => {47 return index;48 },49 cols: () => {50 return cols;51 },52 rows: () => {53 return rows;54 },55 frames: () => {56 return frames;57 },58 duration: () => {59 return duration;60 },61 });6263 $effect(() => {64 frames;65 index = 0;66 });6768 $effect(() => {69 if (frames.length === 0) {70 index = 0;71 return;72 }7374 if (index >= frames.length) {75 index = 0;76 }77 });7879 $effect(() => {80 if (!animate || frames.length === 0) return;8182 let interval = setInterval(() => {83 index = (index + 1) % frames.length;84 }, duration);8586 return () => clearInterval(interval);87 });8889 let baseStyle = $derived({90 display: "grid",91 gridTemplateColumns: `repeat(${cols}, minmax(0, 1fr))`,92 gridAutoRows: "1fr",93 });9495 let mergedProps = $derived.by(() => {96 let style = mergeStyles(baseStyle, restProps.style as StyleValue);97 return withRefProp({98 ...restProps,99 "data-animate": animate,100 style,101 }) as MotionGridCellMotionProps;102 });103</script>104105{#if child}106 {@render child({ props: mergedProps })}107{:else}108 <motion.div bind:ref {...mergedProps}>109 {@render children?.()}110 </motion.div>111{/if}
What it pulls in
npm packages
Files it writes
More from Svelte Animations
All 66 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Animated Beamanimated-beam | Svelte Animations | Blocks | Free | 1 dep · 6 files | |
| Animated Circular Progress Baranimated-circular-progress-bar | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Animated Gradient Textanimated-gradient-text | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Animated Grid Patternanimated-grid-pattern | Svelte Animations | Blocks | Free | 1 dep · 2 files | |
| Animated Listanimated-list | Svelte Animations | Blocks | Free | 1 dep · 2 files | |
| Animated Shiny Textanimated-shiny-text | Svelte Animations | Blocks | Free | no deps | |
| Animated Theme Toggleranimated-theme-toggler | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Arc Timelinearc-timeline | Svelte Animations | Blocks | Free | no deps · 3 files |
