File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -39,14 +39,23 @@ function copyStyles(inputNode: HTMLInputElement, hintNode: HTMLInputElement) {
3939
4040export const useHint = ( ) => {
4141 const { hintText, inputNode } = useTypeaheadContext ( ) ;
42-
42+
4343 const hintRef = useRef < HTMLInputElement | null > ( null ) ;
4444
45- // scroll hint input when the text input is scrolling
46- inputNode ?. addEventListener ( "scroll" , ( ) => {
47- hintRef . current ! . scrollLeft = inputNode ?. scrollLeft
48- } )
49-
45+ useEffect ( ( ) => {
46+ // Scroll hint input when the text input is scrolling.
47+ const handleInputScroll = ( ) => {
48+ if ( hintRef . current && inputNode ) {
49+ hintRef . current . scrollLeft = inputNode . scrollLeft ;
50+ }
51+ } ;
52+
53+ inputNode ?. addEventListener ( 'scroll' , handleInputScroll ) ;
54+ return ( ) => {
55+ inputNode ?. removeEventListener ( 'scroll' , handleInputScroll ) ;
56+ } ;
57+ } , [ inputNode ] ) ;
58+
5059 useEffect ( ( ) => {
5160 if ( inputNode && hintRef . current ) {
5261 copyStyles ( inputNode , hintRef . current ) ;
You can’t perform that action at this time.
0 commit comments