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