useOnLongHover

About

A hook to detect long hover on an element.

Examples

Basic example

import { useOnLongHover } from "rooks";
export default function App() {
  const handleLongHover = () => {
    console.log("Long hover detected");
  };

  const longHoverRef = useOnLongHover(handleLongHover);

  return (
    <>
      <p>Hover on the button for 500ms to trigger a console.log statement</p>
      <button ref={longHoverRef}>Long hover me</button>
    </>
  );
}

Custom duration and mouse leave behavior

import { useOnLongHover } from "rooks";
export default function App() {
  const handleLongHover = () => {
    console.log("Long hover detected");
  };

  const handleMouseLeave = () => {
    console.log("Mouse left before long hover completed");
  };

  const longHoverRef = useOnLongHover(handleLongHover, {
    duration: 1000,
  });

  return (
    <>
      <p>Hover on the button for 1000ms to trigger a console.log statement</p>
      <button ref={longHoverRef}>Long hover me</button>
    </>
  );
}

Arguments

Argument valueTypeDescriptionDefault
callbackFunctionCallback function to be called when long hover is detectedundefined
optionsObjectSee table belowundefined
Options valueTypeDescriptionDefault
durationNumberThe duration (in ms) after which long hover is detected500

Returns

Return valueTypeDescriptionDefault
refCallback RefA ref that can be used on the element you want to detect long hover onundefined

Join the community!

Join our discord server! You can click the floating discord icon at the bottom right of the screen and chat with us!

Powered by vercel