| name | phoenix-liveview-hooks |
| description | Implement Phoenix LiveView JavaScript hooks correctly. Use when adding or reviewing `phx-hook` behavior in HEEx, choosing between colocated hook scripts and external hooks in `assets/js`, wiring hook registration into `LiveSocket`, or enforcing hook safety rules like unique IDs and `phx-update=\"ignore\"` for DOM-managed nodes. |
LiveView Hooks
Use this skill to choose and implement the right LiveView hook pattern.
Quick Decision
- Use a colocated hook when behavior is local to one template and should ship with that HEEx file.
- Use an external hook when behavior is reused across multiple pages/components.
Colocated Hook Pattern
- Add a unique element id and a dot-prefixed hook name on the element.
- Add a colocated script block with
:type={Phoenix.LiveView.ColocatedHook}.
- Keep the hook name dot-prefixed (example:
.PhoneNumber).
- Do not use raw
<script> tags in HEEx.
Reference example: references/liveview-js-hooks.md (Inline colocated js hooks).
External Hook Pattern
- Define hook object in
assets/js.
- Register it in
LiveSocket under hooks.
- Use the hook via
phx-hook="HookName" on elements with stable DOM ids.
- If the hook manages its own DOM updates, set
phx-update="ignore" on that node.
Reference example: references/liveview-js-hooks.md (External phx-hook).
Interop Rules
- Always provide a unique DOM id for elements with
phx-hook.
- Rebind or return the socket from
push_event/3 calls.
See complete extracted guidance and examples in references/liveview-js-hooks.md.