fix: adjust height when value changes, close #429

This commit is contained in:
Xylight committed 2024-09-11 19:55:09 -07:00
1 parent f94a7e82eb
commit 3f93285392
2 files changed
+19 -9

No files matched your search

+11 -4
View File
@@ -1,13 +1,20 @@
<script lang="ts"> <script lang="ts">
import { browser } from '$app/environment'
import { tick } from 'svelte'
export let label: string | undefined = undefined export let label: string | undefined = undefined
export let value: string export let value: string
export let required: boolean = false export let required: boolean = false
let textarea: HTMLTextAreaElement let textarea: HTMLTextAreaElement
function adjustHeight() { $: adjustHeight(value)
textarea.style.height = 'auto' // Reset height to auto to calculate new height async function adjustHeight(value: string) {
textarea.style.height = `${textarea.scrollHeight}px` // Set height to the scrollHeight await tick()
if (textarea) {
textarea.style.height = 'auto' // Reset height to auto to calculate new height
textarea.style.height = `${textarea.scrollHeight}px` // Set height to the scrollHeight
}
} }
</script> </script>
@@ -22,7 +29,7 @@
{/if} {/if}
<textarea <textarea
bind:this={textarea} bind:this={textarea}
on:input={adjustHeight} on:input={() => adjustHeight(value)}
bind:value bind:value
{required} {required}
{...$$restProps} {...$$restProps}
@@ -6,7 +6,7 @@
import { uploadImage } from '$lib/util.js' import { uploadImage } from '$lib/util.js'
import { ImageInput, toast } from 'mono-svelte' import { ImageInput, toast } from 'mono-svelte'
import { Button, Label, Modal, TextArea } from 'mono-svelte' import { Button, Label, Modal, TextArea } from 'mono-svelte'
import { createEventDispatcher } from 'svelte' import { createEventDispatcher, tick } from 'svelte'
import { import {
Bold, Bold,
CodeBracket, CodeBracket,
@@ -84,12 +84,15 @@
}, },
} }
function adjustHeight() { async function adjustHeight() {
textArea.style.height = 'auto' // Reset height to auto to calculate new height await tick()
textArea.style.height = `${textArea.scrollHeight}px` // Set height to the scrollHeight if (textArea) {
textArea.style.height = 'auto' // Reset height to auto to calculate new height
textArea.style.height = `${textArea.scrollHeight}px` // Set height to the scrollHeight
}
} }
$: if (textArea && !previewing) adjustHeight() $: if (!previewing && value) adjustHeight()
</script> </script>
{#if uploadingImage && images} {#if uploadingImage && images}