Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e1a7ef3

Browse filesBrowse files
ilyaliaoOrbisKautofix-ci[bot]antfu
authored
fix(useTextareaAutosize): improve resize handling with requestAnimationFrame (#4557)
Co-authored-by: Robin <37191683+OrbisK@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 5dd2a1e commit e1a7ef3
Copy full SHA for e1a7ef3

File tree

1 file changed

+29
-5
lines changed
Filter options
  • packages/core/useTextareaAutosize

1 file changed

+29
-5
lines changed

‎packages/core/useTextareaAutosize/index.ts

Copy file name to clipboardExpand all lines: packages/core/useTextareaAutosize/index.ts
+29-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import type { MaybeRef } from '@vueuse/shared'
1+
import type { Fn, MaybeRef } from '@vueuse/shared'
22
import type { WatchSource } from 'vue'
3+
import type { ConfigurableWindow } from '../_configurable'
34
import { toRef } from '@vueuse/shared'
45
import { nextTick, shallowRef, toValue, watch } from 'vue'
6+
import { defaultWindow } from '../_configurable'
57
import { useResizeObserver } from '../useResizeObserver'
68

7-
export interface UseTextareaAutosizeOptions {
9+
export interface UseTextareaAutosizeOptions extends ConfigurableWindow {
810
/** Textarea element to autosize. */
911
element?: MaybeRef<HTMLTextAreaElement | undefined>
1012
/** Textarea content. */
@@ -19,7 +21,26 @@ export interface UseTextareaAutosizeOptions {
1921
styleProp?: 'height' | 'minHeight'
2022
}
2123

22-
export function useTextareaAutosize(options?: UseTextareaAutosizeOptions) {
24+
/**
25+
* Call window.requestAnimationFrame(), if not available, just call the function
26+
*
27+
* @param window
28+
* @param fn
29+
*/
30+
function tryRequestAnimationFrame(
31+
window: Window | undefined = defaultWindow,
32+
fn: Fn,
33+
) {
34+
if (window && typeof window.requestAnimationFrame === 'function') {
35+
window.requestAnimationFrame(fn)
36+
}
37+
else {
38+
fn()
39+
}
40+
}
41+
42+
export function useTextareaAutosize(options: UseTextareaAutosizeOptions = {}) {
43+
const { window = defaultWindow } = options
2344
const textarea = toRef(options?.element)
2445
const input = toRef(options?.input ?? '')
2546
const styleProp = options?.styleProp ?? 'height'
@@ -52,8 +73,11 @@ export function useTextareaAutosize(options?: UseTextareaAutosizeOptions) {
5273
useResizeObserver(textarea, ([{ contentRect }]) => {
5374
if (textareaOldWidth.value === contentRect.width)
5475
return
55-
textareaOldWidth.value = contentRect.width
56-
triggerResize()
76+
77+
tryRequestAnimationFrame(window, () => {
78+
textareaOldWidth.value = contentRect.width
79+
triggerResize()
80+
})
5781
})
5882

5983
if (options?.watch)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.