#resize textarea with wire:stream
13 messages · Page 1 of 1 (latest)
This is my textarea
<textarea
x-data="{
outputLogModel: $wire.$entangle('outputLog'),
resize() {
$el.style.height = '0px';
$el.style.height = $el.scrollHeight + 'px';
}
}"
x-init="$nextTick(resize); $watch('outputLogModel', () => resize())"
x-on:input="resize()"
wire:stream="stream"
wire:model="outputLog"
rows="3"
class="filament-input block w-full transition duration-75 rounded-lg shadow-sm focus:ring-primary-500 focus:border-primary-500 border-gray-300 resize-none"
>{{ $outputLog }}</textarea>
Maybe listen/observe changes on the textarea like this (it's an function called in the x-data):
function textareaResizer() {
return {
init() {
const observer = new MutationObserver(() => {
this.resize();
});
observer.observe(messagesContainer, {
childList: true,
subtree: true,
});
// Initial resize, optional
this.resize();
},
resize() {
$el.style.height = '0px';
$el.style.height = $el.scrollHeight + 'px';
}
};
}
It's from head, so it's maybe not working :/
with this works perfectly, except that it doesn't continue all the way to the end of the viewport. That is, since the screen is larger, it can't be seen all the way to the bottom.:
<textarea
x-data="{
outputLogModel: $wire.$entangle('outputLog'),
resize() {
$el.style.height = '0px';
$el.style.height = $el.scrollHeight + 'px';
}
}"
x-init="$nextTick(() => {
const messagesContainer = $el; // Asume que el contenedor es el propio textarea
const observer = new MutationObserver(() => {
resize();
});
observer.observe(messagesContainer, {
childList: true,
subtree: true,
});
// Initial resize
resize();
})"
x-on:input="resize()"
wire:stream="stream"
autofocus
rows="3"
class="filament-input block w-full transition duration-75 rounded-lg shadow-sm focus:ring-primary-500 focus:border-primary-500 border-gray-300 resize-none"
>{{ $outputLog }}</textarea>
Maybe using this function aside of the resize can help to scroll to bottom ?
scrollToBottom(container) {
let container = document.getElementById('#textarea-container');
container.scrollTop = container.scrollHeight;
},
I tried but it doesn't work
#textarea-container is the component you want to scroll on the bottom of, right ?
Or you want to scroll continuously on the bottom of your textarea while streaming content ?
I want to scroll continuously on the bottom of my textarea while streaming content
Allright
Here's a component I made with the same logic 🙂
<textarea
wire:stream="content"
id="textarea"
x-data="{
init: function() {
const textarea = document.getElementById('textarea');
function autoResize() {
if (textarea.value.trim() === '') {
textarea.style.height = '60px';
} else {
textarea.style.height = 'auto';
textarea.style.height = textarea.scrollHeight + 'px';
}
// Scroll to the bottom of the textarea
textarea.scrollTop = textarea.scrollHeight;
}
autoResize();
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList' || mutation.type === 'characterData') {
autoResize();
}
});
});
observer.observe(textarea, {
characterData: true,
subtree: true,
childList: true
});
textarea.addEventListener('input', autoResize, false);
}
}"
></textarea>
Don't forget textarea.scrollTop = textarea.scrollHeight; in the autoResize funcion 😉
I've been trying it out but the textarea starts to increase almost at the end of the viewport.
When it starts to increase the height gets lost at the end and can't be seen. Maybe I should scroll the page instead of the textarea... I have to try.