React-scroll Link not working if Element has parents with position:relative
Please see my Gist here. (This is copied from App.js in a create-react-app app) https://gist.github.com/danielgent/449f2435509bd7fbc09474f6ee9f0408#file-failing-react-scroll-jsx-L18
It seems to work on older versions of react-scroll, so we've pinned our version to an older version
This is new to me anyway. I hadn't realised that position:relative means offsetTop is no longer from the top of the document, but presumably from the top of the stacking context.
Pinning to 1.5.5 fixes it for us
Interesting, don't know right now why it doesn't work for you.
But in this example I'm using relative
https://github.com/fisshy/react-scroll/blob/master/examples/container/app.js
The same issue but with scroller.scrollTo(element). In 1.5.5 version you used getBoundingClientReact().top to calculate top coordinate of the target element and it worked as expected, but then you switched to the offsetTop and it stopped working when parent of the target element has non static position (it calculates position relatively to this parent and scrolls to the top of the page).
https://github.com/fisshy/react-scroll/blob/e7ba7c16b4d4c5f30b87b328a5c8091209cb8a71/modules/mixins/scroller.js#L61
I can confirm this behaviour.
I have a very typical setting. My main components are wrapped with Scroll.Elements and are rendered in <Home />. <Home /> also renders <Header /> which shows a <Sidebar /> when you click on the hamburger icon. Then from within the <Sidebar /> component I want to control scrolling to menu items --- but I can't using v1.6.4 (however using v1.5.5 it is possible).
The important piece of information I guess is, as mentioned by @danielgent, that <Sidebar />'s parent i.e. the <Header /> has a position of relative.
All right, thanks for the feedback. I will look into it, also feel free to make a PR :)
I'm not sure if this was the cause, but what had worked in v1.2.0 stopped working when I upgraded so I rolled back until it's fixed.
I believe this one is solved in the latest commit https://github.com/fisshy/react-scroll/commit/974ae313d5b4eb4feac00338f6293727977ec3ef
It's not published so please try with the code
Can anyone try and get back to me?
Should be fixed in v1.6.5.
Hey, just updated, still got the problem
<div style={{ position: 'relative' }}>
<Element name={"project" + this.props.number} id={"project" + this.props.number}>
<span className={styles.projectNumber}>{formatNumberProject(this.props.number)}</span>
</Element>
</div>
Whats the problem? @serieys
when i clink on my link, the window will go to the top of the html, but if i remove the position:relative on my Element parent, the window will go to the right place of my Element when I click
All right, I managed to reproduce it. Looking into it.
Thanks :D
Check out v1.6.6
https://github.com/fisshy/react-scroll/commit/4139f1e9efdc30e16efceadc201f3875ac50996c
It's working, great job ! Thanks a lot!
@danielgent Does tha latest version solve your issue?
@fisshy Thanks so much. I've been following the discussion and checking previous versions but I can confirm the latest release 1.6.7 fixes the relative position issue (as shown in the code sample above)
@danielgent great! I'm glad its solved.