Skip to content

Commit

Permalink
make ripple touch events passive
Browse files Browse the repository at this point in the history
  • Loading branch information
xomaczar committed Jul 24, 2018
1 parent 7f77a7d commit 524b6fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 12 additions & 4 deletions addon/mixins/ripple-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import { computed } from '@ember/object';
import Mixin from '@ember/object/mixin';
import { run } from '@ember/runloop';
import $ from 'jquery';
import { supportsPassiveEventListeners } from '../utils/browser-features';

/* global window */

const DURATION = 400;


/**
* @class RippleMixin
* @extends Ember.Mixin
Expand Down Expand Up @@ -129,11 +132,16 @@ export default Mixin.create({

},
bindEvents() {
this.rippleElement.on('mousedown', run.bind(this, this.handleMousedown));
this.rippleElement.on('mouseup touchend', run.bind(this, this.handleMouseup));
this.rippleElement.on('mouseleave', run.bind(this, this.handleMouseup));
this.rippleElement.on('touchmove', run.bind(this, this.handleTouchmove));
let re = this.rippleElement.get(0);
re.addEventListener('mousedown', run.bind(this, this.handleMousedown));
re.addEventListener('mouseup', run.bind(this, this.handleMouseup));
re.addEventListener('mouseleave', run.bind(this, this.handleMouseup));

let options = supportsPassiveEventListeners ? { passive: true } : {};
re.addEventListener('touchend', run.bind(this, this.handleMouseup), options);
re.addEventListener('touchmove', run.bind(this, this.handleTouchmove), options);
},

handleMousedown(event) {
if (this.mousedown) {
return;
Expand Down
14 changes: 14 additions & 0 deletions addon/utils/browser-features.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Taken from https://github.com/Modernizr/Modernizr/blob/master/feature-detects/dom/passiveeventlisteners.js
export const supportsPassiveEventListeners = (function() {
let supportsPassiveOption = false;
try {
const opts = Object.defineProperty({}, 'passive', {
get: function() {
supportsPassiveOption = true;
}
});
window.addEventListener('test', null, opts);
// eslint-disable-next-line
} catch (e) {};
return supportsPassiveOption;
})();

0 comments on commit 524b6fb

Please sign in to comment.