{"id":25522295,"url":"https://github.com/launchscout/phoenix-custom-event-hook","last_synced_at":"2025-04-11T01:32:22.032Z","repository":{"id":47627091,"uuid":"307847552","full_name":"launchscout/phoenix-custom-event-hook","owner":"launchscout","description":"A LiveView hook for publishing and handling custom events","archived":false,"fork":false,"pushed_at":"2023-03-06T19:02:55.000Z","size":145,"stargazers_count":30,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-22T23:44:07.605Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/launchscout.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-10-27T22:52:58.000Z","updated_at":"2024-10-07T17:10:36.000Z","dependencies_parsed_at":"2023-07-21T23:55:17.388Z","dependency_job_id":null,"html_url":"https://github.com/launchscout/phoenix-custom-event-hook","commit_stats":null,"previous_names":["gaslight/phoenix-custom-event-hook"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/launchscout%2Fphoenix-custom-event-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/launchscout%2Fphoenix-custom-event-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/launchscout%2Fphoenix-custom-event-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/launchscout%2Fphoenix-custom-event-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/launchscout","download_url":"https://codeload.github.com/launchscout/phoenix-custom-event-hook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248325086,"owners_count":21084866,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-19T18:19:15.531Z","updated_at":"2025-04-11T01:32:22.005Z","avatar_url":"https://github.com/launchscout.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# phoenix-custom-event-hook\n\nThis 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/docs/Web/API/CustomEvent/CustomEvent) to your live view.\n\n## Installation\n\nIn your phoenix project assets directory\n\n```\nnpm install phoenix-custom-event-hook\n```\n\n## Usage\n\n1. Add the PhoenixCustomEvent hook to your LiveSocket\n\n```javascript\nimport PhoenixCustomEvent from 'phoenix-custom-event-hook';\n\nlet csrfToken = document.querySelector(\"meta[name='csrf-token']\").getAttribute(\"content\")\nlet liveSocket = new LiveSocket(\"/live\", Socket, { params: { _csrf_token: csrfToken }, hooks: { PhoenixCustomEvent } })\n```\n\n2. Add `phx-hook` and `phx-send-events` attributes to elements in your template.\n\nIn 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 \nwith any `data-` attribute values (the event target dataset). This can be customized if needed (see below).\n\n```html\n\u003clit-google-map api-key=\"\" phx-hook=\"PhoenixCustomEvent\" phx-send-events=\"bounds_changed\"\u003e\n```\n\n3. Handle the event in your live view\n\n```elixir\n  def handle_event(\n        \"bounds_changed\",\n        %{\"north\" =\u003e north, \"east\" =\u003e east, \"west\" =\u003e west, \"south\" =\u003e south},\n        socket\n      ) do\n    airports =\n      Airports.list_airports_in_bounds(%{north: north, east: east, west: west, south: south})\n\n    {:noreply, socket |\u003e assign(airports: airports)}\n  end\n```\n\nAn 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.\n\n```html\n\u003clit-google-map api-key=\"\" phx-hook=\"PhoenixCustomEvent\" phx-target=\"\u003c%= @myself %\u003e\" phx-custom-event-bounds_changed=\"bounds_changed\"\u003e\n```\n\nNot currently supported: multiple event targets, targeting events by CSS selector.\n\n## Loading events\n\nThis hook will also dispatch the following events on the element it is added to:\n\n* `phx-event-start` when an event is sent to live view\n* `phx-event-complete` when a reply is received\n\n## Receiving events\n\nIf 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. \n\nFor example, in LiveView:\n\n```elixir\n  socket\n  |\u003e push_event(\"message_updated\", %{message: \"HI there\"})\n```\n\nIn your Custom Element:\n\n```javascript\n  this.addEventListener(\"message_updated\", ({ detail: { message } }) =\u003e {\n    console.log(message);\n  });\n```\n\n## Event serialization\n\nAs of version 0.0.6, the payload for the event pushed to live view will contain:\n\n* the detail property of the custom event\n* the dataset from the event target\n\nThis 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:\n\n```js\nimport PhoenixCustomEvent from 'phoenix-custom-event-hook';\nPhoenixCustomEvent.serializeEvent = (_event) =\u003e { return {foo: 'bar' } };\n```\n\n## License\n\n[MIT](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaunchscout%2Fphoenix-custom-event-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaunchscout%2Fphoenix-custom-event-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaunchscout%2Fphoenix-custom-event-hook/lists"}