2024-12-18 10:51:29 -06:00
|
|
|
<div id="babblebox">
|
|
|
|
<div class="messages" hx-get="/babblebox/messages" hx-trigger="every 5s">
|
|
|
|
<?= $messages ?>
|
|
|
|
</div>
|
2024-12-13 13:15:04 -06:00
|
|
|
|
2024-12-18 10:51:29 -06:00
|
|
|
<form hx-post="/babblebox" hx-target="#babblebox > .messages" style="margin-top: 1rem;">
|
|
|
|
<input type="text" name="babble" maxlength="255"><br>
|
|
|
|
<input type="submit" name="submit" value="Babble">
|
|
|
|
<input type="reset" name="reset" value="Clear">
|
|
|
|
</form>
|
2024-12-13 13:15:04 -06:00
|
|
|
|
2024-12-18 10:51:29 -06:00
|
|
|
<script>
|
2024-12-18 18:47:28 -06:00
|
|
|
let chatBox = document.querySelector('#babblebox > .messages')
|
2024-12-18 10:51:29 -06:00
|
|
|
let isUserAtBottom = true
|
|
|
|
if (chatBox !== null) {
|
|
|
|
chatBox.scrollTop = chatBox.scrollHeight;
|
|
|
|
const isAtBottom = () => chatBox.scrollHeight - chatBox.scrollTop === chatBox.clientHeight
|
2024-12-13 13:15:04 -06:00
|
|
|
|
2024-12-18 10:51:29 -06:00
|
|
|
const scrollChatToBottom = () => {
|
|
|
|
if (isUserAtBottom) chatBox.scrollTop = chatBox.scrollHeight;
|
|
|
|
}
|
2024-12-13 13:15:04 -06:00
|
|
|
|
2024-12-18 10:51:29 -06:00
|
|
|
const observer = new MutationObserver(scrollChatToBottom)
|
|
|
|
observer.observe(chatBox, { childList: true, subtree: true })
|
2024-12-13 13:15:04 -06:00
|
|
|
|
2024-12-18 10:51:29 -06:00
|
|
|
chatBox.addEventListener('scroll', () => isUserAtBottom = isAtBottom())
|
2024-12-13 13:15:04 -06:00
|
|
|
}
|
2024-12-18 10:51:29 -06:00
|
|
|
</script>
|
|
|
|
</div>
|