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): added styleTarget option to style other element #2312

Merged
merged 30 commits into from Mar 14, 2023

Conversation

jd-solanki
Copy link
Contributor

Description

When detachStyles is true styles won't get applied to the textarea. You can use textareaScrollHeight 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?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@jd-solanki
Copy link
Contributor Author

Hi @antfu
According to your comment in discord, I have provided styleTarget option instead of detachStyles.

This was a nice improvement that reduces # of lines. Thanks for the suggestion.

demo.vue code if you want to play:

<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>

@jd-solanki jd-solanki changed the title feat(useTextareaAutosize): allow detaching styles feat(useTextareaAutosize): added styleTarget option to style other element based on textarea content Nov 7, 2022
Comment on lines 30 to 39
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

@jd-solanki
Copy link
Contributor Author

jd-solanki commented Nov 7, 2022

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).

Screenshot 2022-11-07 at 6 18 46 PM

P.S. notice the textarea's white background isn't filling the outline/wrapper

@jd-solanki jd-solanki requested a review from antfu November 7, 2022 12:52
@jd-solanki jd-solanki mentioned this pull request Nov 13, 2022
4 tasks
@antfu antfu changed the title feat(useTextareaAutosize): added styleTarget option to style other element based on textarea content feat(useTextareaAutosize): added styleTarget option to style other element Mar 14, 2023
@antfu antfu merged commit a3e9547 into vueuse:main Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants