noeldelgado / react-gemini-scrollbar

:last_quarter_moon: React component for custom overlay-scrollbars with native scrolling mechanism.
MIT License
133 stars 24 forks source link

After changing the content's size dynamically, it needs a resize event to adjust the scrollbar #21

Open ziadloo opened 8 years ago

ziadloo commented 8 years ago

I have a div which I add to its children dynamically and as the result its height changes. Once the content's height is changed, the vertical scrollbar will be able to move too far to the bottom, so much that the bar will be cut off by the frame as if it has gone underneath it. But if I resize the window, the recalculation done makes everything alright again.

Is there some way I can fix this problem from my end?

noeldelgado commented 8 years ago

Hi, try calling the update method on the scrollbar instance reference after you have added any children dynamically.

I think you can accomplish this by adding a ref to the gemini component, such as:

// your-component.js

<GeminiScrollbar ref='gemini'>
...
</GeminiScrollbar>

then you can do something like this.refs.gemini.scrollbar.update();.

Let me know if that works for you.

Btw, how are you adding the children dynamically? shouldn't the componentDidUpdate method being run at that point? The scrollbar component should run the update itself when that method gets fired.

// gemini-scrollbar/index.js

componentDidUpdate() {
  this.scrollbar.update();
},
ziadloo commented 8 years ago

You are right, your code works fine if there's no animation involved. But since I add the new components with animation over a period of time and the animation affects the size of the content, that makes the problem. I mean if I remove the <ReactCSSTransitionGroup> element, the height is calculated perfectly.

Do you have any generic solution for content with animation? I mean I can come up with one for myself but I wonder if you've already solved it in a decent way!

noeldelgado commented 8 years ago

Sorry, I haven't worked with animation before (using React). I look forward to seeing what you come up with!

ziadloo commented 8 years ago

The right approach would be using ReactTransitionGroup class and implement your own version of ReactCSSTransitionGroup so you can recalculate whatever you need once the animation is done. For the time being I took the dirty approach and just added a timer to call the update method after the animation is over.