@@ -14,7 +14,7 @@ export interface UseTextareaAutosizeOptions {
1414 /** Function called when the textarea size changes. */
1515 onResize ?: ( ) => void
1616 /** Specify style target to apply the height based on textarea content. If not provided it will use textarea it self. */
17- styleTarget ?: MaybeRef < HTMLElement >
17+ styleTarget ?: MaybeRef < HTMLElement | undefined >
1818 /** Specify the style property that will be used to manipulate height. Can be `height | minHeight`. Default value is `height`. */
1919 styleProp ?: 'height' | 'minHeight'
2020}
@@ -24,6 +24,7 @@ export function useTextareaAutosize(options?: UseTextareaAutosizeOptions) {
2424 const input = ref < string > ( options ?. input as any )
2525 const styleProp = options ?. styleProp ?? 'height'
2626 const textareaScrollHeight = ref ( 1 )
27+ const textareaOldWidth = ref ( 0 )
2728
2829 function triggerResize ( ) {
2930 if ( ! textarea . value )
@@ -33,10 +34,10 @@ export function useTextareaAutosize(options?: UseTextareaAutosizeOptions) {
3334
3435 textarea . value . style [ styleProp ] = '1px'
3536 textareaScrollHeight . value = textarea . value ?. scrollHeight
36-
37+ const _styleTarget = toValue ( options ?. styleTarget )
3738 // If style target is provided update its height
38- if ( options ?. styleTarget )
39- toValue ( options . styleTarget ) . style [ styleProp ] = `${ textareaScrollHeight . value } px`
39+ if ( _styleTarget )
40+ _styleTarget . style [ styleProp ] = `${ textareaScrollHeight . value } px`
4041 // else update textarea's height by updating height variable
4142 else
4243 height = `${ textareaScrollHeight . value } px`
@@ -48,7 +49,12 @@ export function useTextareaAutosize(options?: UseTextareaAutosizeOptions) {
4849
4950 watch ( textareaScrollHeight , ( ) => options ?. onResize ?.( ) )
5051
51- useResizeObserver ( textarea , ( ) => triggerResize ( ) )
52+ useResizeObserver ( textarea , ( [ { contentRect } ] ) => {
53+ if ( textareaOldWidth . value === contentRect . width )
54+ return
55+ textareaOldWidth . value = contentRect . width
56+ triggerResize ( )
57+ } )
5258
5359 if ( options ?. watch )
5460 watch ( options . watch , triggerResize , { immediate : true , deep : true } )
0 commit comments