Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use passive resize event when possible #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "6"
- "8"
script: "npm run travis"
env:
- CXX=g++-4.8
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"canvas": "^1.6.7",
"canvas": "^2.6.0",
"common-tags": "^1.4.0",
"coveralls": "^3.0.0",
"cross-env": "^5.2.0",
"eslint": "^4.8.0",
"eslint-config-onelint": "^2.0.0",
"eslint-config-onelint-react": "^2.0.1",
"eslint-plugin-react": "^7.4.0",
"jsdom": "^11.3.0",
"jsdom": "^15.1.1",
"mocha": "^4.0.1",
"nyc": "^11.2.1",
"prop-types": "^15.6.0",
Expand All @@ -66,5 +66,8 @@
"unexpected-dom": "^4.0.0",
"unexpected-react": "^5.0.0",
"unexpected-sinon": "^10.8.2"
},
"dependencies": {
"are-passive-events-supported": "^1.1.1"
}
}
8 changes: 6 additions & 2 deletions src/Truncate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import arePassiveEventsSupported from 'are-passive-events-supported';


export default class Truncate extends Component {
static propTypes = {
Expand Down Expand Up @@ -56,7 +58,8 @@ export default class Truncate extends Component {
}
});

window.addEventListener('resize', onResize);
this.resizeOptions = arePassiveEventsSupported() ? { passive: true } : undefined;
window.addEventListener('resize', onResize, this.resizeOptions);
}

componentDidUpdate(prevProps) {
Expand All @@ -82,7 +85,8 @@ export default class Truncate extends Component {

ellipsis.parentNode.removeChild(ellipsis);

window.removeEventListener('resize', onResize);
window.removeEventListener('resize', onResize, this.resizeOptions);
delete this.resizeOptions;

window.cancelAnimationFrame(timeout);
}
Expand Down
2 changes: 1 addition & 1 deletion test/Truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('<Truncate />', () => {
describe('in a client environment', () => {
before(() => {
const { JSDOM } = jsdom;
const { document } = (new JSDOM('')).window;
const { document } = (new JSDOM('', { url: 'http://localhost/' })).window;
global.document = document;
global.window = global.document.defaultView;
global.window.requestAnimationFrame = requestAnimationFrame;
Expand Down
Loading