2024-06-01 12:47:14 -05:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { autoGrowHeight } from '$lib/actions/autogrow';
|
|
|
|
|
import { shortcut } from '$lib/actions/shortcut';
|
|
|
|
|
import { tick } from 'svelte';
|
|
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
interface Props {
|
|
|
|
|
content?: string;
|
|
|
|
|
class?: string;
|
|
|
|
|
onContentUpdate?: (newContent: string) => void;
|
|
|
|
|
placeholder?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let { content = '', class: className = '', onContentUpdate = () => null, placeholder = '' }: Props = $props();
|
2024-06-01 12:47:14 -05:00
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
let textarea: HTMLTextAreaElement | undefined = $state();
|
|
|
|
|
let newContent = $state(content);
|
2024-06-01 12:47:14 -05:00
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
$effect(() => {
|
2024-09-02 14:41:19 -05:00
|
|
|
if (textarea && newContent.length > 0) {
|
2024-08-05 19:13:00 +00:00
|
|
|
void tick().then(() => autoGrowHeight(textarea));
|
|
|
|
|
}
|
2024-11-14 08:43:25 -06:00
|
|
|
});
|
2024-06-01 12:47:14 -05:00
|
|
|
|
|
|
|
|
const updateContent = () => {
|
|
|
|
|
if (content === newContent) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
onContentUpdate(newContent);
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<textarea
|
|
|
|
|
bind:this={textarea}
|
|
|
|
|
class="resize-none {className}"
|
2024-11-14 08:43:25 -06:00
|
|
|
onfocusout={updateContent}
|
|
|
|
|
oninput={(e) => (newContent = e.currentTarget.value)}
|
2024-06-01 12:47:14 -05:00
|
|
|
{placeholder}
|
|
|
|
|
use:shortcut={{
|
|
|
|
|
shortcut: { key: 'Enter', ctrl: true },
|
|
|
|
|
onShortcut: (e) => e.currentTarget.blur(),
|
|
|
|
|
}}
|
|
|
|
|
data-testid="autogrow-textarea">{content}</textarea
|
|
|
|
|
>
|