Files
pepperplus-ui/src/components/ScrollIntoView.tsx
2026-01-28 10:34:36 +00:00

26 lines
1012 B
TypeScript

// This program has been developed by students from the bachelor Computer Science at Utrecht
// University within the Software Project course.
// © Copyright Utrecht University (Department of Information and Computing Sciences)
import {useEffect, useRef} from "react";
/**
* A React component that automatically scrolls itself into view whenever rendered.
*
* This component is especially useful in scrollable containers to keep the most
* recent content visible (e.g., chat applications, live logs, or notifications).
*
* It uses the browser's `Element.scrollIntoView()` API with smooth scrolling behavior.
*
* @returns A `<div>` element that scrolls into view when mounted or updated.
*/
export default function ScrollIntoView() {
/** Ref to the DOM element that will be scrolled into view. */
const elementRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (elementRef.current) elementRef.current.scrollIntoView({ behavior: "smooth" });
});
return <div ref={elementRef} />;
}