Components
This code implements a reusable React component called PointerHighlight that adds an animated highlight effect to any element. It solves the problem of visually emphasizing interactive elements, improving user experience and engagement. The component works by using the useRef and useEffect hooks to track the dimensions of the target element and trigger animations when it's in the viewport. useInView from motion/react detects when the element is visible. A ResizeObserver ensures accurate dimensions even after resizing. The getVariantStyles function dynamically applies different visual styles based on the selected variant ('default', 'gradient', 'neon', 'minimal', 'premium'). Each variant features unique animations, including border expansion, corner accents, glow effects, and a pointer with a trail effect. The motion library from Framer Motion handles the animations, providing smooth transitions and easing functions. The component uses Tailwind CSS for styling and cn for efficient class name concatenation. Example scenarios include highlighting interactive elements in dashboards, cards, buttons, and other UI components to guide user interaction and create a more engaging interface. Developers can customize the appearance using props like variant, glowEffect, and animationDelay. The component also handles responsiveness by adapting to changes in element dimensions.
Loading preview...
import { PointerHighlight } from "@/components/ui/animated-pointer-highlight";
export default function DemoOne() {
return (
<div className="mx-auto max-w-lg py-20 text-2xl font-bold tracking-tight md:text-4xl">
The best way to grow is to
<PointerHighlight>
<span className="px-2">Get viral</span>
</PointerHighlight>
</div>
);
}