Custom pointer
Swaps the mouse pointer for your own shape inside an area.
Swaps the mouse pointer for your own shape inside an area.
npx shadcn@latest add @magicui/pointer
npm install motion
First time with Magic UI? Set it up once.
/**
* Component from Magic UI, https://magicui.design
* MIT License, Copyright (c) Magic UI. Full text: https://github.com/magicuidesign/magicui/blob/main/LICENSE.md
*/
"use client"
import { useEffect, useRef, useState } from "react"
import {
AnimatePresence,
motion,
useMotionValue,
type HTMLMotionProps,
} from "motion/react"
import { cn } from "@/lib/utils"
/**
* A custom pointer component that displays an animated cursor.
* Add this as a child to any component to enable a custom pointer when hovering.
* You can pass custom children to render as the pointer.
*
* @component
* @param {HTMLMotionProps<"div">} props - The component props
*/
export function Pointer({
className,
style,
children,
...props
}: HTMLMotionProps<"div">): React.ReactNode {
const x = useMotionValue(0)
const y = useMotionValue(0)
const [isActive, setIsActive] = useState<boolean>(false)
const containerRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const parentElement =
typeof window !== "undefined"
? (containerRef.current?.parentElement ?? null)
: null
const handleMouseMove = (e: MouseEvent) => {
x.set(e.clientX)
y.set(e.clientY)
setIsActive(true)
}
const handleMouseEnter = (e: MouseEvent) => {
x.set(e.clientX)
y.set(e.clientY)
setIsActive(true)
}
const handleMouseLeave = () => {
setIsActive(false)
}
if (parentElement) {
parentElement.style.cursor = "none"
parentElement.addEventListener("mousemove", handleMouseMove)
parentElement.addEventListener("mouseenter", handleMouseEnter)
parentElement.addEventListener("mouseleave", handleMouseLeave)
}
return () => {
if (parentElement) {
parentElement.style.cursor = ""
parentElement.removeEventListener("mousemove", handleMouseMove)
parentElement.removeEventListener("mouseenter", handleMouseEnter)
parentElement.removeEventListener("mouseleave", handleMouseLeave)
}
}
}, [x, y])
return (
<>
<div ref={containerRef} />
<AnimatePresence>
{isActive && (
<motion.div
className="pointer-events-none fixed z-50 transform-[translate(-50%,-50%)]"
style={{
top: y,
left: x,
...style,
}}
initial={{
scale: 0,
opacity: 0,
}}
animate={{
scale: 1,
opacity: 1,
}}
exit={{
scale: 0,
opacity: 0,
}}
{...props}
>
{children || (
<svg
stroke="currentColor"
fill="currentColor"
strokeWidth="1"
viewBox="0 0 16 16"
height="24"
width="24"
xmlns="http://www.w3.org/2000/svg"
className={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>
)}
</motion.div>
)}
</AnimatePresence>
</>
)
}
/**
* Demo from Magic UI, https://magicui.design
* MIT License, Copyright (c) Magic UI. Full text: https://github.com/magicuidesign/magicui/blob/main/LICENSE.md
* Changed for Tastebud: pointer colours from the Figma palette.
*/
"use client"
import { motion } from "motion/react"
import { Pointer } from "@/components/ui/pointer"
export default function PointerDemo1() {
return (
<div className="grid grid-cols-1 gap-6 md:grid-cols-2 md:grid-rows-2">
<div className="border-border rounded-lg border p-4">
<div className="relative flex h-40 flex-col items-center justify-center">
<h3 className="text-xl font-semibold">Animated Pointer</h3>
<p className="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"
className="text-[#F24E1E]"
>
<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 className="border-border rounded-lg border p-4">
<div className="relative flex h-40 flex-col items-center justify-center">
<h3 className="text-xl font-semibold">Colored Pointer</h3>
<p className="text-muted-foreground text-sm">
A custom pointer with different color
</p>
</div>
<Pointer className="fill-[#1ABCFE]" />
</div>
<div className="border-border rounded-lg border p-4">
<div className="relative flex h-40 flex-col items-center justify-center">
<h3 className="text-xl font-semibold">Custom Shape</h3>
<p className="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" className="fill-[#A259FF]" />
<circle cx="12" cy="12" r="5" className="fill-white" />
</svg>
</Pointer>
</div>
<div className="border-border rounded-lg border p-4">
<div className="relative flex h-40 flex-col items-center justify-center">
<h3 className="text-xl font-semibold">Emoji Pointer</h3>
<p className="text-muted-foreground text-sm">
Using an emoji as a custom pointer
</p>
</div>
<Pointer>
<div className="text-2xl">👆</div>
</Pointer>
</div>
</div>
)
}
Changed for this site: Pointer colours from the Figma palette.