Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
A description for Pointer component.
motion-svruned
Animated Pointer
Animated pointer
Colored Pointer
A custom pointer with different color
Custom Shape
A pointer with a custom SVG shape
Emoji Pointer
Using an emoji as a custom pointer
pointer.svelte
<script> import { Pointer } from "$lib/components/magic/pointer/index.js"; import { motion } from "motion-sv";</script>
<div class="grid grid-cols-1 gap-6 md:grid-cols-2 md:grid-rows-2"> <div class="border-border rounded-lg border p-4"> <div class="relative flex h-40 flex-col items-center justify-center"> <h3 class="text-xl font-semibold">Animated Pointer</h3> <p class="text-muted-foreground text-sm">Animated pointer</p> </div> <Pointer> <motion.div animate={{ scale: [0.8, 1, 0.8], rotate: [0, 5, -5, 0], }} transition={{ duration: 1.5, repeat: Infinity, ease: "easeInOut", }} > <svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg" class="text-pink-600" > <motion.path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z" fill="currentColor" animate={{ scale: [1, 1.2, 1] }} transition={{ duration: 0.8, repeat: Infinity, ease: "easeInOut", }} /> </svg> </motion.div> </Pointer> </div> <div class="border-border rounded-lg border p-4"> <div class="relative flex h-40 flex-col items-center justify-center"> <h3 class="text-xl font-semibold">Colored Pointer</h3> <p class="text-muted-foreground text-sm">A custom pointer with different color</p> </div> <Pointer class="fill-blue-500" /> </div> <div class="border-border rounded-lg border p-4"> <div class="relative flex h-40 flex-col items-center justify-center"> <h3 class="text-xl font-semibold">Custom Shape</h3> <p class="text-muted-foreground text-sm">A pointer with a custom SVG shape</p> </div> <Pointer> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" > <circle cx="12" cy="12" r="10" class="fill-purple-500" /> <circle cx="12" cy="12" r="5" class="fill-white" /> </svg> </Pointer> </div> <div class="border-border rounded-lg border p-4"> <div class="relative flex h-40 flex-col items-center justify-center"> <h3 class="text-xl font-semibold">Emoji Pointer</h3> <p class="text-muted-foreground text-sm">Using an emoji as a custom pointer</p> </div> <Pointer> <div class="text-2xl">👆</div> </Pointer> </div></div>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/pointer.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:
pointer.svelte
<script lang="ts"> import type { ClassValue } from "svelte/elements"; import { motion, AnimatePresence, useMotionValue } from "motion-sv"; import { cn } from "#lib/utils.js"; import { type Snippet } from "svelte"; import { watch } from "runed"; // import type { HTMLMotionProps } from "motion-sv";
interface PointerProps { children?: Snippet; class?: ClassValue; }
let { children, class: className }: PointerProps = $props();
const x = useMotionValue(0); const y = useMotionValue(0); let isActive = $state(false); let containerRef: HTMLDivElement | null = $state(null);
watch([() => x, () => y], () => { if (typeof window !== "undefined" && containerRef) { // Get the parent element directly from the ref const parentElement = containerRef.parentElement;
if (parentElement) { console.log("Parent element found:", parentElement);
// Add cursor-none to parent parentElement.style.cursor = "none";
// Add event listeners to parent const handleMouseMove = (e: MouseEvent) => { // console.log("Mouse move event:", e); // console.log("Mouse coordinates:", e.clientX, e.clientY); console.log(x, y); x.set(e.clientX); y.set(e.clientY); isActive = true; };
const handleMouseEnter = (e: MouseEvent) => { x.set(e.clientX); y.set(e.clientY);
isActive = true; };
const handleMouseLeave = () => { isActive = false; };
parentElement.addEventListener("mousemove", handleMouseMove); parentElement.addEventListener("mouseenter", handleMouseEnter); parentElement.addEventListener("mouseleave", handleMouseLeave);
return () => { parentElement.style.cursor = ""; parentElement.removeEventListener("mousemove", handleMouseMove); parentElement.removeEventListener("mouseenter", handleMouseEnter); parentElement.removeEventListener("mouseleave", handleMouseLeave); }; } } });</script>
<div bind:this={containerRef}></div><AnimatePresence> {#if isActive} <motion.div class="pointer-events-none fixed z-50 transform-[translate(-50%,-50%)]" style={{ top: y, left: x, }} initial={{ scale: 0, opacity: 0, }} animate={{ scale: 1, opacity: 1, }} exit={{ scale: 0, opacity: 0, }} > {#if children} {@render children()} {:else} <svg stroke="currentColor" fill="currentColor" stroke-width="1" viewBox="0 0 16 16" height="24" width="24" xmlns="http://www.w3.org/2000/svg" class={cn("rotate-[-70deg] stroke-white text-black", className)} > <path d="M14.082 2.182a.5.5 0 0 1 .103.557L8.528 15.467a.5.5 0 0 1-.917-.007L5.57 10.694.803 8.652a.5.5 0 0 1-.006-.916l12.728-5.657a.5.5 0 0 1 .556.103z" /> </svg> {/if} </motion.div> {/if}</AnimatePresence> index.ts
export { default as Pointer } from "./pointer.svelte";Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── pointer/ ├── pointer.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/pointer/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 { Pointer } from "#lib/components/magic/pointer/index.js";</script>
<div class="relative h-48 rounded-xl border p-8"> Move your pointer here <Pointer /></div>Props
Pointer
A component for Pointer.
| Name | Type | Default | Description |
|---|---|---|---|
class | ClassValue | "" | Additional CSS classes to apply |