Pointer
svelte-animations/pointerFreeBlock
A component that displays a pointer when hovering over an element
Install it
npx shadcn@latest add https://sv-animations.vercel.app/r/pointer.jsonSource
1<script lang="ts">2 import { motion, AnimatePresence, useMotionValue } from "motion-sv";3 import { cn } from "$UTILS$";4 import { type Snippet } from "svelte";5 import { watch } from "runed";6 // import type { HTMLMotionProps } from "motion-sv";78 interface PointerProps {9 children?: Snippet;10 class?: string;11 }1213 let { children, class: className }: PointerProps = $props();1415 const x = useMotionValue(0);16 const y = useMotionValue(0);17 let isActive = $state(false);18 let containerRef: HTMLDivElement | null = $state(null);1920 watch([() => x, () => y], () => {21 if (typeof window !== "undefined" && containerRef) {22 // Get the parent element directly from the ref23 const parentElement = containerRef.parentElement;2425 if (parentElement) {26 console.log("Parent element found:", parentElement);2728 // Add cursor-none to parent29 parentElement.style.cursor = "none";3031 // Add event listeners to parent32 const handleMouseMove = (e: MouseEvent) => {33 // console.log("Mouse move event:", e);34 // console.log("Mouse coordinates:", e.clientX, e.clientY);35 console.log(x, y);36 x.set(e.clientX);37 y.set(e.clientY);38 isActive = true;39 };4041 const handleMouseEnter = (e: MouseEvent) => {42 x.set(e.clientX);43 y.set(e.clientY);4445 isActive = true;46 };4748 const handleMouseLeave = () => {49 isActive = false;50 };5152 parentElement.addEventListener("mousemove", handleMouseMove);53 parentElement.addEventListener("mouseenter", handleMouseEnter);54 parentElement.addEventListener("mouseleave", handleMouseLeave);5556 return () => {57 parentElement.style.cursor = "";58 parentElement.removeEventListener("mousemove", handleMouseMove);59 parentElement.removeEventListener("mouseenter", handleMouseEnter);60 parentElement.removeEventListener("mouseleave", handleMouseLeave);61 };62 }63 }64 });65</script>6667<div bind:this={containerRef}></div>68<AnimatePresence>69 {#if isActive}70 <motion.div71 class="pointer-events-none fixed z-50 transform-[translate(-50%,-50%)]"72 style={{73 top: y,74 left: x,75 }}76 initial={{77 scale: 0,78 opacity: 0,79 }}80 animate={{81 scale: 1,82 opacity: 1,83 }}84 exit={{85 scale: 0,86 opacity: 0,87 }}88 >89 {#if children}90 {@render children()}91 {:else}92 <svg93 stroke="currentColor"94 fill="currentColor"95 stroke-width="1"96 viewBox="0 0 16 16"97 height="24"98 width="24"99 xmlns="http://www.w3.org/2000/svg"100// … truncated
What it pulls in
npm packages
Files it writes
More from Svelte Animations
All 66 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Animated Beamanimated-beam | Svelte Animations | Blocks | Free | 1 dep · 6 files | |
| Animated Circular Progress Baranimated-circular-progress-bar | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Animated Gradient Textanimated-gradient-text | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Animated Grid Patternanimated-grid-pattern | Svelte Animations | Blocks | Free | 1 dep · 2 files | |
| Animated Listanimated-list | Svelte Animations | Blocks | Free | 1 dep · 2 files | |
| Animated Shiny Textanimated-shiny-text | Svelte Animations | Blocks | Free | no deps | |
| Animated Theme Toggleranimated-theme-toggler | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Arc Timelinearc-timeline | Svelte Animations | Blocks | Free | no deps · 3 files |
