[Improvement] optimize passive events (#478)
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
import { isServer } from './index';
|
||||
import { on, off } from './event';
|
||||
|
||||
const context = '@@clickoutsideContext';
|
||||
|
||||
@@ -24,7 +24,7 @@ export default {
|
||||
arg: binding.arg || 'click'
|
||||
};
|
||||
|
||||
!isServer && document.addEventListener(el[context].arg, handler);
|
||||
on(document, el[context].arg, handler);
|
||||
},
|
||||
|
||||
update(el, binding) {
|
||||
@@ -32,7 +32,7 @@ export default {
|
||||
},
|
||||
|
||||
unbind(el) {
|
||||
!isServer && document.removeEventListener(el[context].arg, el[context].handler);
|
||||
off(document, el[context].arg, el[context].handler);
|
||||
},
|
||||
|
||||
install(Vue) {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { isServer } from './';
|
||||
|
||||
export let supportsPassive = false;
|
||||
if (!isServer) {
|
||||
try {
|
||||
const opts = {};
|
||||
Object.defineProperty(opts, 'passive', {
|
||||
get() {
|
||||
/* istanbul ignore next */
|
||||
supportsPassive = true;
|
||||
}
|
||||
});
|
||||
window.addEventListener('test-passive', null, opts);
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
export function on(target, event, handler, passive = false) {
|
||||
!isServer &&
|
||||
target.addEventListener(
|
||||
event,
|
||||
handler,
|
||||
supportsPassive ? { capture: false, passive } : false
|
||||
);
|
||||
}
|
||||
|
||||
export function off(target, event, handler) {
|
||||
!isServer && target.removeEventListener(event, handler);
|
||||
}
|
||||
Reference in New Issue
Block a user