Skip to content

Commit

Permalink
optional listen to window resize event
Browse files Browse the repository at this point in the history
  • Loading branch information
eluxen committed Nov 26, 2019
1 parent b423b36 commit 3559dbd
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ export default class Truncate extends Component {
]),
trimWhitespace: PropTypes.bool,
width: PropTypes.number,
onTruncate: PropTypes.func
onTruncate: PropTypes.func,
listenToResize: PropTypes.bool
};

static defaultProps = {
children: '',
ellipsis: '…',
lines: 1,
trimWhitespace: false,
width: 0
width: 0,
listenToResize: true
};

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

window.addEventListener('resize', onResize);
if (this.props.listenToResize) {
window.addEventListener('resize', onResize);
}
}

componentDidUpdate(prevProps) {
Expand Down Expand Up @@ -84,7 +88,9 @@ export default class Truncate extends Component {
ellipsis.parentNode.removeChild(ellipsis);
}

window.removeEventListener('resize', onResize);
if (this.props.listenToResize) {
window.removeEventListener('resize', onResize);
}

window.cancelAnimationFrame(timeout);
}
Expand Down

0 comments on commit 3559dbd

Please sign in to comment.