Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
Stagger Text
A description for Stagger Text component.
motion-sv
Stagger Text!!
S St ta ag gg ge er r T Te ex xt t! !! !
stagger-text.svelte
<script lang="ts"> import { StaggerText } from "$lib/components/magic/stagger-text/index.js";</script>
<div> <StaggerText text="Stagger Text!!" /></div>
<style></style>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/stagger-text.json
Install dependencies
Install the required packages for this component:
pnpm add motion-sv
Copy the Source Code
Copy and paste the following code into your project:
stagger-text.svelte
<script lang="ts" module> export const variants: Variants = { initial: { y: 0 }, animate: (i: number) => ({ y: "-100%", transition: { delay: i * 0.04, duration: 0.4, ease: "easeInOut", type: "tween" as const, }, }), exit: (i: number) => ({ y: 0, transition: { delay: i * 0.02, duration: 0.3 }, }), };</script>
<script lang="ts"> import { motion, type Variants } from "motion-sv"; import type { ClassValue } from "svelte/elements"; import { cn } from "#lib/utils.js";
type StaggerTextElement = "span" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6";
interface StaggerTextProps { text: string; class?: ClassValue; as?: StaggerTextElement; }
let { text = "text", class: className, as = "h3" }: StaggerTextProps = $props();
let MotionComponent = $derived(motion[as]);
let activeIndex = $state(5); let isActive = $state(false);
const handleCopy = (e: ClipboardEvent) => { e.preventDefault(); if (e.clipboardData) { e.clipboardData.setData("text/plain", text); } };</script>
<span class="sr-only"> {text}</span><MotionComponent class={cn("overflow-clip tracking-wide select-text", className)} aria-label={text} oncopy={handleCopy}> {#each text.split("") as char, i (i)} {@const delay = Math.abs(activeIndex - i)} <motion.span class="relative inline-flex flex-col" role="presentation" onmouseenter={() => { activeIndex = i; isActive = true; }} onmouseleave={() => { activeIndex = -1; isActive = false; }} > <!-- Original text --> <motion.span class="h-fit select-none" aria-hidden="true" {variants} custom={delay} initial="initial" animate={isActive ? "animate" : "exit"} > {char === " " ? " " : char} </motion.span>
<!-- Copy text (animated from below) --> <motion.span class="absolute top-[0] left-0 h-fit w-full select-text" style={{ translate: "0 100%" }} aria-hidden="true" {variants} custom={delay} initial="initial" animate={isActive ? "animate" : "exit"} > {char === " " ? " " : char} </motion.span> </motion.span> {/each}</MotionComponent> index.ts
export { default as StaggerText } from "./stagger-text.svelte";Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── stagger-text/ ├── stagger-text.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/stagger-text/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 { StaggerText } from "#lib/components/magic/stagger-text/index.js";</script>
<StaggerText text="Staggered text" />Props
Stagger Text
A component for Stagger Text.
| Name | Type | Default | Description |
|---|---|---|---|
class | ClassValue | "" | Additional CSS classes to apply |