Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
A spotlight effect that follows your mouse cursor and highlights borders on hover.
motion-sv
Login
Enter your credentials to access your account
components
<script> import { MagicCard } from "#lib/components/magic/magic-card/index.js"; import { Button } from "#lib/components/ui/button/index.js"; import * as Card from "#lib/components/ui/card/index.js"; import { Input } from "#lib/components/ui/input/index.js"; import { Label } from "#lib/components/ui/label/index.js"; import { mode } from "mode-watcher";</script>
<Card.Root class="w-full max-w-sm border-none p-0 shadow-none"> <MagicCard gradientSize={160} gradientColor={mode.current === "dark" ? "#262626" : "#D9D9D955"} class="p-0" > <Card.Header class="border-border border-b p-4 [.border-b]:pb-4"> <Card.Title>Login</Card.Title> <Card.Description>Enter your credentials to access your account</Card.Description> </Card.Header> <Card.Content class="p-4"> <form> <div class="grid gap-4"> <div class="grid gap-2"> <Label for="email">Email</Label> <Input id="email" type="email" placeholder="name@example.com" /> </div> <div class="grid gap-2"> <Label for="password">Password</Label> <Input id="password" type="password" /> </div> </div> </form> </Card.Content> <Card.Footer class="border-border border-t p-4 [.border-t]:pt-4"> <Button class="w-full">Sign In</Button> </Card.Footer> </MagicCard></Card.Root>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/magic-card.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:
magic-card.svelte
<script lang="ts"> import type { ClassValue } from "svelte/elements"; import { motion, useMotionTemplate, useMotionValue } from "motion-sv"; import { cn } from "#lib/utils.js"; import type { Snippet } from "svelte";
interface MagicCardProps { /** Card content */ children?: Snippet; /** Additional CSS classes */ class?: ClassValue; /** Size of the gradient circle */ gradientSize?: number; /** Color of the gradient overlay */ gradientColor?: string; /** Opacity of the gradient overlay */ gradientOpacity?: number; /** Starting color of the border gradient */ gradientFrom?: string; /** Ending color of the border gradient */ gradientTo?: string; }
let { children, class: className, gradientSize = 200, gradientColor = "#262626", gradientOpacity = 0.8, gradientFrom = "#9E7AFF", gradientTo = "#FE8BBB", }: MagicCardProps = $props();
let mouseX = $derived(useMotionValue(-gradientSize)); let mouseY = $derived(useMotionValue(-gradientSize));
let borderGradient = $derived( useMotionTemplate`radial-gradient(${gradientSize}px circle at ${mouseX}px ${mouseY}px, ${gradientFrom}, ${gradientTo}, var(--border) 100%)` ); let overlayGradient = $derived( useMotionTemplate`radial-gradient(${gradientSize}px circle at ${mouseX}px ${mouseY}px, ${gradientColor}, transparent 100%)` );
const reset = () => { mouseX.set(-gradientSize); mouseY.set(-gradientSize); };
const handlePointerMove = (e: PointerEvent) => { const target = e.currentTarget as HTMLElement; const rect = target.getBoundingClientRect(); mouseX.set(e.clientX - rect.left); mouseY.set(e.clientY - rect.top); };
// Reset on mount $effect(() => { reset(); });
// Global event listeners $effect(() => { const handleGlobalPointerOut = (e: PointerEvent) => { if (!e.relatedTarget) { reset(); } };
const handleVisibility = () => { if (document.visibilityState !== "visible") { reset(); } };
window.addEventListener("pointerout", handleGlobalPointerOut); window.addEventListener("blur", reset); document.addEventListener("visibilitychange", handleVisibility);
return () => { window.removeEventListener("pointerout", handleGlobalPointerOut); window.removeEventListener("blur", reset); document.removeEventListener("visibilitychange", handleVisibility); }; });</script>
<div class={cn("group relative rounded-[inherit]", className)} onpointermove={handlePointerMove} onpointerleave={reset} onpointerenter={reset} role="region"> <motion.div class="bg-border pointer-events-none absolute inset-0 rounded-[inherit] duration-300 group-hover:opacity-100" style={{ background: borderGradient }} /> <div class="bg-background absolute inset-px rounded-[inherit]"></div> <motion.div class="pointer-events-none absolute inset-px rounded-[inherit] opacity-0 transition-opacity duration-300 group-hover:opacity-100" style={{ background: overlayGradient, opacity: gradientOpacity }} /> <div class="relative"> {#if children} {@render children()} {/if} </div></div> index.ts
export { default as MagicCard } from "./magic-card.svelte";Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── magic-card/ ├── magic-card.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/magic-card/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 { MagicCard } from "#lib/components/magic/magic-card/index.js";</script>
<MagicCard class="rounded-xl p-8"> Hover over this card</MagicCard>Props
Magic Card
A component for Magic Card.
| Name | Type | Default | Description |
|---|---|---|---|
children | Snippet | | Card content. |
class | ClassValue | "" | Additional CSS classes. |
gradientSize | number | 200 | Size of the gradient circle. |
gradientColor | string | "#262626" | Color of the gradient overlay. |
gradientOpacity | number | 0.8 | Opacity of the gradient overlay. |
gradientFrom | string | "#9E7AFF" | Starting color of the border gradient. |
gradientTo | string | "#FE8BBB" | Ending color of the border gradient. |