shadcn-ui / ui

Beautifully designed components that you can copy and paste into your apps. Accessible. Customizable. Open Source.
https://ui.shadcn.com
MIT License
74.54k stars 4.62k forks source link

When `ScrollArea` appears, how do I make it automatically scroll to the bottom? #1081

Closed evanlong0803 closed 4 months ago

oyal commented 1 year ago

Monitoring data changes. Let the last element in the ScrollArea : scrollIntoView

alexlai18 commented 1 year ago

Monitoring data changes. Let the last element in the ScrollArea : scrollIntoView

For me, inside the CardContent component, the scrollarea position will change. Do you know how to keep the position within the parent div boxed in?

aadityam16 commented 7 months ago

Facing the same issue, did you find any solution till now?

RickyRAV commented 7 months ago

wondering the same here

Zombrooc commented 5 months ago

I don't know if my solution will work for this case. But i need to automatically scroll to the bottom when a new dynamic form is created.

So i Try this:

create a useRef var:

const cardRef = useRef(null);

Add this ref only in the last item

{fields.map((child, index, row) => (
  <Card
    key={child.id}
    className="mb-4 relative"
    ref={index + 1 === row.length ? cardRef : null} //Verify if the card is the last one.
  >
    {/* My code */}
  </Card>
))}

finally, i use useEffect to watch data changes on my array. If the array changes the useEffect will see and execute the scroll

useEffect(() => {
    if (fields.length > 0) {
      cardRef.current.scrollIntoView({ behavior: "smooth" }); //Use scrollIntoView to automatically scroll to my ref
    }
  }, [fields.length]);
shadcn commented 4 months ago

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

imtns commented 2 weeks ago

Anyone know how to scroll to bottom in scrollarea ?