Skip to content

Commit

Permalink
Use effect for hint scroll listener
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgio committed Nov 28, 2024
1 parent 31b1802 commit 3ad4a74
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/components/Hint/Hint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,23 @@ function copyStyles(inputNode: HTMLInputElement, hintNode: HTMLInputElement) {

export const useHint = () => {
const { hintText, inputNode } = useTypeaheadContext();

const hintRef = useRef<HTMLInputElement | null>(null);

// scroll hint input when the text input is scrolling
inputNode?.addEventListener("scroll", () => {
hintRef.current!.scrollLeft = inputNode?.scrollLeft
})

useEffect(() => {
// Scroll hint input when the text input is scrolling.
const handleInputScroll = () => {
if (hintRef.current && inputNode) {
hintRef.current.scrollLeft = inputNode.scrollLeft;
}
};

inputNode?.addEventListener('scroll', handleInputScroll);
return () => {
inputNode?.removeEventListener('scroll', handleInputScroll);
};
}, [inputNode]);

useEffect(() => {
if (inputNode && hintRef.current) {
copyStyles(inputNode, hintRef.current);
Expand Down

0 comments on commit 3ad4a74

Please sign in to comment.