Proposal ScrollArea | Make it possible to control Scroll Area programmatically #990
-
It should be possible to control Scroll Area programmatically, I think it should also support custom easing functions, or perhaps it should be able to integrate with other animations libraries. Here is an example how it could look like: const Component = () => {
const scrollArea = useScrollArea();
// Ref used some where in a component inside ScrollArea component
const ref = useRef();
const onClickHandler = () => {
scrollArea.scrollToElement(ref)
// or
scrollArea.scrollToTop();
// or
scrollArea.scrollToBottom();
}
return (
<>
<button onClikc={onClickHandler}>
Scroll To
</button>
<ScrollArea.Root>
...
</ScrollArea.Root>
</>
);
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Hi @joseDaKing, As I explained in your other discussion post (#989 (comment)) you have full access to the underlying scrolling div, so you can totally control it programatically. Here's an example: |
Beta Was this translation helpful? Give feedback.
-
If anyone stumbles upon this, this is how to do it:
If you are using shadcn, extend the props so you can pass the ref to the
|
Beta Was this translation helpful? Give feedback.
-
If you tried to do this and your scrollarea won't go all the way to the bottom, it's because you have to let the component load synchronously first, otherwise the calculated height for the div won't be the updated one. In order to do so you just have to make the scrolling event async with this little trick:
As you can see you can even put 0ms for the timeout, what matters is that by making the event asynchronous you let the call stack execute first (which renders your new component) and then you get the correct value for the scrollHeight. |
Beta Was this translation helpful? Give feedback.
Hi @joseDaKing,
As I explained in your other discussion post (#989 (comment)) you have full access to the underlying scrolling div, so you can totally control it programatically.
Here's an example:
https://codesandbox.io/s/scrollarea-programatic-q66k7