Dragon-Knight/templates/babblebox.php

30 lines
986 B
PHP
Raw Normal View History

<div id="babblebox">
<div class="messages" hx-get="/babblebox/messages" hx-trigger="every 5s">
<?= $messages ?>
</div>
2024-12-13 13:15:04 -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
<script>
let chatBox = document.querySelector('#babblebox > .messages')
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
const scrollChatToBottom = () => {
if (isUserAtBottom) chatBox.scrollTop = chatBox.scrollHeight;
}
2024-12-13 13:15:04 -06:00
const observer = new MutationObserver(scrollChatToBottom)
observer.observe(chatBox, { childList: true, subtree: true })
2024-12-13 13:15:04 -06:00
chatBox.addEventListener('scroll', () => isUserAtBottom = isAtBottom())
2024-12-13 13:15:04 -06:00
}
</script>
</div>