open-webui/src/lib/components/common/Textarea.svelte

41 lines
948 B
Svelte
Raw Normal View History

2024-10-20 06:17:47 +00:00
<script lang="ts">
import { onMount, tick } from 'svelte';
export let value = '';
export let placeholder = '';
2024-10-21 02:04:30 +00:00
export let rows = 1;
export let required = false;
2024-10-20 06:17:47 +00:00
export let className =
'w-full rounded-lg px-3 py-2 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none resize-none h-full';
let textareaElement;
2024-11-24 07:10:12 +00:00
// Adjust height on mount and after setting the element.
2024-10-20 06:17:47 +00:00
onMount(async () => {
await tick();
2024-11-24 07:10:12 +00:00
adjustHeight();
2024-10-20 06:17:47 +00:00
});
2024-11-24 07:10:12 +00:00
// This reactive statement runs whenever `value` changes
$: adjustHeight();
2024-10-24 10:13:35 +00:00
2024-11-24 07:10:12 +00:00
// Adjust height to match content
2024-10-20 06:17:47 +00:00
const adjustHeight = () => {
if (textareaElement) {
2024-11-24 07:10:12 +00:00
// Reset height to calculate the correct scroll height
textareaElement.style.height = 'auto';
2024-10-20 06:17:47 +00:00
textareaElement.style.height = `${textareaElement.scrollHeight}px`;
}
};
</script>
<textarea
bind:this={textareaElement}
bind:value
{placeholder}
2024-10-21 02:04:30 +00:00
on:input={adjustHeight}
2024-10-20 06:17:47 +00:00
class={className}
2024-10-21 02:04:30 +00:00
{rows}
{required}
2024-10-20 06:17:47 +00:00
/>