<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="188261" data-post-id="188261">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="samaaron" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/samaaron/120/20199_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  samaaron
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>OK, for those looking to work with mouse down events, here’s the things I had to learn/do:</p>
<ol>
<li>Create the HTML element in the call to <code>render</code> with an <code>id</code> and <code>phx-hook</code> attributes,</li>
<li>Modify my <code>app.js</code> to work with the hook and specifically handle the element’s mouse down event</li>
<li>Add a new event handler in my LiveView component to receive the event.</li>
</ol>
<p>Let’s look at each in turn…</p>
<p><strong>1. Creating the HTML element</strong></p>
<p>Somewhere in your <code>render</code> you need to have a HTML element you’d like to listen to events on. I’m using a button here. Your element needs to have a unique ID (this can be anything but is typically hyphen-separated such as <code>"lovely-button"</code>) and also a <code>phx-hook</code> attribute which can also be anything but is typically CamelCase such as <code>YoButton</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">&lt;button id="lovely-button" phx-hook="YoButton"&gt;yo button!&lt;/button&gt;
</code></pre>
<p><strong>2. Handling the JS event</strong></p>
<p>Now we have our HTML element, we need to add the appropriate javascript event handler. This will be called when the button is clicked and will then tell the server via the <code>pushEvent</code> function.</p>
<p>First we need to edit our <code>app.js</code> to include a <code>Hooks</code> object which includes an internal <code>YoButton</code> object (this needs to be named similarly to the <code>phx-hook</code> attribute above).</p>
<p>LiveView gives us a <code>mounted</code> callback to use which is called when all the server and client initialisation is completed, so we know things are ready to go.</p>
<p>Inside this callback, we can add our event handler which listens for <code>'mousedown'</code> events and then uses <code>pushEvent</code> to tell the server:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">let Hooks = {}

Hooks.YoButton = {
  mounted(){
    this.el.addEventListener('mousedown', e =&gt; {
     this.pushEvent("mouseymouse");
    });
  }
}
</code></pre>
<p>We also need to teach our <code>liveSocket</code> about the Hooks:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">let liveSocket = new LiveSocket("/live", Socket, {
  hooks: Hooks,
  params: {_csrf_token: csrfToken},
  ....
</code></pre>
<p><strong>3. Receiving the event on the server</strong></p>
<p>This is probably the easiest bit. In our LiveView component we just need a new handler function:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def handle_event("mouseymouse", _params, socket) do
   # do something interesting!
  {:noreply, socket}
end
</code></pre>
<p>I hope that this helps someone trying to do something similar <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="188261" data-batch-url="/posts/batch_likers">
                        31
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/phoenix-liveview-request-proposal-mouse-events/31213/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-188261" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="188261"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="329091" data-post-id="329091">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Anko" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Anko
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>came here after reading this post by John Carmack <a href="https://twitter.com/ID_AA_Carmack/status/1787850053912064005" rel="noopener nofollow ugc">https://twitter.com/ID_AA_Carmack/status/1787850053912064005</a></p>
<p>basically he advocates for mouse down making interfaces a lot snappier.  I have tried it and it does really seem to work… Maybe it’s worth reviving this conversation?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="329091" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/phoenix-liveview-request-proposal-mouse-events/31213/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-329091" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="329091"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="329093" data-post-id="329093">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="samaaron" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/samaaron/120/20199_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  samaaron
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Totally agreed. My use-case is music apps and in that context it’s ludicrous for mouse-off being the trigger rather than mouse-on. Imagine a cymbal that make a noise when the drumstick left it or a clarinet that sounded when you stopped blowing <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Of course, with practice you could compensate for the latency just like organ scholars do - but it’s not natural and definitely way less fun.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="329093" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/phoenix-liveview-request-proposal-mouse-events/31213/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-329093" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="329093"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>