If I have a lengthy list of items, only one of which is marked as active
. Imagine being able to switch the active selection using keyboard inputs (press arrow down and the item below becomes active). Let's say the list of items is taller than the viewport and requires scrolling.
Is it possible to ensure that the active
item always remains visible using CSS alone?
While the JavaScript code provided below can achieve this, I am seeking an alternative method without having to call scrollIntoViewIfNeeded()
explicitly. Is there a way to instruct CSS to handle this task instead?
const prevNode = getNode(/* implementation details */)
const nextNode = getNode(/* implementation details */)
prevNode.classList.remove('active')
nextNode.classList.add('active')
nextNode.scrollIntoViewIfNeeded()
This peculiar requirement arises from my usage of React. It feels out of place to programmatically invoke scrollIntoViewIfNeeded()
when the classes are actually assigned in a declarative manner (through redux state changes).
I would greatly appreciate any suggestions. In case a straightforward CSS solution isn't feasible, I am open to hearing recommendations on how to efficiently implement this functionality in React for a lengthy list.
Thank you!