Guest User

Highlight

a guest
Feb 5th, 2023
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import type { ComponentType } from "react"
  2. import React, { useState, useRef } from "react"
  3. import { motion } from "framer-motion"
  4.  
  5. function calculateRelativePosition(event, referenceElement) {
  6.     return {
  7.         x:
  8.             event.pageX -
  9.             referenceElement.offsetLeft -
  10.             (referenceElement.offsetParent
  11.                 ? referenceElement.offsetParent.offsetLeft
  12.                 : 0),
  13.         y:
  14.             event.pageY -
  15.             referenceElement.offsetTop -
  16.             (referenceElement.offsetParent
  17.                 ? referenceElement.offsetParent.offsetTop
  18.                 : 0),
  19.     }
  20. }
  21.  
  22. export function Highlight(Component): ComponentType {
  23.     const style = {
  24.         position: "absolute",
  25.         width: "100%",
  26.         height: "100%",
  27.         zIndex: 0,
  28.         boxSizing: "border-box",
  29.         borderRadius: "inherit",
  30.         WebkitMaskImage: `radial-gradient(circle at center, rgb(255, 255, 255) 0%, rgba(0, 0, 0, 0) 100%)`,
  31.         opacity: 0,
  32.         backgroundImage: `linear-gradient(to right, rgb(77, 104, 243), rgb(138, 97, 255))`,
  33.     }
  34.  
  35.     return (props) => {
  36.         const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 })
  37.         const boxRef = useRef()
  38.         const handleMouseMove = (e) =>
  39.             setMousePosition(calculateRelativePosition(e, boxRef.current))
  40.  
  41.         return (
  42.             <Component {...props} ref={boxRef} onMouseMove={handleMouseMove}>
  43.                 <motion.div
  44.                     style={style}
  45.                     whileHover={{ opacity: 1 }}
  46.                     animate={{
  47.                         WebkitMaskImage: `radial-gradient(circle at ${mousePosition.x}px ${mousePosition.y}px, rgb(255, 255, 255) 0%, rgba(0, 0, 0, 0) 100%)`,
  48.                     }}
  49.                     transition={{ duration: 0.0 }}
  50.                 />
  51.                 {props.children}
  52.             </Component>
  53.         )
  54.     }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment