npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phoenix-custom-event-hook

v0.0.6

Published

This package is a [Phoenix LiveView](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html) [hook](https://hexdocs.pm/phoenix_live_view/js-interop.html#client-hooks) that allows you to easily send [Custom Events](https://developer.mozilla.org/en-US/d

Downloads

3,620

Readme

phoenix-custom-event-hook

This package is a Phoenix LiveView hook that allows you to easily send Custom Events to your live view.

Installation

In your phoenix project assets directory

npm install phoenix-custom-event-hook

Usage

  1. Add the PhoenixCustomEvent hook to your LiveSocket
import PhoenixCustomEvent from 'phoenix-custom-event-hook';

let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, { params: { _csrf_token: csrfToken }, hooks: { PhoenixCustomEvent } })
  1. Add phx-hook and phx-send-events attributes to elements in your template.

In this example, the lit-google-element emits a bounds_changed custom event which will become live_view event. The payload will be the detail of the custom event, merged with any data- attribute values (the event target dataset). This can be customized if needed (see below).

<lit-google-map api-key="" phx-hook="PhoenixCustomEvent" phx-send-events="bounds_changed">
  1. Handle the event in your live view
  def handle_event(
        "bounds_changed",
        %{"north" => north, "east" => east, "west" => west, "south" => south},
        socket
      ) do
    airports =
      Airports.list_airports_in_bounds(%{north: north, east: east, west: west, south: south})

    {:noreply, socket |> assign(airports: airports)}
  end

An target component can be specified by assigning a component id to your custom element's phx-target attribute. In this example, any events emitted by the lit-google-map element will be handled by the LiveComponent that renders it, rather than the LiveView.

<lit-google-map api-key="" phx-hook="PhoenixCustomEvent" phx-target="<%= @myself %>" phx-custom-event-bounds_changed="bounds_changed">

Not currently supported: multiple event targets, targeting events by CSS selector.

Loading events

This hook will also dispatch the following events on the element it is added to:

  • phx-event-start when an event is sent to live view
  • phx-event-complete when a reply is received

Receiving events

If you wish to receive events from LiveView, add a phx-receive-events attribute to the element this hook is defined on which contains a list of events you wish to receive. Each event will become a CustomEvent of the same name with the detail property containing the payload.

For example, in LiveView:

  socket
  |> push_event("message_updated", %{message: "HI there"})

In your Custom Element:

  this.addEventListener("message_updated", ({ detail: { message } }) => {
    console.log(message);
  });

Event serialization

As of version 0.0.6, the payload for the event pushed to live view will contain:

  • the detail property of the custom event
  • the dataset from the event target

This will be merged together into the payload sent to LiveView. If you wish to override this behaviour, you may define your own implemention of the serializeEvent function on the hook object, for example:

import PhoenixCustomEvent from 'phoenix-custom-event-hook';
PhoenixCustomEvent.serializeEvent = (_event) => { return {foo: 'bar' } };

License

MIT.