I have a popup list which is a div
that contains a vertical list of child div
s. I have added up/down keyboard navigation to change which child is currently highlighted.
Right now, if I press the down key enough times, the highlighted item is no longer visible. The same thing also occurs with the up key if the view is scrolled.
What is the right way in React to automatically scroll a child div
into view?
To build on @Michelle Tilley's answer, I sometimes want to scroll if the user's selection changes, so I trigger the scroll on
componentDidUpdate
. I also did some math to figure out how far to scroll and whether scrolling was needed, which for me looks like the following:Another example which uses function in ref rather than string
Demo: https://codepen.io/anon/pen/XpqJVe
I assume that you have some sort of
List
component and some sort ofItem
component. The way I did it in one project was to let the item know if it was active or not; the item would ask the list to scroll it into view if necessary. Consider the following pseudocode:A better solution is probably to make the list responsible for scrolling the item into view (without the item being aware that it's even in a list). To do so, you could add a
ref
attribute to a certain item and find it with that:If you don't want to do the math to determine if the item is visible inside the list node, you could use the DOM method
scrollIntoView()
or the Webkit-specificscrollIntoViewIfNeeded
, which has a polyfill available so you can use it in non-Webkit browsers.I'm just adding another bit of info for others searching for a Scroll-To capability in React. I had tied several libraries for doing Scroll-To for my app, and none worked from my use case until I found react-scrollchor, so I thought I'd pass it on. https://github.com/bySabi/react-scrollchor
In you keyup/down handler you just need to set the
scrollTop
property of the div you want to scroll to make it scroll down (or up).For example:
JSX:
<div ref="foo">{content}</div>
keyup/down handler:
this.refs.foo.getDOMNode().scrollTop += 10
If you do something similar to above, your div will scroll down 10 pixels (assuming the div is set to overflow
auto
orscroll
in css, and your content is overflowing of course).You will need to expand on this to find the offset of the element inside your scrolling div that you want to scroll the div down to, and then modify the
scrollTop
to scroll far enough to show the element based on it's height.Have a look at MDN's definitions of scrollTop, and offsetTop here:
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetTop
For React 16, the correct answer is different from earlier answers:
Then to scroll, just add (after constructor):
Note: You must use '.current,' and you can send options to scrollIntoView:
(Found at http://www.albertgao.xyz/2018/06/07/scroll-a-not-in-view-component-into-the-view-using-react/)
Reading the spec, it was a little hard to suss out the meaning of block and inline, but after playing with it, I found that for a vertical scrolling list, block: 'end' made sure the element was visible without artificially scrolling the top of my content off the viewport. With 'center', an element near the bottom would be slid up too far and empty space appeared below it. But my container is a flex parent with justify: 'stretch' so that may affect the behavior. I didn't dig too much further. Elements with overflow hidden will impact how the scrollIntoView acts, so you'll probably have to experiment on your own.
My application has a parent that must be in view and if a child is selected, it then also scrolls into view. This worked well since parent DidMount happens before child's DidMount, so it scrolls to the parent, then when the active child is rendered, scrolls further to bring that one in view.