Skip to content

Commit 4468d13

Browse files
authored
Merge pull request #485 from phoenixframework/sd-runtime-hook
document runtime colocated hook usage
2 parents fab5e4f + fed9e58 commit 4468d13

1 file changed

Lines changed: 41 additions & 9 deletions

File tree

lib/phoenix/live_dashboard/page_builder.ex

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule Phoenix.LiveDashboard.PageBuilder do
2-
@moduledoc """
2+
@moduledoc ~S'''
33
Page builder is the default mechanism for building custom dashboard pages.
44
55
Each dashboard page is a LiveView with additional callbacks for
@@ -19,7 +19,7 @@ defmodule Phoenix.LiveDashboard.PageBuilder do
1919
2020
@impl true
2121
def render(assigns) do
22-
~H\"""
22+
~H"""
2323
<.live_table
2424
id="ets-table"
2525
dom_id="ets-table"
@@ -40,7 +40,7 @@ defmodule Phoenix.LiveDashboard.PageBuilder do
4040
<%= encode_pid(ets[:owner]) %>
4141
</:col>
4242
</.live_table>
43-
\"""
43+
"""
4444
end
4545
4646
defp fetch_ets(params, node) do
@@ -108,9 +108,41 @@ defmodule Phoenix.LiveDashboard.PageBuilder do
108108
109109
## Custom Hooks
110110
111-
If your page needs to register custom hooks, you can use the `register_after_opening_head_tag/2`
112-
function. Because the hooks need to be available on the dead render in the layout, before the
113-
LiveView's LiveSocket is configured, your need to do this inside an `on_mount` hook:
111+
### Colocated Hooks
112+
113+
If you're using LiveView 1.1 or later, you can use [**runtime** colocated hooks](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.ColocatedHook.html#module-runtime-hooks):
114+
115+
```elixir
116+
defmodule MyAppWeb.MyCustomPage do
117+
use Phoenix.LiveDashboard.PageBuilder
118+
119+
...
120+
121+
@impl true
122+
def render(assigns) do
123+
~H"""
124+
<script :type={Phoenix.LiveView.ColocatedHook} name=".MyHook" runtime nonce={@csp_nonces.script}>
125+
{
126+
mounted() {
127+
// do something
128+
}
129+
}
130+
</script>
131+
132+
<div id="my-element" phx-hook=".MyHook">...</div>
133+
"""
134+
end
135+
end
136+
```
137+
138+
In case you are not using CSP nonces, you can omit the nonce attribute.
139+
140+
### LiveView hooks and registerCustomHooks
141+
142+
If your are on an earlier version of LiveView or runtime hooks don't work for another reason,
143+
you can use the `register_after_opening_head_tag/2` function. Because the hooks need to be available
144+
on the dead render in the layout, before the LiveView's LiveSocket is configured, you
145+
need to do this inside an `on_mount` hook:
114146
115147
```elixir
116148
defmodule MyAppWeb.MyLiveDashboardHooks do
@@ -124,7 +156,7 @@ defmodule Phoenix.LiveDashboard.PageBuilder do
124156
end
125157
126158
defp after_opening_head_tag(assigns) do
127-
~H\"\"\"
159+
~H"""
128160
<script nonce={@csp_nonces[:script]}>
129161
window.LiveDashboard.registerCustomHooks({
130162
MyHook: {
@@ -134,7 +166,7 @@ defmodule Phoenix.LiveDashboard.PageBuilder do
134166
}
135167
})
136168
</script>
137-
\"\"\"
169+
"""
138170
end
139171
end
140172
@@ -183,7 +215,7 @@ defmodule Phoenix.LiveDashboard.PageBuilder do
183215
>
184216
> If your custom page needs a different CSP policy, for example due to inline styles set by scripts,
185217
> please consider documenting these requirements.
186-
"""
218+
'''
187219

188220
use Phoenix.Component
189221

0 commit comments

Comments
 (0)