UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

1 lines 1.49 kB
{"version":3,"file":"index.cjs","names":[],"sources":["../../../../src/hooks/use-hover/index.ts"],"sourcesContent":["\"use client\"\n\nimport { useCallback, useEffect, useRef, useState } from \"react\"\n\n/**\n * `useHover` is a custom hook that detects whether the pointer has moved over or away from an element.\n *\n * @see https://yamada-ui.com/docs/hooks/use-hover\n */\nexport const useHover = <Y extends HTMLElement = HTMLDivElement>() => {\n const [hovered, setHovered] = useState(false)\n const ref = useRef<Y>(null)\n\n const onMouseEnter = useCallback(() => setHovered(true), [])\n const onMouseLeave = useCallback(() => setHovered(false), [])\n\n useEffect(() => {\n const el = ref.current\n\n if (el) {\n el.addEventListener(\"mouseenter\", onMouseEnter)\n el.addEventListener(\"mouseleave\", onMouseLeave)\n\n return () => {\n el.removeEventListener(\"mouseenter\", onMouseEnter)\n el.removeEventListener(\"mouseleave\", onMouseLeave)\n }\n }\n }, [onMouseEnter, onMouseLeave])\n\n return { ref, hovered }\n}\n"],"mappings":";;;;;;;;;;;;;AASA,MAAa,iBAAyD;CACpE,MAAM,CAAC,SAAS,kCAAuB,MAAM;CAC7C,MAAM,wBAAgB,KAAK;CAE3B,MAAM,4CAAiC,WAAW,KAAK,EAAE,EAAE,CAAC;CAC5D,MAAM,4CAAiC,WAAW,MAAM,EAAE,EAAE,CAAC;AAE7D,4BAAgB;EACd,MAAM,KAAK,IAAI;AAEf,MAAI,IAAI;AACN,MAAG,iBAAiB,cAAc,aAAa;AAC/C,MAAG,iBAAiB,cAAc,aAAa;AAE/C,gBAAa;AACX,OAAG,oBAAoB,cAAc,aAAa;AAClD,OAAG,oBAAoB,cAAc,aAAa;;;IAGrD,CAAC,cAAc,aAAa,CAAC;AAEhC,QAAO;EAAE;EAAK;EAAS"}