Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
A shared animated background that smoothly moves between selected or hovered items.
motion-svruned
preview.svelte
<script lang="ts"> import { AnimatedBackground, AnimatedBackgroundItem, } from "$lib/components/magic/animated-background/index.js"; import { Annotation } from "$lib/components/ui/annotation/index.js"; import { Button } from "$lib/components/ui/button/index.js"; import { Home, PhoneCall, Settings, User } from "@lucide/svelte";
const tabs = [ { label: "Home", icon: Home }, { label: "About", icon: User }, { label: "Services", icon: Settings }, { label: "Contact", icon: PhoneCall }, ];</script>
<div class="absolute bottom-8"> <Annotation note="Click one. Watch it glide." direction="s" customColor="var(--preview-annotation-color)" noMark class="border-muted flex w-full space-x-2 rounded-xl! border bg-neutral-50/40 p-1.5 [--preview-annotation-color:var(--color-ink)] dark:bg-zinc-900/50 dark:[--preview-annotation-color:var(--color-muted-foreground)]" > <AnimatedBackground defaultValue={tabs[0].label} class="rounded-lg bg-zinc-200/50 dark:bg-zinc-800" transition={{ type: "spring", bounce: 0.2, duration: 0.3 }} > {#each tabs as tab (tab.label)} {@const Icon = tab.icon} <AnimatedBackgroundItem data-id={tab.label} class="group h-9 w-9"> <Button aria-label={tab.label} variant="ghost" size="icon" class="hover:bg-transparent dark:hover:bg-transparent" > <Icon class="h-5 w-5" /> </Button> </AnimatedBackgroundItem> {/each} </AnimatedBackground> </Annotation></div>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/animated-background.json
Install dependencies
Install the required packages for this component:
pnpm add motion-sv runed
Copy the Source Code
Copy and paste the following code into your project:
animated-background-context.svelte.ts
import { createContext } from "svelte";import type { LayoutMotionNamespace, Options } from "motion-sv";import type { ClassValue } from "svelte/elements";
export type AnimatedBackgroundContext = { readonly activeId: string | null; readonly backgroundClass: ClassValue | undefined; readonly transition: Options["transition"]; readonly enableHover: boolean; readonly layoutId: string; readonly layout: LayoutMotionNamespace; setActiveId: (id: string | null) => void;};
export const [getAnimatedBackgroundContext, setAnimatedBackgroundContext] = createContext<AnimatedBackgroundContext>(); animated-background-item.svelte
<script lang="ts" module> import type { WithElementRef } from "#lib/utils.js"; import type { HTMLAttributes } from "svelte/elements";
export type AnimatedBackgroundItemProps = WithElementRef< Omit<HTMLAttributes<HTMLDivElement>, "data-id"> & { "data-id": string; }, HTMLDivElement >;</script>
<script lang="ts"> import { cn } from "#lib/utils.js"; import { getAnimatedBackgroundContext } from "./animated-background-context.svelte.js";
let { ref = $bindable(null), "data-id": dataId, class: className, onclick, onmouseenter, onmouseleave, children, ...restProps }: AnimatedBackgroundItemProps = $props();
const background = getAnimatedBackgroundContext(); const layout = background.layout; const active = $derived(background.activeId === dataId);</script>
<div bind:this={ref} {...restProps} data-id={dataId} data-checked={active ? "true" : "false"} data-animated-background-id={background.layoutId} class={cn("relative inline-flex", className)} onclick={(event) => { if (!background.enableHover) { background.setActiveId(dataId); } onclick?.(event); }} onmouseenter={(event) => { if (background.enableHover) { background.setActiveId(dataId); } onmouseenter?.(event); }} onmouseleave={(event) => { const relatedItem = event.relatedTarget instanceof Element ? event.relatedTarget.closest("[data-animated-background-id]") : null; const isMovingWithinBackground = relatedItem?.getAttribute("data-animated-background-id") === background.layoutId;
if (background.enableHover && !isMovingWithinBackground) { background.setActiveId(null); } onmouseleave?.(event); }}> {#if active} <layout.div layoutId={background.layoutId} class={cn("absolute inset-0", background.backgroundClass)} transition={background.transition} ></layout.div> {/if} <div class="z-10"> {@render children?.()} </div></div> animated-background.svelte
<script lang="ts" module> import type { Options } from "motion-sv"; import type { Snippet } from "svelte"; import type { ClassValue } from "svelte/elements";
export type AnimatedBackgroundProps = { children?: Snippet; defaultValue?: string; onValueChange?: (newActiveId: string | null) => void; class?: ClassValue; transition?: Options["transition"]; enableHover?: boolean; };</script>
<script lang="ts"> import { createLayoutMotion } from "motion-sv"; import { watch } from "runed"; import { setAnimatedBackgroundContext } from "./animated-background-context.svelte.js";
let { children, defaultValue, onValueChange, class: backgroundClass, transition, enableHover = false, }: AnimatedBackgroundProps = $props();
let activeId = $state<string | null>(null); const uniqueId = $props.id(); const layout = createLayoutMotion(); const updateActiveId = layout.update.with((id: string | null) => { activeId = id; });
function setActiveId(id: string | null) { updateActiveId(id); onValueChange?.(id); }
watch( () => defaultValue, (value) => { if (value !== undefined) { updateActiveId(value); } } );
setAnimatedBackgroundContext({ get activeId() { return activeId; }, get backgroundClass() { return backgroundClass; }, get transition() { return transition; }, get enableHover() { return enableHover; }, layoutId: `background-${uniqueId}`, layout, setActiveId, });</script>
{@render children?.()} index.ts
import Root, { type AnimatedBackgroundProps } from "./animated-background.svelte";import Item, { type AnimatedBackgroundItemProps } from "./animated-background-item.svelte";
export { Root, Item, type AnimatedBackgroundProps as Props, type AnimatedBackgroundProps, type AnimatedBackgroundItemProps, // Root as AnimatedBackground, Item as AnimatedBackgroundItem,};Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── animated-background/ ├── animated-background-context.svelte.ts ├── animated-background-item.svelte ├── animated-background.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/animated-background/llms.txt
Follow the guide's installation and usage instructions.Match the project's existing structure, imports, and styles.Keep changes scoped to the component and verify it works.Reply with a short bullet list of completed changes.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
animated-tabs-hover-example.svelte
<script lang="ts"> import { AnimatedBackground, AnimatedBackgroundItem, } from "$lib/components/magic/animated-background/index.js";
const tabs = ["Home", "About", "Services", "Contact"];</script>
<div class="flex flex-row"> <AnimatedBackground defaultValue={tabs[0]} class="rounded-lg bg-zinc-100 dark:bg-zinc-800" transition={{ type: "spring", bounce: 0.2, duration: 0.3 }} enableHover > {#each tabs as tab (tab)} <AnimatedBackgroundItem data-id={tab}> <button type="button" class="px-2 py-0.5 text-zinc-600 transition-colors duration-300 hover:text-zinc-950 dark:text-zinc-400 dark:hover:text-zinc-50" > {tab} </button> </AnimatedBackgroundItem> {/each} </AnimatedBackground></div>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.
animated-card-background-hover-example.svelte
<script lang="ts"> import { AnimatedBackground, AnimatedBackgroundItem, } from "$lib/components/magic/animated-background/index.js";
const items = [ { title: "Dialog", description: "Enhances modal presentations." }, { title: "Popover", description: "For small interactive overlays." }, { title: "Accordion", description: "Collapsible sections for more content.", }, { title: "Collapsible", description: "Collapsible sections for more content.", }, { title: "Drag to Reorder", description: "Reorder items with drag and drop.", }, { title: "Swipe to Delete", description: "Delete items with swipe gestures.", }, ];</script>
<div class="grid grid-cols-2 p-10 md:grid-cols-3"> <AnimatedBackground class="rounded-lg bg-zinc-100 dark:bg-zinc-800" transition={{ type: "spring", bounce: 0.2, duration: 0.6 }} enableHover > {#each items as item, index (item.title)} <AnimatedBackgroundItem data-id={`card-${index}`}> <div class="flex flex-col space-y-1 p-4 select-none"> <h3 class="text-base font-medium text-zinc-800 dark:text-zinc-50"> {item.title} </h3> <p class="text-base text-zinc-600 dark:text-zinc-400"> {item.description} </p> </div> </AnimatedBackgroundItem> {/each} </AnimatedBackground></div>Segmented Control
segmented-control-example.svelte
<script lang="ts"> import { AnimatedBackground, AnimatedBackgroundItem, } from "$lib/components/magic/animated-background/index.js";
const labels = ["Day", "Week", "Month", "Year"];</script>
<div class="rounded-xl bg-gray-100 p-1 dark:bg-zinc-800"> <AnimatedBackground defaultValue="Day" class="rounded-lg bg-white dark:bg-zinc-700" transition={{ ease: "easeInOut", duration: 0.2 }} > {#each labels as label (label)} <AnimatedBackgroundItem data-id={label}> <button type="button" aria-label={`${label} view`} class="inline-flex w-20 items-center justify-center text-center text-zinc-800 transition-transform active:scale-[0.98] dark:text-zinc-50" > {label} </button> </AnimatedBackgroundItem> {/each} </AnimatedBackground></div>Props
Animated Background
The root component that manages the active item and shared background animation.
| Name | Type | Default | Description |
|---|---|---|---|
children | Snippet | - | The animated background items to render. |
defaultValue | string | undefined | The ID of the initially active item. |
onValueChange | (newActiveId: string | null) => void | undefined | Called when the active item changes. |
class | ClassValue | "" | CSS classes applied to the animated background element. |
transition | Options["transition"] | undefined | Motion transition options for the shared background. |
enableHover | boolean | false | Activates items on hover instead of click. |
Animated Background Item
An individual selectable item that participates in the shared background animation.
| Name | Type | Default | Description |
|---|---|---|---|
data-id | string | required | A unique ID used to track the active item. |
class | ClassValue | "" | Additional CSS classes applied to the item wrapper. |
children | Snippet | - | The item content to render. |