Hello, i’m currently implementing infinite scroll feature in my chat app and i have two functionalities which don’t work together. Those are:
phx-hook="ScrollDown"
responsible for scrolling down every time a message is sent and when liveview is mounted.phx-viewport-top="load_more"
responsible for loading 20 messages older than currently loaded oldest one
ScrollDown hook works well by itself but after i added phx-view-top
my app acts as if the js hook did not exist.
This is my whole heex, but the relevant part is inside <ul>
tags.
<div class="flex flex-col mx-auto w-full max-w-2xl h-[80vh] mt-16 my-auto">
<h1 class="text-4xl font-medium">Talk to the founder</h1>
<!-- Box for messages list -->
<!-- MESSAGES LIST -->
<ul
id="messages"
phx-update="stream"
phx-viewport-top="load_more"
phx-hook="ScrollDown"
class="mt-3 border border-neutral-400 p-2 rounded text-sm h-full overflow-y-auto shadow"
>
<div :for={{dom_id, message} <- @streams.messages} id={dom_id}>
<li class="p-1 hover:bg-neutral-300 rounded relative pb-4 break-words">
<b><%= message.name %></b>
<br /> <%= message.content %>
<span class="font-light font-mono text-xs text-neutral-500 absolute bottom-0 right-2">
<%= message.inserted_at %>
</span>
</li>
</div>
</ul>
<!-- END MESSAGES LIST -->
<!-- END Box for messages list -->
<!-- Message box -->
<.form for={@message_form} phx-submit="send_message" class="flex">
<input
name="message"
rows="1"
class="peer block w-full appearance-none border-0 border-b border-gray-500 bg-transparent py-2.5 px-0 text-sm text-gray-900 focus:border-violet-600 focus:outline-none focus:ring-0 mr-2"
placeholder="Your message"
autocomplete="off"
/>
<.button class="mt-5 rounded-md bg-neutral-800 hover:bg-neutral-700 px-6 py-2 text-sm text-white h-12">
<.icon name="hero-paper-airplane" class="w-5 w-5" />
</.button>
</.form>
<!-- END Message box -->
</div>
I’m not sure what can i say about my code as it seems pretty self-explanatory, questions are welcome
JS Hook:
export default ScrollDown = {
mounted() {
this.el.scrollTop = this.el.scrollHeight
window.addEventListener('phx:scroll_down', () => {
this.el.scrollTop = this.el.scrollHeight
})
},
updated() {
this.el.scrollTop = this.el.scrollHeight
}
}
I would be very thankful for some help as i’m currently out of ideas on why such behaviour occurs