Skip to content

Commit 5025e3a

Browse files
yakudikalexander.kudinovantfu
authoredFeb 20, 2024··
feat(useTextareaAutosize): allow configuring styleProp to support native rows attribute (#3552)
Co-authored-by: alexander.kudinov <ka@modumlab.com> Co-authored-by: Anthony Fu <github@antfu.me> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent cf29c4c commit 5025e3a

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed
 

‎packages/core/useTextareaAutosize/index.md

+39
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Automatically update the height of a textarea depending on the content.
88

99
## Usage
1010

11+
### Simple example
12+
1113
```vue
1214
<script setup lang="ts">
1315
const { textarea, input } = useTextareaAutosize()
@@ -22,3 +24,40 @@ const { textarea, input } = useTextareaAutosize()
2224
/>
2325
</template>
2426
```
27+
28+
::: info
29+
30+
It's recommended to reset the scrollbar styles for the textarea element to avoid incorrect height values for large amounts of text.
31+
32+
```css
33+
textarea {
34+
-ms-overflow-style: none;
35+
scrollbar-width: none;
36+
}
37+
38+
textarea::-webkit-scrollbar {
39+
display: none;
40+
}
41+
```
42+
43+
:::
44+
45+
### With `rows` attribute
46+
47+
If you need support for the rows attribute on a textarea element, then you should set the `styleProp` option to `minHeight`.
48+
49+
```vue
50+
<script setup lang="ts">
51+
const { textarea, input } = useTextareaAutosize({ styleProp: 'minHeight' })
52+
</script>
53+
54+
<template>
55+
<textarea
56+
ref="textarea"
57+
v-model="input"
58+
class="resize-none"
59+
placeholder="What's on your mind?"
60+
rows="3"
61+
/>
62+
</template>
63+
```

‎packages/core/useTextareaAutosize/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ export interface UseTextareaAutosizeOptions {
1515
onResize?: () => void
1616
/** Specify style target to apply the height based on textarea content. If not provided it will use textarea it self. */
1717
styleTarget?: MaybeRef<HTMLElement>
18+
/** Specify the style property that will be used to manipulate height. Can be `height | minHeight`. Default value is `height`. */
19+
styleProp?: 'height' | 'minHeight'
1820
}
1921

2022
export function useTextareaAutosize(options?: UseTextareaAutosizeOptions) {
2123
const textarea = ref<HTMLTextAreaElement>(options?.element as any)
2224
const input = ref<string>(options?.input as any)
25+
const styleProp = options?.styleProp ?? 'height'
2326
const textareaScrollHeight = ref(1)
2427

2528
function triggerResize() {
@@ -28,17 +31,17 @@ export function useTextareaAutosize(options?: UseTextareaAutosizeOptions) {
2831

2932
let height = ''
3033

31-
textarea.value!.style.height = '1px'
34+
textarea.value.style[styleProp] = '1px'
3235
textareaScrollHeight.value = textarea.value?.scrollHeight
3336

3437
// If style target is provided update its height
3538
if (options?.styleTarget)
36-
toValue(options.styleTarget).style.height = `${textareaScrollHeight.value}px`
39+
toValue(options.styleTarget).style[styleProp] = `${textareaScrollHeight.value}px`
3740
// else update textarea's height by updating height variable
3841
else
3942
height = `${textareaScrollHeight.value}px`
4043

41-
textarea.value!.style.height = height
44+
textarea.value.style[styleProp] = height
4245

4346
options?.onResize?.()
4447
}

0 commit comments

Comments
 (0)
Please sign in to comment.