/phoenix-colocated-hooks:phoenix-colocated-hooksfrom phoenix-colocated-hooks
Use when we need an explanation of phoenix colocated hooks
Ships in phoenix-colocated-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.
Read when SKILL.md points to them.
Colocated hooks let you define Phoenix LiveView client hooks directly alongside your HEEx templates. They live inside a <script> block with a special :type and are compiled into a JS manifest that bundlers can import.
.heex/~H templateDefine a hook inside your LiveView template using the Phoenix.LiveView.ColocatedHook type, and reference it with phx-hook on an element:
defmodule MyAppWeb.DemoLive do
use MyAppWeb, :live_view
def mount(_params, _session, socket) do
{:ok, socket}
end
def render(assigns) do
~H"""
<input
type="text"
name="user[phone_number]"
id="user-phone-number"
phx-hook=".PhoneNumber"
/>
<script :type={Phoenix.LiveView.ColocatedHook} name=".PhoneNumber">
export default {
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]}`
}
})
}
}
</script>
"""
end
end
Key points:
<script> tag uses :type={Phoenix.LiveView.ColocatedHook}name attribute is required and must start with a dot (e.g. .PhoneNumber, not PhoneNumber)phx-hook attribute value must also include the dot (e.g. phx-hook=".PhoneNumber", not phx-hook="PhoneNumber")MyAppWeb.DemoLive.PhoneNumber)At compile time, Phoenix extracts colocated hooks and writes them into a generated JS folder (typically under _build). A manifest file aggregates hooks as named exports for your bundler:
import { hooks } from "phoenix-colocated/my_app"
console.log(hooks)
/*
{
"MyAppWeb.DemoLive.PhoneNumber": { ... },
...
}
*/
Important details:
mix compile before running your assets pipeline so hooks existcompile runs before assets.deploy:# Instead of
release: ["assets.deploy", "release"]
# Use
release: ["compile", "assets.deploy", "release"]
Use this skill when you need:
When not to use colocated hooks:
phoenix-hooks skill).phx-hook semantics (no LiveView hook lifecycle, just exports/utilities), use Phoenix.LiveView.ColocatedJS instead (see the phoenix-colocated-js skill).What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub dvcrn/skills --plugin phoenix-colocated-hooks