lbry-desktop/src/renderer/component/common/tooltip.jsx

72 lines
1.7 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import * as React from 'react';
2018-03-26 23:32:43 +02:00
import classnames from 'classnames';
type Props = {
body: string,
label?: string,
2018-06-20 05:55:25 +02:00
children?: React.Node,
icon?: boolean,
direction: string,
2018-06-20 05:55:25 +02:00
onComponent?: boolean, // extra padding to account for button/form field size
alwaysVisible?: boolean, // should tooltip stay open, guide callbacks will close it manually
2018-03-26 23:32:43 +02:00
};
2018-10-10 19:43:15 +02:00
type State = {
direction: string,
};
class ToolTip extends React.PureComponent<Props, State> {
static defaultProps = {
direction: 'bottom',
alwaysVisible: false,
};
2018-03-26 23:32:43 +02:00
constructor(props: Props) {
2018-09-24 00:01:12 +02:00
super(props);
this.state = {
direction: this.props.direction,
};
}
2018-11-08 20:48:38 +01:00
tooltip: ?HTMLSpanElement;
2018-03-26 23:32:43 +02:00
render() {
2018-09-24 00:01:12 +02:00
const { direction } = this.state;
const { children, label, body, icon, onComponent, alwaysVisible } = this.props;
const tooltipContent = children || label;
2018-06-20 05:55:25 +02:00
const bodyLength = body.length;
const isShortDescription = bodyLength < 30;
2018-03-26 23:32:43 +02:00
return (
<span
className={classnames('tooltip', {
'tooltip--label': label && !icon,
'tooltip--icon': icon,
'tooltip--top': direction === 'top',
'tooltip--right': direction === 'right',
'tooltip--bottom': direction === 'bottom',
'tooltip--left': direction === 'left',
2018-06-20 05:55:25 +02:00
'tooltip--on-component': onComponent,
'tooltip--always-visible': alwaysVisible,
})}
>
{tooltipContent}
2018-06-20 05:55:25 +02:00
<span
2018-11-08 20:48:38 +01:00
ref={ref => {
this.tooltip = ref;
}}
className={classnames('card tooltip__body', {
2018-06-20 05:55:25 +02:00
'tooltip__body--short': isShortDescription,
})}
>
{body}
</span>
2018-03-26 23:32:43 +02:00
</span>
);
}
}
export default ToolTip;