|
| 1 | +require('./Tooltip.scss') |
| 2 | + |
| 3 | +import React, { Component, PropTypes } from 'react' |
| 4 | +import classNames from 'classnames' |
| 5 | +import ReactDOM from 'react-dom' |
| 6 | + |
| 7 | +class Tooltip extends Component { |
| 8 | + constructor(props) { |
| 9 | + super(props) |
| 10 | + this.state = { isActive: false } |
| 11 | + this.showTooltip = this.showTooltip.bind(this) |
| 12 | + this.hideTooltip = this.hideTooltip.bind(this) |
| 13 | + this.onClick = this.onClick.bind(this) |
| 14 | + } |
| 15 | + |
| 16 | + showTooltip(evt) { |
| 17 | + const pointerWidth = this.props.pointerWidth |
| 18 | + const tooltipMargin = this.props.tooltipMargin |
| 19 | + const pointerGap = this.props.pointerGap |
| 20 | + const tooltipPadding = this.props.tooltipPadding |
| 21 | + const tooltipDelay = this.props.tooltipDelay |
| 22 | + const tooltip = ReactDOM.findDOMNode(this) |
| 23 | + const ttContainer = tooltip.querySelector('.tooltip-container') |
| 24 | + const tooltipPointer = ttContainer.querySelector('.tooltip-pointer') |
| 25 | + const targetRect = evt.currentTarget.getBoundingClientRect() |
| 26 | + const targetRectCenterX = (targetRect.width / 2) + targetRect.left + window.scrollX |
| 27 | + const targetRectCenterY = (targetRect.height / 2) + targetRect.top + window.scrollY |
| 28 | + ttContainer.style.padding = tooltipPadding + 'px' |
| 29 | + tooltipPointer.style.width = pointerWidth + 'px' |
| 30 | + tooltipPointer.style.height = pointerWidth + 'px' |
| 31 | + const ttBorderRadius = getComputedStyle(ttContainer).getPropertyValue('border-top-left-radius').replace(/[^-\d\.]/g, '') |
| 32 | + //if right side of tooltip will fit above/below target on screen when centered to target |
| 33 | + if (targetRectCenterX + (tooltip.clientWidth / 2) + tooltipMargin < document.body.clientWidth) { |
| 34 | + //if left side of tooltip will not fit in available screen space when centered to target |
| 35 | + if (targetRectCenterX < (tooltip.clientWidth / 2) + tooltipMargin) { |
| 36 | + //push tooltip to left side of screen plus margin |
| 37 | + tooltip.style.left = tooltipMargin + 'px' |
| 38 | + //else, center tooltip to target |
| 39 | + } else { |
| 40 | + tooltip.style.left = targetRectCenterX - (tooltip.clientWidth / 2) + 'px' |
| 41 | + } |
| 42 | + //else, push tooltip to right edge of screen plus margin |
| 43 | + } else { |
| 44 | + tooltip.style.left = document.body.clientWidth - tooltip.clientWidth - tooltipMargin + 'px' |
| 45 | + } |
| 46 | + let tooltipRect = tooltip.getBoundingClientRect() |
| 47 | + let tooltipPointerLeft = 0 |
| 48 | + if (targetRectCenterX + tooltipMargin + (pointerWidth/2) >= document.body.clientWidth) { |
| 49 | + tooltipPointerLeft = tooltip.clientWidth - (pointerWidth * Math.sqrt(2)) - ttBorderRadius + 'px' |
| 50 | + } else if (targetRectCenterX < tooltipMargin + (pointerWidth/2)) { |
| 51 | + tooltipPointerLeft = ttBorderRadius + 'px' |
| 52 | + } else { |
| 53 | + tooltipPointerLeft = targetRectCenterX - tooltipRect.left - (pointerWidth/2) + 'px' |
| 54 | + } |
| 55 | + //if target is too close to top of page to fit default tooltip bubble |
| 56 | + if (targetRect.top < tooltip.clientHeight + tooltipMargin + (pointerWidth/2) + pointerGap) { |
| 57 | + //if tooltip is wider than tooltip target plus available screen space when centered above/below target, |
| 58 | + //and screen has not been scrolled down past the top of the tooltip, |
| 59 | + //and there is enough space to put tooltip to the right of target on screen |
| 60 | + if (targetRectCenterX < (tooltip.clientWidth / 2) + tooltipMargin && window.scrollY < (targetRectCenterY - (tooltip.clientHeight / 2)) && targetRect.right + tooltip.clientWidth + tooltipMargin + (pointerWidth/2) + pointerGap < document.body.clientWidth) { |
| 61 | + tooltip.style.left = targetRect.right + pointerGap + (pointerWidth/2) + 'px' |
| 62 | + tooltip.style.top = Math.max((targetRectCenterY - (tooltip.clientHeight/2)), tooltipMargin) + 'px' |
| 63 | + tooltipRect = tooltip.getBoundingClientRect() |
| 64 | + tooltipPointer.style.bottom = 'auto' |
| 65 | + tooltipPointer.style.top = Math.max((((pointerWidth * Math.sqrt(2))/2) - (pointerWidth/2)), (targetRectCenterY - tooltipRect.top - window.scrollY - (pointerWidth/2))) + 'px' |
| 66 | + tooltipPointerLeft = - (pointerWidth/2) + 'px' |
| 67 | + } else { |
| 68 | + tooltip.style.top = targetRect.bottom + pointerGap + (pointerWidth/2) + window.scrollY + 'px' |
| 69 | + tooltipPointer.style.top = 'auto' |
| 70 | + tooltipPointer.style.bottom = tooltip.clientHeight - (pointerWidth/2) + 'px' |
| 71 | + } |
| 72 | + } else { |
| 73 | + tooltip.style.top = targetRect.top - tooltip.clientHeight + window.scrollY - pointerGap - (pointerWidth/2) + 'px' |
| 74 | + tooltipPointer.style.bottom = - (pointerWidth/2) + 'px' |
| 75 | + tooltipPointer.style.top = 'auto' |
| 76 | + } |
| 77 | + tooltipPointer.style.left = tooltipPointerLeft |
| 78 | + |
| 79 | + if (tooltip.classList.contains('tooltip-hide')) { |
| 80 | + tooltip.classList.remove('tooltip-hide') |
| 81 | + tooltip.style.transition = 'opacity ' + tooltipDelay + 'ms linear' |
| 82 | + tooltip.style.opacity = '1' |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + hideTooltip() { |
| 87 | + const tooltip = ReactDOM.findDOMNode(this) |
| 88 | + if (!tooltip.classList.contains('tooltip-hide')) { |
| 89 | + tooltip.classList.add('tooltip-hide') |
| 90 | + tooltip.style.transition = 'opacity 0s linear' |
| 91 | + tooltip.style.opacity = '0' |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + onClick(evt) { |
| 96 | + if (this.state.isActive) { |
| 97 | + this.hideTooltip() |
| 98 | + } else { |
| 99 | + this.showTooltip(evt) |
| 100 | + } |
| 101 | + |
| 102 | + this.setState({ isActive: !this.state.isActive }) |
| 103 | + } |
| 104 | + |
| 105 | + componentDidMount() { |
| 106 | + const targetId = this.props.tooltipId |
| 107 | + const target = document.getElementById(targetId) |
| 108 | + if (this.props.popMethod === 'hover') { |
| 109 | + target.addEventListener('mouseenter', this.showTooltip) |
| 110 | + target.addEventListener('mouseleave', this.hideTooltip) |
| 111 | + } else if (this.props.popMethod === 'click') { |
| 112 | + target.classList.add('click-pointer') |
| 113 | + target.addEventListener('click', this.onClick) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + componentWillUnmount() { |
| 118 | + const targetId = this.props.tooltipId |
| 119 | + const target = document.getElementById(targetId) |
| 120 | + target.removeEventListener('mouseenter', this.showTooltip) |
| 121 | + target.removeEventListener('mouseleave', this.hideTooltip) |
| 122 | + |
| 123 | + target.removeEventListener('click', this.onClick) |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + render() { |
| 128 | + const body = ( |
| 129 | + <div className="tooltip-body"> |
| 130 | + {React.Children.map(this.props.children, (child) => { |
| 131 | + return child.props.children |
| 132 | + })} |
| 133 | + </div> |
| 134 | + ) |
| 135 | + const ttClasses = classNames( |
| 136 | + 'Tooltip', 'tooltip-hide', this.props.theme, this.props.className |
| 137 | + ) |
| 138 | + return ( |
| 139 | + <div className={ttClasses}> |
| 140 | + <div className="tooltip-container"> |
| 141 | + <div className="tooltip-pointer"> |
| 142 | + </div> |
| 143 | + {body} |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + ) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +Tooltip.propTypes = { |
| 151 | + children : PropTypes.object.isRequired, |
| 152 | + tooltipId : PropTypes.string.isRequired, |
| 153 | + pointerWidth : PropTypes.number, |
| 154 | + tooltipMargin : PropTypes.number, |
| 155 | + pointerGap : PropTypes.number, |
| 156 | + tooltipPadding : PropTypes.number, |
| 157 | + tooltipDelay : PropTypes.number, |
| 158 | + theme : PropTypes.string, |
| 159 | + popMethod : PropTypes.string |
| 160 | +} |
| 161 | + |
| 162 | +Tooltip.defaultProps = { |
| 163 | + pointerWidth : 10, |
| 164 | + tooltipMargin : 10, |
| 165 | + pointerGap : 5, |
| 166 | + tooltipPadding : 15, |
| 167 | + tooltipDelay : 0, |
| 168 | + theme : 'default', |
| 169 | + popMethod : 'hover' |
| 170 | +} |
| 171 | + |
| 172 | +export default Tooltip |
0 commit comments