mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-05 21:32:02 -06:00
Co-authored-by: Matti Nannt <mail@matthiasnannt.com> Co-authored-by: Shubham Palriwala <spalriwalau@gmail.com>
27 lines
793 B
TypeScript
27 lines
793 B
TypeScript
import { RefObject, useEffect } from "react";
|
|
|
|
// Custom hook to synchronize the horizontal scroll position of two elements.
|
|
export const useSyncScroll = (
|
|
highlightContainerRef: RefObject<HTMLElement>,
|
|
inputRef: RefObject<HTMLElement>
|
|
): void => {
|
|
useEffect(() => {
|
|
const syncScrollPosition = () => {
|
|
if (highlightContainerRef.current && inputRef.current) {
|
|
highlightContainerRef.current.scrollLeft = inputRef.current.scrollLeft;
|
|
}
|
|
};
|
|
|
|
const sourceElement = inputRef.current;
|
|
if (sourceElement) {
|
|
sourceElement.addEventListener("scroll", syncScrollPosition);
|
|
}
|
|
|
|
return () => {
|
|
if (sourceElement) {
|
|
sourceElement.removeEventListener("scroll", syncScrollPosition);
|
|
}
|
|
};
|
|
}, [inputRef, highlightContainerRef]);
|
|
};
|