/
index.html.heex
45 lines (39 loc) · 1.73 KB
/
index.html.heex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<.wc_breadcrumb class="bg-white h-6 text-slate-400">
<:crumb>Hooks</:crumb>
<:crumb>PhxWCHook</:crumb>
</.wc_breadcrumb>
<.wc_card class="w-[100%_!important] h-[calc(100vh-56px-2em)_!important] overflow-auto">
<:title class="flex flex-row justify-center items-center">
<span class="text-4xl text-blue-600 hover:text-blue-400">
PhxWCHook
</span>
</:title>
<.wc_markdown content={"""
Custom hook `PhxWCHook` for connect custom elements to phoenix.
This will send and receive event to live view.
```js
import "phoenix_webcomponent";
const PhxWCHook = window.__PhxWCHook__;
const liveSocket = new LiveSocket("/live", Socket, {hooks: { PhxWCHook }});
```
Send custom events to live view:
```html
<Element phx-wc-send-sync-content="load_content" phx-hook="PhxWCHook" />
<Element phx-wc-send-sync-content="load_content;loadAccepted" phx-hook="PhxWCHook" />
```
- In the first element, when element trigger customEvents `sync-content`, also use `pushEvent` send `load_content` to live view.
- Second element are same as first, but will call `loadAccepted` on element when receive server send feedback.
Receive live view event:
```html
<Element phx-wc-receive-update_content="updateContent" phx-hook="PhxWCHook" />
<!-- equal -->
<Element phx-wc-receive="update_content;updateContent" phx-hook="PhxWCHook" />
```
- In this case, when live view fire `update_content` event, also trigger `updateContent` method on elmenet.
- If value(`updateContent`) is empty, trigger a same event `update_content` on element.
Also add css
```css
import "phoenix_webcomponent/priv/static/phoenix_webcomponent.css"
```
"""} />
</.wc_card>