项目作者: sadorlovsky

项目描述 :
please, save me from a propagation 🙏
高级语言: TypeScript
项目地址: git://github.com/sadorlovsky/without-propagation.git
创建时间: 2019-12-13T10:38:41Z
项目社区:https://github.com/sadorlovsky/without-propagation

开源协议:MIT License

下载


without-propagation build codecov

You don’t want a propagation, right?

  1. <div>
  2. <button>Click me</button>
  3. </div>
  1. const div = document.querySelector('div')
  2. const button = document.querySelector('button')
  3. div.addEventListener('click', (e) => {
  4. console.log('Catch a click on the div')
  5. })
  6. button.addEventListener('click', (e) => {
  7. console.log('Catch a click on the button')
  8. })
  9. button.click()
  10. // > Catch a click on the button
  11. // > Catch a click on the div

Oh my… sure I don’t

  1. yarn add without-propagation

You’re fine now:

  1. import withoutPropagation from 'without-propagation'
  2. const div = document.querySelector('div')
  3. const button = document.querySelector('button')
  4. div.addEventListener('click', (e) => {
  5. console.log('The event will be never fired')
  6. })
  7. button.addEventListener('click', withoutPropagation((e) => {
  8. console.log('Catch a click on the button only')
  9. }))
  10. button.click()
  11. // > Catch a click on the button only