Animated Background

A shared animated background that smoothly moves between selected or hovered items.

motion-svruned

Installation

pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/animated-background.json

Usage

<script lang="ts">	import {		AnimatedBackground,		AnimatedBackgroundItem	} from "#lib/components/magic/animated-background/index.js";
	const tabs = ["Home", "About", "Contact"];</script>
<AnimatedBackground	defaultValue="Home"	class="rounded-md bg-muted">	{#each tabs as tab}		<AnimatedBackgroundItem data-id={tab}>			<button type="button" class="px-3 py-2">				{tab}			</button>		</AnimatedBackgroundItem>	{/each}</AnimatedBackground>

Examples

Hover Tabs

Hover Cards

Dialog

Enhances modal presentations.

Popover

For small interactive overlays.

Accordion

Collapsible sections for more content.

Collapsible

Collapsible sections for more content.

Drag to Reorder

Reorder items with drag and drop.

Swipe to Delete

Delete items with swipe gestures.

Segmented Control

Props

Animated Background

The root component that manages the active item and shared background animation.

NameTypeDefaultDescription
childrenSnippet-The animated background items to render.
defaultValuestringundefinedThe ID of the initially active item.
onValueChange(newActiveId: string | null) => voidundefinedCalled when the active item changes.
classClassValue""CSS classes applied to the animated background element.
transitionOptions["transition"]undefinedMotion transition options for the shared background.
enableHoverbooleanfalseActivates items on hover instead of click.

Animated Background Item

An individual selectable item that participates in the shared background animation.

NameTypeDefaultDescription
data-idstringrequiredA unique ID used to track the active item.
classClassValue""Additional CSS classes applied to the item wrapper.
childrenSnippet-The item content to render.