Skip to content

Commit db471ef

Browse files
authored
Merge pull request #135 from wwayne/transform-3d
Transform 3d
2 parents bea6765 + d865519 commit db471ef

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ Check the example [React-tooltip Test](http://wwayne.com/react-tooltip)
7979
I suggest always put `<ReactTooltip />` in the Highest level or smart component of Redux, so you might need these static
8080
method to control tooltip's behaviour in some situations
8181

82+
## Trouble Shooting
83+
#### Using tooltip within the modal (e.g. [react-modal](https://github.com/reactjs/react-modal))
84+
The component was designed to set a `<Reactooltip />` one place then use tooltip everywhere, but a lot of people stuck in using this component with modal, you can check the discussion [here](https://github.com/wwayne/react-tooltip/issues/130), the summarization of solving the problem is as following:
85+
86+
1. Put `<Reactootlip />` out of the `<Modal>`
87+
2. Use `React.rebuild()` when opening the modal
88+
3. If your modal's z-index happens to higher than the tooltip, use the attribute `class` to custom your tooltip's z-index
89+
8290
## Article
8391
[How I insert sass into react component](https://medium.com/@wwayne_me/how-i-insert-sass-into-my-npm-react-component-b46b9811c226#.gi4hxu44a)
8492

example/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const Test = React.createClass({
9191
<p className="sub-title">Use everything as tooltip</p>
9292

9393
<div className="example-jsx">
94-
<div className="side">
94+
<div className="side" style={{ transform: 'translate3d(5px, 5px, 5px)' }}>
9595
<a data-tip data-for='happyFace'> d(`・∀・)b </a>
9696
<ReactTooltip id='happyFace' type="error"><span>Show happy face</span></ReactTooltip>
9797
</div>

src/utils/getPosition.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export default function (e, target, node, place, effect, offset) {
2424
const windowWidth = window.innerWidth
2525
const windowHeight = window.innerHeight
2626

27+
const {parentTop, parentLeft} = getParent(target)
28+
2729
// Get the edge offset of the tooltip
2830
const getTipOffsetLeft = (place) => {
2931
const offset_X = defaultOffset[place].l
@@ -153,8 +155,8 @@ export default function (e, target, node, place, effect, offset) {
153155
return {
154156
isNewState: false,
155157
position: {
156-
left: getTipOffsetLeft(place),
157-
top: getTipOffsetTop(place)
158+
left: getTipOffsetLeft(place) - parentLeft,
159+
top: getTipOffsetTop(place) - parentTop
158160
}
159161
}
160162
}
@@ -186,8 +188,9 @@ const getDefaultPosition = (effect, targetWidth, targetHeight, tipWidth, tipHeig
186188
let right
187189
let bottom
188190
let left
189-
const disToMouse = 8
191+
const disToMouse = 3
190192
const triangleHeight = 2
193+
const cursorHeight = 12 // Optimize for float bottom only, cause the cursor will hide the tooltip
191194

192195
if (effect === 'float') {
193196
top = {
@@ -199,8 +202,8 @@ const getDefaultPosition = (effect, targetWidth, targetHeight, tipWidth, tipHeig
199202
bottom = {
200203
l: -(tipWidth / 2),
201204
r: tipWidth / 2,
202-
t: disToMouse,
203-
b: tipHeight + disToMouse + triangleHeight
205+
t: disToMouse + cursorHeight,
206+
b: tipHeight + disToMouse + triangleHeight + cursorHeight
204207
}
205208
left = {
206209
l: -(tipWidth + disToMouse + triangleHeight),
@@ -266,3 +269,17 @@ const calculateOffset = (offset) => {
266269

267270
return {extraOffset_X, extraOffset_Y}
268271
}
272+
273+
// Get the offset of the parent elements
274+
const getParent = (currentTarget) => {
275+
let currentParent = currentTarget.parentElement
276+
while (currentParent) {
277+
if (currentParent.style.transform.length > 0) break
278+
currentParent = currentParent.parentElement
279+
}
280+
281+
const parentTop = currentParent && currentParent.getBoundingClientRect().top || 0
282+
const parentLeft = currentParent && currentParent.getBoundingClientRect().left || 0
283+
284+
return {parentTop, parentLeft}
285+
}

0 commit comments

Comments
 (0)