Skip to content

Navigation Menu

Sign in
Appearance settings

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

fix(useTextareaAutosize): improve resize handling with requestAnimationFrame #4557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 15, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(useTextareaAutosize): add tryRequestAnimationFrame for fallback
Co-authored-by: Robin <37191683+OrbisK@users.noreply.github.com>
  • Loading branch information
ilyaliao and OrbisK committed Feb 14, 2025
commit 45a0e6db5e7ce625361a4b3d8e984d2003bfbf70
22 changes: 20 additions & 2 deletions 22 packages/core/useTextareaAutosize/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { MaybeRef } from '@vueuse/shared'
import type { Fn, MaybeRef } from '@vueuse/shared'
import type { WatchSource } from 'vue'
import { toRef } from '@vueuse/shared'
import { nextTick, ref, toValue, watch } from 'vue'
import { type ConfigurableWindow, defaultWindow } from '../_configurable'

Check failure on line 5 in packages/core/useTextareaAutosize/index.ts

View workflow job for this annotation

GitHub Actions / lint

Prefer using a top-level type-only import instead of inline type specifiers
import { useResizeObserver } from '../useResizeObserver'

export interface UseTextareaAutosizeOptions extends ConfigurableWindow {
Expand All @@ -20,6 +20,24 @@
styleProp?: 'height' | 'minHeight'
}

/**
* Call window.requestAnimationFrame(), if not available, just call the function
*
* @param window
* @param fn
*/
function tryRequestAnimationFrame(
window: Window | undefined = defaultWindow,
fn: Fn,
) {
if (window && typeof window.requestAnimationFrame === 'function') {
window.requestAnimationFrame(fn)
}
else {
fn()
}
}

export function useTextareaAutosize(options: UseTextareaAutosizeOptions = {}) {
const { window = defaultWindow } = options
const textarea = toRef(options?.element)
Expand Down Expand Up @@ -55,7 +73,7 @@
if (textareaOldWidth.value === contentRect.width)
return

window?.requestAnimationFrame(() => {
tryRequestAnimationFrame(window, () => {
textareaOldWidth.value = contentRect.width
triggerResize()
})
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.