ToolTip 04
Finish implementing the ToolTip component
by Neeraj Yadav
HTML
<div id="app"></div>
CSS
body {
background: #20262E;
padding: 0px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 0px;
padding: 0px;
width: 100%;
height: 768px;
}
#btnLeft {
position: absolute;
left: 10px;
}
#btnRight {
position: absolute;
right: 10px;
}
#btnBtmR {
position: absolute;
right: 10px;
bottom: 10px;
}
#btnCenter {
position: absolute;
left: 150px;
top: 150px;
}
#tooltip {
position: absolute;
left: 50px;
top: 50px;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-style: normal;
font-weight: 400;
letter-spacing: normal;
line-height: 1.42857143;
text-align: left;
text-align: start;
text-shadow: none;
text-transform: none;
white-space: normal;
word-break: normal;
word-spacing: normal;
word-wrap: normal;
font-size: 12px;
display: none;
}
#tooltip.right {
margin-left: 5px;
}
#tooltip.left {
margin-left: -5px;
}
#tooltip.top {
margin-top: -5px;
}
#tooltip.bottom {
margin-top: 5px;
}
#tooltip .tooltip-arrow {
top: 50%;
left: 0;
margin-top: -5px;
border-width: 5px 5px 5px 0;
border-right-color: #000;
}
#tooltip.right .tooltip-arrow {
top: 50%;
left: auto;
margin-left: -5px;
border-width: 5px 5px 5px 0;
border-right-color: #000;
}
#tooltip.top .tooltip-arrow {
top: auto;
bottom: -5px;
left: 50%;
margin-left: -5px;
border-width: 5px 5px 0;
border-top-color: #000;
}
#tooltip.left .tooltip-arrow {
top: 50%;
margin-top: -5px;
border-width: 5px 0 5px 5px;
border-left-color: #000;
right: -5px;
left: auto;
}
#tooltip.bottom .tooltip-arrow {
top: 0;
left: 50%;
margin-left: -5px;
border-width: 0 5px 5px;
border-bottom-color: #000;
}
#tooltip .tooltip-arrow {
position: absolute;
width: 0;
height: 0;
border-color: transparent;
border-right-color: transparent;
border-style: solid;
}
#tooltip .tooltip-inner {
max-width: 200px;
padding: 3px 8px;
color: #fff;
text-align: center;
...
React
class ToolTip extends React.Component
{
constructor(props)
{
super(props);
this.state = {visible:false, x:0, y:0, type:"none"};
}
render()
{
let {state} = this;
let visibility = state.visible == true ? "on" : "off";
let style = {
left: ((state.x + window.scrollX) + 'px'),
top: ((state.y + window.scrollY) + 'px')
};
let classNames = {};
if(state.type != null && state.type != "none")
{
classNames[state.type] = true;
}
classNames[visibility] = true;
return <div id="tooltip" className={Object.keys(classNames).join(" ")} style={style}>
<div className="tooltip-arrow"></div><div className="tooltip-inner">ToolTip Component</div></div>;
}
componentDidMount()
{
}
componentWillUnmount()
{
}
pastShow(hoverRect)
{
// position the tooltip after showing it
let ttNode = ReactDOM.findDOMNode(this);
if(ttNode != null)
{
let x = 0, y = 0;
const docWidth = document.documentElement.clientWidth,
docHeight = document.documentElement.clientHeight;
let rx = hoverRect.x + hoverRect.width, // most right x
lx = hoverRect.x, // most left x
ty = hoverRect.y, // most top y
by = hoverRect.y + hoverRect.height; // most bottom y
// tool tip rectange
let ttRect = ttNode.getBoundingClientRect();
let bRight = (rx + ttRect.width) <= (window.scrollX + docWidth);
let bLeft = (lx - ttRect.width) >= 0;
let bAbove = (ty - ttRect.height) >= 0;
let bBellow = (by + ttRect.height) <= (window.scrollY + docHeight);
let newState = {};
// the tooltip doesn't fit to the right
if(bRight)
{
x = rx;
y = ty + (hoverRect.height - ttRect.height);
if(y < 0)
{
y = ty;
}
newState.type = "right";
}
else if(bBellow)
...