Overview
Components
Text Animations
Text with sliding background highlight animation using mix-blend-mode.
motion-sv
You can just ship things.
preview.svelte
<script lang="ts"> import { HighlightedText } from "$lib/components/spell/highlighted-text/index.js";</script>
<div class="flex items-center text-2xl font-medium tracking-[-.03em] md:text-4xl"> You <HighlightedText>can</HighlightedText> just <HighlightedText delay={0.4} from="left">ship things.</HighlightedText></div>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/s/highlighted-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:
highlighted-text.svelte
<script lang="ts"> import { cn } from "#lib/utils.js"; import { motion, useInView, type AnimationOptions, type Variants } from "motion-sv"; import type { Snippet } from "svelte"; import type { ClassValue, HTMLAttributes } from "svelte/elements";
type From = "left" | "right" | "top" | "bottom";
interface HighlightedTextProps extends HTMLAttributes<HTMLSpanElement> { children?: Snippet; class?: ClassValue; from?: From; delay?: number; triggerOnView?: boolean; once?: boolean; }
const transition: AnimationOptions = { type: "spring", damping: 30, stiffness: 300, };
const fromVariants: Record<From, Variants> = { left: { hidden: { x: "-100%" }, visible: { x: "0%" }, }, right: { hidden: { x: "100%" }, visible: { x: "0%" }, }, top: { hidden: { y: "-100%" }, visible: { y: "0%" }, }, bottom: { hidden: { y: "100%" }, visible: { y: "0%" }, }, };
let { children, class: className, from = "bottom", delay = 0, triggerOnView = false, once = true, ...props }: HighlightedTextProps = $props();
let element = $state<HTMLSpanElement | null>(null); let view = useInView( () => (triggerOnView ? element : null)!, () => ({ once, amount: 0.2 }) as any );
const variants = $derived(fromVariants[from]); const isVisible = $derived(triggerOnView ? view.current : true);</script>
<span bind:this={element} class={cn("relative inline-flex overflow-hidden align-baseline", className)} {...props}> <motion.span class="absolute inset-0 -right-[0.18em] -left-[0.15em] z-0 bg-black dark:bg-white" initial="hidden" animate={isVisible ? "visible" : "hidden"} {variants} transition={{ ...transition, delay }} /> <span class="relative z-10 pr-[0.18em] pl-[0.15em] text-white mix-blend-difference"> {@render children?.()} </span></span> index.ts
import HighlightedText from "./highlighted-text.svelte";export { HighlightedText };Folder Structure
Folder Structure
src/└── lib/ └── components/ └── spell/ └── highlighted-text/ ├── highlighted-text.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/spell/highlighted-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 { HighlightedText } from "#lib/components/spell/highlighted-text/index.js";</script>
<HighlightedText>Important</HighlightedText>Examples
Direction And View Trigger
Change the sweep origin and wait for viewport entry when you want the emphasis to arrive with surrounding content.
From Left
From Right
From Top
From Bottom
direction-example.svelte
<script lang="ts"> import { HighlightedText } from "$lib/components/spell/highlighted-text/index.js";</script>
<div class="flex flex-col gap-6 text-xl font-semibold tracking-tight"> <div> <HighlightedText from="left" delay={0} triggerOnView>From Left</HighlightedText> </div> <div> <HighlightedText from="right" delay={0.2} triggerOnView>From Right</HighlightedText> </div> <div> <HighlightedText from="top" delay={0.4} triggerOnView>From Top</HighlightedText> </div> <div> <HighlightedText from="bottom" delay={0.6} triggerOnView>From Bottom</HighlightedText> </div></div>Props
| Name | Type | Default | Description |
|---|---|---|---|
children | Snippet | undefined | required | The inline text content wrapped by the animated highlight. |
from | "left" | "right" | "top" | "bottom" | "bottom" | Controls the direction the highlight bar sweeps in from. |
delay | number | 0 | Adds a delay in seconds before the highlight motion begins. |
triggerOnView | boolean | false | Waits to animate until the component enters the viewport. |
once | boolean | true | When using triggerOnView, controls whether the animation runs only on first entry. |
class | ClassValue | | Custom classes applied to the outer inline wrapper. |