Skip to content

Commit a696244

Browse files
committed
automatically resize textarea
1 parent b7884a2 commit a696244

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/components/SuggestionForm.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script lang="ts">
2+
import { resize_textarea } from '$lib/client/utils'
23
import { faCheckCircle, faWarning } from '@fortawesome/free-solid-svg-icons'
34
import { tick } from 'svelte'
45
import Fa from 'svelte-fa'
@@ -60,7 +61,8 @@
6061

6162
<div class="form-group">
6263
<label for="body">Details</label>
63-
<textarea id="body" bind:value={body} required></textarea>
64+
<textarea id="body" {@attach resize_textarea} bind:value={body} required
65+
></textarea>
6466
</div>
6567

6668
<button class="button" disabled={sending}>

src/lib/client/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,21 @@ export function string_to_color(str: string): string {
2626
const h = hash % 360
2727
return `hsl(${h}, 80%, 50%)`
2828
}
29+
30+
export const resize_textarea: Attachment = (textarea) => {
31+
if (!(textarea instanceof HTMLTextAreaElement)) return
32+
33+
textarea.style.height = `${textarea.scrollHeight}px`
34+
textarea.style.overflowY = 'hidden'
35+
36+
const adjust = () => {
37+
textarea.style.height = 'auto'
38+
textarea.style.height = `${textarea.scrollHeight}px`
39+
}
40+
41+
textarea.addEventListener('input', adjust)
42+
43+
return () => {
44+
textarea.removeEventListener('input', adjust)
45+
}
46+
}

0 commit comments

Comments
 (0)