@@ -7,6 +7,7 @@ import React, {
77 TextareaHTMLAttributes ,
88 useCallback ,
99 useState ,
10+ useRef ,
1011} from "react" ;
1112import cn from "classnames" ;
1213import { bem , useEnsuredRef , useResizeObserver } from "@react-md/utils" ;
@@ -168,9 +169,10 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
168169 setHeight ( undefined ) ;
169170 }
170171
171- const [ mask , setMask ] = useState < HTMLTextAreaElement | null > ( null ) ;
172+ const maskRef = useRef < HTMLTextAreaElement | null > ( null ) ;
172173 const [ scrollable , setScrollable ] = useState ( false ) ;
173- const updateHeight = ( ) : void => {
174+ const updateHeight = useCallback ( ( ) => {
175+ const mask = maskRef . current ;
174176 if ( ! mask ) {
175177 return ;
176178 }
@@ -195,19 +197,20 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
195197 if ( height !== nextHeight ) {
196198 setHeight ( nextHeight ) ;
197199 }
198- } ;
200+ } , [ height , maxRows , scrollable ] ) ;
199201
200202 // the window can be resized while there is text inside the textarea so need to
201203 // recalculate the height when the width changes as well.
202- useResizeObserver ( {
204+ const [ , maskRefHandler ] = useResizeObserver ( updateHeight , {
205+ ref : maskRef ,
203206 disableHeight : true ,
204- target : mask ,
205- onResize : updateHeight ,
206207 } ) ;
208+
207209 const [ valued , onChange ] = useValuedState < HTMLTextAreaElement > ( {
208210 value,
209211 defaultValue,
210212 onChange : ( event ) => {
213+ const mask = maskRef . current ;
211214 if ( propOnChange ) {
212215 propOnChange ( event ) ;
213216 }
@@ -286,7 +289,7 @@ const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
286289 aria-hidden
287290 defaultValue = { value || defaultValue }
288291 id = { `${ id } -mask` }
289- ref = { setMask }
292+ ref = { maskRefHandler }
290293 readOnly
291294 rows = { rows }
292295 tabIndex = { - 1 }
0 commit comments