/phoenix-hooks:phoenix-hooksfrom phoenix-hooks
Use when we need an explanation of standard Phoenix LiveView client hooks (phx-hook) and when to use colocated vs regular hooks
Ships in phoenix-hooks. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
phx-hook)LiveView client hooks let you attach JavaScript behavior to elements that LiveView manages. You register hook objects in your JS bundle and reference them from the DOM using phx-hook.
A hook object referenced by phx-hook can implement these callbacks:
mounted – element added to the DOM and its LiveView has finished mountingbeforeUpdate – element is about to be updated in the DOM (must be synchronous)updated – element has been updated in the DOM by the serverdestroyed – element removed from the page (by parent update or removal)disconnected – parent LiveView disconnected from the serverreconnected – parent LiveView reconnected to the serverNote: outside of a LiveView context, only mounted fires and only for elements present at DOM ready.
Inside each callback, this is a ViewHook instance with:
this.el – the bound DOM nodethis.liveSocket – the underlying LiveSocket instancethis.pushEvent(event, payload, (reply, ref) => ...) – push an event to the parent LiveView; returns a Promise if no callback is providedthis.pushEventTo(selectorOrTarget, event, payload, (reply, ref) => ...) – push targeted events to specific LiveViews/LiveComponents via a selector or DOM node (e.g. this.el); if multiple targets, Promise resolves like Promise.allSettled() with {reply, ref} objectsthis.handleEvent(event, (payload) => ...) – register handler for events pushed from the server; returns a refthis.removeHandleEvent(ref) – remove an event handler registered via handleEventthis.upload(name, files) – inject file-like objects into an uploaderthis.uploadTo(selectorOrTarget, name, files) – inject files into an uploader scoped by selector/targetthis.js() – returns a JS command helper for DOM manipulation integrated with LiveView’s patchingMarkup:
<input
type="text"
name="user[phone_number]"
id="user-phone-number"
phx-hook="PhoneNumber"
/>
JS hook registration:
/**
* @type {import("phoenix_live_view").HooksOptions}
*/
let Hooks = {};
Hooks.PhoneNumber = {
mounted() {
this.el.addEventListener("input", e => {
let match = this.el.value
.replace(/\D/g, "")
.match(/^(\d{3})(\d{3})(\d{4})$/);
if (match) {
this.el.value = `${match[1]}-${match[2]}-${match[3]}`;
}
});
},
};
import {Socket} from "phoenix";
import {LiveSocket} from "phoenix_live_view";
let liveSocket = new LiveSocket("/live", Socket, { hooks: Hooks, /* ... */ });
Rules:
phx-hook must reference the hook name as registered in your hooks objectphx-hook must have a unique DOM iddom.onBeforeElUpdatedFor deeper integration with third-party libraries that manage the DOM, you can use the dom option on LiveSocket:
let liveSocket = new LiveSocket("/live", Socket, {
params: {_csrf_token: csrfToken},
hooks: Hooks,
dom: {
onBeforeElUpdated(from, to) {
for (const attr of from.attributes) {
if (attr.name.startsWith("data-js-")) {
to.setAttribute(attr.name, attr.value);
}
}
},
},
});
onBeforeElUpdated(from, to) runs just before LiveView patches the DOMViewHook SubclassesYou can also define hooks as subclasses of ViewHook:
import {ViewHook} from "phoenix_live_view";
class MyHook extends ViewHook {
mounted() {
// your logic here
}
}
let liveSocket = new LiveSocket("/live", Socket, {
hooks: { MyHook },
});
This is useful when you want class-based composition or inheritance for hooks.
LiveView supports both standard hooks (defined in your JS bundle) and colocated hooks/JS (defined in HEEx templates):
phx-hook="MyHook"Phoenix.LiveView.ColocatedHook and imported from phoenix-colocated/my_appPhoenix.LiveView.ColocatedJSGuidance:
phoenix-colocated-hooks and phoenix-colocated-js skills).hooks module.phx-hook names match the registered hook name exactly, and hooks are wired into your LiveSocket via the hooks option.What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub dvcrn/skills --plugin phoenix-hooks