Smart tooltip #1979

Merged
btzr-io merged 4 commits from tooltip into master 2018-09-25 02:43:08 +02:00
Showing only changes of commit d6cfc8d5fc - Show all commits

View file

@ -16,8 +16,66 @@ class ToolTip extends React.PureComponent<Props> {
direction: 'bottom', direction: 'bottom',
}; };
constructor(props) {
super(props);
this.tooltip = React.createRef();
this.state = {
direction: this.props.direction,
};
}
componentDidMount() {
this.handleVisibility();
}
getVisibility = () => {
const node = this.tooltip.current;
const rect = node.getBoundingClientRect();
// Get parent-container
const viewport = document.getElementById('content');
const visibility = {
top: rect.top >= 0,
left: rect.left >= 0,
right: rect.right <= viewport.offsetWidth,
bottom: rect.bottom <= viewport.offsetHeight,
};
return visibility;
};
invertDirection = () => {
// Get current direction
const { direction } = this.state;
// Inverted directions
const directions = {
top: 'bottom',
left: 'right',
right: 'left',
bottom: 'top',
};
const inverted = directions[direction];
// Update direction
if (inverted) {
this.setState({ direction: inverted });
}
};
handleVisibility = () => {
const { direction } = this.state;
const visibility = this.getVisibility();
if (!visibility[direction]) {
this.invertDirection();
neb-b commented 2018-09-24 21:09:09 +02:00 (Migrated from github.com)
Review

Could you add a short comment above this explaining why we might need to invert? The code is very readable but it wouldn't hurt to add some additional explanation.

Could you add a short comment above this explaining why we might need to invert? The code is very readable but it wouldn't hurt to add some additional explanation.
}
};
render() { render() {
const { children, label, body, icon, direction, onComponent } = this.props; const { direction } = this.state;
const { children, label, body, icon, onComponent } = this.props;
const tooltipContent = children || label; const tooltipContent = children || label;
const bodyLength = body.length; const bodyLength = body.length;
@ -25,6 +83,8 @@ class ToolTip extends React.PureComponent<Props> {
return ( return (
<span <span
onFocus={this.handleVisibility}
onMouseOver={this.handleVisibility}
className={classnames('tooltip', { className={classnames('tooltip', {
'tooltip--label': label && !icon, 'tooltip--label': label && !icon,
'tooltip--icon': icon, 'tooltip--icon': icon,
@ -37,6 +97,7 @@ class ToolTip extends React.PureComponent<Props> {
> >
{tooltipContent} {tooltipContent}
<span <span
ref={this.tooltip}
className={classnames('tooltip__body', { className={classnames('tooltip__body', {
'tooltip__body--short': isShortDescription, 'tooltip__body--short': isShortDescription,
})} })}