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): added styleTarget
option to style other element
#2312
Conversation
is `detachStyles` is `true` styles won't get applied to the textarea
Hi @antfu This was a nice improvement that reduces # of lines. Thanks for the suggestion.
<script setup lang="ts">
import { useTextareaAutosize } from '@vueuse/core'
import { ref } from 'vue'
// const { textarea, input } = useTextareaAutosize()
// --
const wrapper = ref()
const textareaCustom = ref()
const { input: wrapperInput } = useTextareaAutosize({
element: textareaCustom,
styleTarget: wrapper,
})
</script>
<template>
<div>
<span>Type, the textarea will grow:</span>
<div ref="wrapper" class="textarea-container outline outline-color-[tomato]">
<textarea ref="textareaCustom" v-model="wrapperInput" class="!m-0 w-full h-full !shadow-none" />
</div>
{{ wrapperInput }}
<!-- <textarea
ref="textarea"
v-model="input"
class="resize-none"
placeholder="What's on your mind?"
/> -->
</div>
</template>
|
styleTarget
option to style other element based on textarea content
textareaScrollHeight.value = textarea.value?.scrollHeight | ||
|
||
// If style target is provided update its height | ||
if (options?.styleTarget) | ||
unref(options.styleTarget).style.height = `${textareaScrollHeight.value}px` | ||
|
||
// else update textarea's height by updating height variable | ||
else height = `${textareaScrollHeight.value}px` | ||
|
||
textarea.value!.style.height = height |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
textareaScrollHeight.value = textarea.value?.scrollHeight | |
// If style target is provided update its height | |
if (options?.styleTarget) | |
unref(options.styleTarget).style.height = `${textareaScrollHeight.value}px` | |
// else update textarea's height by updating height variable | |
else height = `${textareaScrollHeight.value}px` | |
textarea.value!.style.height = height | |
textareaScrollHeight.value = textarea.value?.scrollHeight | |
const styleTarget = unref(options?.styleTarget) || textarea.value | |
styleTarget!.style.height = height |
Do you mean by this? In the previous implementation textarea
's style seems always applied (L39)
Hi @antfu Thanks for your review. I tried your suggested changes but it isn't working because we have to reset the style of textarea so textarea can be rendered as normal (like height isn't modified).
|
styleTarget
option to style other element based on textarea contentstyleTarget
option to style other element
Description
When
detachStyles
istrue
styles won't get applied to the textarea. You can usetextareaScrollHeight
ref returned by this composable to apply the height instead. This is useful in cases where we have custom UI for textarea as in Anu or any other textarea customization.Related PR in Anu: jd-solanki/anu#37
With this PR merged we can set the height of textarea wrapper based on the height of the actual textarea content.
Additional context
This is just not specific to Anu, this is useful even if someone has a custom UI for textarea.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).