Skip to content

Instantly share code, notes, and snippets.

@tandat2209
Created June 9, 2020 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tandat2209/3ff49e863792a4904c3018a8bc66c81e to your computer and use it in GitHub Desktop.
Save tandat2209/3ff49e863792a4904c3018a8bc66c81e to your computer and use it in GitHub Desktop.
OverflowTooltip
import React from 'react';
import ReactResizeDetector from 'react-resize-detector';
import PropTypes from 'prop-types';
import ReactTooltip from 'react-tooltip';
export default class OverflowTooltip extends React.Component {
constructor(props) {
super(props);
this.node = null;
this.onResize = this.onResize.bind(this);
}
onResize() {
if (this.node.offsetWidth < this.node.scrollWidth) {
this.node.setAttribute('data-tip', this.props.children);
} else if (this.node.hasAttribute('data-tip')) {
this.node.removeAttribute('data-tip');
}
ReactTooltip.rebuild();
}
render() {
const { children, element, ...others } = this.props;
const Container = element || 'div';
return (
<Container
{...others}
ref={node => {
this.node = node;
}}
>
{children}
<ReactResizeDetector
handleWidth
handleHeight
onResize={this.onResize}
/>
</Container>
);
}
}
OverflowTooltip.propTypes = {
children: PropTypes.string.isRequired,
element: PropTypes.string
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment