Skip to content
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

feat(useTextareaAutosize): allow configuring styleProp to support native rows attribute #3552

Merged
merged 6 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/core/useTextareaAutosize/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,25 @@ category: Browser

Automatically update the height of a textarea depending on the content.

::: warning
antfu marked this conversation as resolved.
Show resolved Hide resolved
Don't forget to reset the scrollbar styles for the textarea element to avoid incorrect height values for large amounts of text.
antfu marked this conversation as resolved.
Show resolved Hide resolved

```css
textarea {
-ms-overflow-style: none;
scrollbar-width: none;
}

textarea::-webkit-scrollbar {
display: none;
}
```
:::

## Usage

### Simple example

```vue
<script setup lang="ts">
const { textarea, input } = useTextareaAutosize()
Expand All @@ -22,3 +39,23 @@ const { textarea, input } = useTextareaAutosize()
/>
</template>
```

### With `rows` attribute

If you need support for the rows attribute on a textarea element, then you should set the `styleProp` option to `minHeight`.

```vue
<script setup lang="ts">
const { textarea, input } = useTextareaAutosize({ styleProp: 'minHeight' })
</script>

<template>
<textarea
ref="textarea"
v-model="input"
class="resize-none"
placeholder="What's on your mind?"
rows="3"
/>
</template>
```
9 changes: 6 additions & 3 deletions packages/core/useTextareaAutosize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ export interface UseTextareaAutosizeOptions {
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>
/** Specify the style property that will be used to manipulate height. Can be `height | minHeight`. Default value is `height`. */
styleProp?: 'height' | 'minHeight'
}

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

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

let height = ''

textarea.value!.style.height = '1px'
textarea.value.style[styleProp] = '1px'
textareaScrollHeight.value = textarea.value?.scrollHeight

// If style target is provided update its height
if (options?.styleTarget)
toValue(options.styleTarget).style.height = `${textareaScrollHeight.value}px`
toValue(options.styleTarget).style[styleProp] = `${textareaScrollHeight.value}px`
// else update textarea's height by updating height variable
else
height = `${textareaScrollHeight.value}px`

textarea.value!.style.height = height
textarea.value.style[styleProp] = height

options?.onResize?.()
}
Expand Down