📄 useTextareaAutosize¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 3 |
| 📦 Imports | 13 |
| 🟢 Vue Composition API | 3 |
| 📐 Interfaces | 2 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/core/useTextareaAutosize/index.ts
📦 Imports¶
| Name | Source |
|---|---|
Fn |
@vueuse/shared |
MaybeRef |
vue |
MultiWatchSources |
vue |
Ref |
vue |
WatchSource |
vue |
ConfigurableWindow |
../_configurable |
toRef |
@vueuse/shared |
nextTick |
vue |
shallowRef |
vue |
toValue |
vue |
watch |
vue |
defaultWindow |
../_configurable |
useResizeObserver |
../useResizeObserver |
Vue Composition API¶
| Name | Type | Reactive Variables | Composables |
|---|---|---|---|
watch |
watch | none | none |
watch |
watch | none | none |
watch |
watch | none | none |
Functions¶
useTextareaAutosize(options: UseTextareaAutosizeOptions): UseTextareaAutosizeReturn¶
Parameters:
optionsUseTextareaAutosizeOptions
Returns: UseTextareaAutosizeReturn
Calls:
toRef (from @vueuse/shared)shallowRef (from vue)toValue (from vue)Math.minwatch (from vue)nextTick (from vue)options?.onResizeuseResizeObserver (from ../useResizeObserver)tryRequestAnimationFrametriggerResize
Internal Comments:
Code
export function useTextareaAutosize(options: UseTextareaAutosizeOptions = {}): UseTextareaAutosizeReturn {
const { window = defaultWindow } = options
const textarea = toRef(options?.element)
const input = toRef(options?.input ?? '')
const styleProp = options?.styleProp ?? 'height'
const textareaScrollHeight = shallowRef(1)
const textareaOldWidth = shallowRef(0)
function triggerResize() {
if (!textarea.value)
return
let height = ''
const maxHeight = options?.maxHeight
textarea.value.style[styleProp] = '1px'
textareaScrollHeight.value = textarea.value?.scrollHeight
const _styleTarget = toValue(options?.styleTarget)
const styleHeight = maxHeight != null
? `${Math.min(textareaScrollHeight.value, maxHeight)}px`
: `${textareaScrollHeight.value}px`
// If style target is provided update its height
if (_styleTarget)
_styleTarget.style[styleProp] = styleHeight
// else update textarea's height by updating height variable
else
height = styleHeight
textarea.value.style[styleProp] = height
}
watch([input, textarea], () => nextTick(triggerResize), { immediate: true })
watch(textareaScrollHeight, () => options?.onResize?.())
useResizeObserver(textarea, ([{ contentRect }]) => {
if (textareaOldWidth.value === contentRect.width)
return
tryRequestAnimationFrame(window, () => {
textareaOldWidth.value = contentRect.width
triggerResize()
})
})
if (options?.watch)
watch(options.watch, triggerResize, { immediate: true, deep: true })
return {
textarea,
input,
triggerResize,
}
}
tryRequestAnimationFrame(window: Window | undefined, fn: Fn): void¶
Call window.requestAnimationFrame(), if not available, just call the function
Parameters:
windowany: No descriptionfnany: No description
Raw JSDoc
Calls:
window.requestAnimationFramefn
Code
Internal helpers¶
Declared inside another function in this file.
triggerResize(): void¶
Returns: void
Calls:
toValue (from vue)Math.min
Internal Comments:
Code
function triggerResize() {
if (!textarea.value)
return
let height = ''
const maxHeight = options?.maxHeight
textarea.value.style[styleProp] = '1px'
textareaScrollHeight.value = textarea.value?.scrollHeight
const _styleTarget = toValue(options?.styleTarget)
const styleHeight = maxHeight != null
? `${Math.min(textareaScrollHeight.value, maxHeight)}px`
: `${textareaScrollHeight.value}px`
// If style target is provided update its height
if (_styleTarget)
_styleTarget.style[styleProp] = styleHeight
// else update textarea's height by updating height variable
else
height = styleHeight
textarea.value.style[styleProp] = height
}
Interfaces¶
UseTextareaAutosizeOptions¶
Interface Code
export interface UseTextareaAutosizeOptions extends ConfigurableWindow {
/** Textarea element to autosize. */
element?: MaybeRef<HTMLTextAreaElement | undefined | null>
/** Textarea content. */
input?: MaybeRef<string>
/** Maximum autosized height in pixels. */
maxHeight?: number
/** Watch sources that should trigger a textarea resize. */
watch?: WatchSource | MultiWatchSources
/** Function called when the textarea size changes. */
onResize?: () => void
/** Specify style target to apply the height based on textarea content. If not provided it will use textarea it self. */
styleTarget?: MaybeRef<HTMLElement | undefined>
/** Specify the style property that will be used to manipulate height. Can be `height | minHeight`. Default value is `height`. */
styleProp?: 'height' | 'minHeight'
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
element |
MaybeRef<HTMLTextAreaElement \| undefined \| null> |
✓ | not shown |
input |
MaybeRef<string> |
✓ | not shown |
maxHeight |
number |
✓ | not shown |
watch |
WatchSource \| MultiWatchSources |
✓ | not shown |
onResize |
() => void |
✓ | not shown |
styleTarget |
MaybeRef<HTMLElement \| undefined> |
✓ | not shown |
styleProp |
'height' \| 'minHeight' |
✓ | not shown |
UseTextareaAutosizeReturn¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
textarea |
Ref<HTMLTextAreaElement \| undefined \| null> |
✗ | not shown |
input |
Ref<string> |
✗ | not shown |
triggerResize |
() => void |
✗ | not shown |
Generated by Syntax Scribe