From 3559dbd36b2ffd7471b8024d6a4c72a6d3d311b5 Mon Sep 17 00:00:00 2001 From: eluxen Date: Tue, 26 Nov 2019 16:35:15 +0200 Subject: [PATCH] optional listen to window resize event --- src/Truncate.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Truncate.js b/src/Truncate.js index 943ea69b..eba8e2bd 100644 --- a/src/Truncate.js +++ b/src/Truncate.js @@ -11,7 +11,8 @@ export default class Truncate extends Component { ]), trimWhitespace: PropTypes.bool, width: PropTypes.number, - onTruncate: PropTypes.func + onTruncate: PropTypes.func, + listenToResize: PropTypes.bool }; static defaultProps = { @@ -19,7 +20,8 @@ export default class Truncate extends Component { ellipsis: '…', lines: 1, trimWhitespace: false, - width: 0 + width: 0, + listenToResize: true }; state = {}; @@ -56,7 +58,9 @@ export default class Truncate extends Component { } }); - window.addEventListener('resize', onResize); + if (this.props.listenToResize) { + window.addEventListener('resize', onResize); + } } componentDidUpdate(prevProps) { @@ -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); }