react-use icon indicating copy to clipboard operation
react-use copied to clipboard

fix(useClickAway): Fix click on self in shadowDOM will also trigger a callback

Open molvqingtai opened this issue 2 years ago • 1 comments

Description

<custom-element>
   #shadow-root
      <button ref={buttonRef}>button</button>
   #shadow-root
</custom-element>
useClickAway(
   buttonRef,
  (e) => {
    console.log(e.target) // custom-element
  },
  ['click']
)

As shown above, clicking the button in shadowDOM will also incorrectly trigger the callback, because in shadowDOM, the target has been forwarded to :host, that is, custom-element, and events within the shadowDOM are not visible to the outside. This PR will solve the problem.

Reference: https://javascript.info/shadow-dom-events

Type of change

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as before)

Checklist

  • [x] Read the Contributing Guide
  • [x] Perform a code self-review
  • [x] Comment the code, particularly in hard-to-understand areas
  • [ ] Add documentation
  • [ ] Add hook's story at Storybook
  • [x] Cover changes with tests
  • [x] Ensure the test suite passes (yarn test)
  • [x] Provide 100% tests coverage
  • [x] Make sure code lints (yarn lint). Fix it with yarn lint:fix in case of failure.
  • [x] Make sure types are fine (yarn lint:types).

molvqingtai avatar Nov 10 '23 06:11 molvqingtai

can we merge this? @molvqingtai

diegoarcega avatar Aug 01 '25 13:08 diegoarcega