Skip to content

Conversation

Xav1erSue
Copy link

@Xav1erSue Xav1erSue commented Apr 29, 2025

[中文版模板 / Chinese template]

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

[useClickAway] 支持传入事件监听选项 #2770

💡 Background and solution

背景:如果有元素的点击事件中阻止了冒泡,会导致 useClickAway 无法捕捉到此次点击

代码:

import React, { useState, useRef } from "react";
import { useClickAway } from "ahooks";

export default () => {
  const [counter, setCounter] = useState(0);

  const ref = useRef<HTMLButtonElement>(null);

  useClickAway(() => setCounter((s) => s + 1), ref);

  return (
    <div>
      <button ref={ref} type="button">
        box1
      </button>
      <button
        type="button"
        style={{ marginLeft: 16 }}
        onClick={(e) => e.stopPropagation()}
      >
        box2
      </button>
      <p>counter: {counter}</p>
    </div>
  );
};

期望:支持传入事件监听选项如 capture 等参数,改变事件捕获时机

📝 Changelog

Language Changelog
🇺🇸 English feat: useClickAway supports passing options as the third argument to addEventListner
🇨🇳 Chinese feat: useClickAway 支持传入 options 作为 addEventListner 的第三个参数

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@Xav1erSue Xav1erSue force-pushed the feat/useClickAway_eventOptions branch from 66ebcdc to 8efa3fc Compare April 29, 2025 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants