react-scroll icon indicating copy to clipboard operation
react-scroll copied to clipboard

React-scroll Link not working if Element has parents with position:relative

Open danielgent opened this issue 9 years ago • 18 comments

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.

danielgent avatar Nov 09 '17 17:11 danielgent

Pinning to 1.5.5 fixes it for us

danielgent avatar Nov 09 '17 17:11 danielgent

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

fisshy avatar Nov 09 '17 19:11 fisshy

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

mikhail-hatsilau avatar Nov 10 '17 08:11 mikhail-hatsilau

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.

soosap avatar Nov 14 '17 21:11 soosap

All right, thanks for the feedback. I will look into it, also feel free to make a PR :)

fisshy avatar Nov 15 '17 06:11 fisshy

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.

nuclearspike avatar Nov 19 '17 05:11 nuclearspike

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?

fisshy avatar Nov 23 '17 07:11 fisshy

Should be fixed in v1.6.5.

fisshy avatar Nov 23 '17 09:11 fisshy

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>

serieys avatar Nov 23 '17 13:11 serieys

Whats the problem? @serieys

fisshy avatar Nov 23 '17 13:11 fisshy

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

serieys avatar Nov 23 '17 13:11 serieys

All right, I managed to reproduce it. Looking into it.

fisshy avatar Nov 23 '17 13:11 fisshy

Thanks :D

serieys avatar Nov 23 '17 13:11 serieys

Check out v1.6.6

https://github.com/fisshy/react-scroll/commit/4139f1e9efdc30e16efceadc201f3875ac50996c

fisshy avatar Nov 23 '17 20:11 fisshy

It's working, great job ! Thanks a lot!

serieys avatar Nov 24 '17 09:11 serieys

@danielgent Does tha latest version solve your issue?

fisshy avatar Nov 24 '17 10:11 fisshy

@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 avatar Nov 24 '17 11:11 danielgent

@danielgent great! I'm glad its solved.

fisshy avatar Nov 24 '17 12:11 fisshy