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
Merged
Changes from 11 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a00b5be
feat(useTextareaAutosize): allow detaching styles
jd-solanki Oct 11, 2022
5f4dc46
Merge branch 'main' into main
jd-solanki Oct 11, 2022
1c069e5
Merge branch 'main' into main
jd-solanki Oct 17, 2022
4efa8a5
Merge branch 'main' into main
jd-solanki Oct 24, 2022
1fa68f8
Merge branch 'main' into main
jd-solanki Oct 25, 2022
4d317ef
Merge branch 'main' into main
jd-solanki Oct 31, 2022
dbecf1b
Merge branch 'main' into main
jd-solanki Nov 3, 2022
d11f3ba
Merge branch 'main' into main
jd-solanki Nov 6, 2022
ac4817d
feat!: `detachStyles` replaced with `styleTarget`
jd-solanki Nov 6, 2022
872c8c2
chore: fix typing
jd-solanki Nov 6, 2022
6a8b3f8
refactor: remove unwanted `textareaScrollHeight` return
jd-solanki Nov 6, 2022
8d801ff
Merge branch 'main' into main
jd-solanki Nov 7, 2022
bfd541a
Merge branch 'main' into main
jd-solanki Nov 9, 2022
097e2de
Merge branch 'main' into main
jd-solanki Nov 10, 2022
e5e432e
Merge branch 'main' into main
jd-solanki Nov 11, 2022
3b167ea
Merge branch 'main' into main
jd-solanki Nov 12, 2022
45e1843
Merge branch 'main' into main
jd-solanki Nov 13, 2022
d7b9a09
Merge branch 'main' into main
jd-solanki Nov 18, 2022
b8e55d3
Merge branch 'main' into main
jd-solanki Nov 27, 2022
2fd70f5
Merge branch 'main' into main
jd-solanki Dec 16, 2022
469e38e
Merge branch 'main' into main
jd-solanki Dec 26, 2022
905db2d
Merge branch 'main' into main
jd-solanki Jan 18, 2023
615316b
Merge branch 'main' into main
jd-solanki Jan 26, 2023
96556f1
Merge branch 'main' into main
jd-solanki Feb 16, 2023
0067af2
Merge branch 'main' into main
jd-solanki Feb 19, 2023
d16725a
Merge branch 'main' into main
jd-solanki Feb 19, 2023
19b02bd
Merge branch 'main' into main
jd-solanki Feb 24, 2023
fb82721
Merge branch 'main' into main
jd-solanki Mar 2, 2023
3bde000
Merge branch 'main' into main
jd-solanki Mar 7, 2023
4e90f5e
chore: format
antfu Mar 14, 2023
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
18 changes: 16 additions & 2 deletions packages/core/useTextareaAutosize/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MaybeRef } from '@vueuse/shared'
import type { WatchSource } from 'vue-demi'
import { ref, watch } from 'vue-demi'
import { ref, unref, watch } from 'vue-demi'

export interface UseTextareaAutosizeOptions {
/** Textarea element to autosize. */
Expand All @@ -11,18 +11,32 @@ export interface UseTextareaAutosizeOptions {
watch?: WatchSource | Array<WatchSource>
/** 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>
}

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

function triggerResize() {
if (!textarea.value)
return

let height = ''

textarea.value!.style.height = '1px'
textarea.value!.style.height = `${textarea.value?.scrollHeight}px`
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)


options?.onResize?.()
}
Expand Down