Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] useMouseInElement #2615

Open
zhuyue6 opened this issue Jul 30, 2024 · 1 comment
Open

[RFC] useMouseInElement #2615

zhuyue6 opened this issue Jul 30, 2024 · 1 comment
Labels
feature New feature or request

Comments

@zhuyue6
Copy link

zhuyue6 commented Jul 30, 2024

用于对一个元素进行监听:鼠标是否在停留在该元素上,并提供回调函数,可以在鼠标放到元素上触发动作,比如触发动画回调,还有更复杂一些的场景弹出一个浮框内容,浮框跟着鼠标在该元素运动

API

function  useMouseInElement(target?: BasicTarget, inCallback?: (result: Result) => void, outCallback?: (result: Result) => void)

Demo

import { useMouseInElement } from 'ahooks';
import React, { useRef } from 'react';

const App: React.FC = () => {
  const ref = useRef<HTMLDivElement>(null);
  const { isInside } = useMouseInElement(
    ref.current,
    () => {
      console.log('inside');
    },
    () => {
      console.log('outside');
    },
  );

  return (
    <div>
      <div
        ref={ref}
        style={{ width: 200, height: 200, padding: 12, border: '1px solid #000', marginBottom: 8 }}
      >
        isInside:{String(isInside)}
      </div>
    </div>
  );
};

export default App;
@crazylxr crazylxr added the feature New feature or request label Aug 5, 2024
@zhuyue6
Copy link
Author

zhuyue6 commented Aug 22, 2024

你好,这个feature or pr 有啥问题不, 具体的场景是mui 项目,实现类似antd Popover相关功能需要用到这个钩子

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants