React Detectable Overflow
A React component which is able to detect changes in the state that the contents is overflowed.
Install
npm install react-detectable-overflow
or
yarn add react-detectable-overflow
Props
value | true | string | | |
tag | | string | element type (e.g. 'p' , 'div' ) | 'div' |
style | | object | css style of the element | { width: '100%', textOverflow: 'ellipsis', whiteSpace: 'nowrap', overflow: 'hidden', } |
className | | string | class names | '' |
onChange | | (isOverflowed: boolean) => void | callback function called when its overflowing status is changed | |
Example
import * as React from 'react';
import DetectableOverflow from 'react-detectable-overflow';
class SampleComponent extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
}
handleChange(isOverflowed) {
}
render {
return (
<DetectableOverflow
value="This is a sample text."
onChange={this.handleChange}
/>
);
}
}
Caution
Be careful when you change the length of value
by onChange callback. The following code perhaps causes the infinite loop of changing isOverflowed
state.
<DetectableOverflow
value={this.state.value}
onChange={(isOverflowed) => {
if (isOverflowed) {
this.setState({ value: 'short' });
} else {
this.setState({ value: 'loooooooooooooooooooooooooooooooooooooong' });
}
}}
/>
License
This package is released under the MIT License, see LICENSE