Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
export const PhxWCHook = { | ||
mounted() { | ||
const attrs = this.el.attributes; | ||
const phxTarget = attrs["phx-target"].value; | ||
const pushEvent = phxTarget | ||
? (event, payload, callback) => | ||
this.pushEventTo(phxTarget, event, payload, callback) | ||
: this.pushEvent; | ||
|
||
for (var i = 0; i < attrs.length; i++) { | ||
if (/^phx-wc-send-/.test(attrs[i].name)) { | ||
const eventName = attrs[i].name.replace(/^phx-wc-send-/, ""); | ||
const [phxEvent, callbackName] = attrs[i].value.split(';'); | ||
this.el.addEventListener(eventName, ({ detail }) => { | ||
pushEvent(phxEvent, detail, (e) => { | ||
this[callbackName]?.(e, detail, eventName) | ||
}); | ||
}); | ||
} | ||
if (/^phx-wc-receive-/.test(attrs[i].name)) { | ||
const eventName = attrs[i].name.replace(/^phx-wc-receive-/, ""); | ||
const handler = attrs[i].value; | ||
this.handleEvent(eventName, (payload) => { | ||
if (handler && this.el[handler]) { | ||
this.el[handler]?.(payload); | ||
} else { | ||
this.el.dispatchEvent(new CustomEvent(eventName, { detail: payload })); | ||
} | ||
}); | ||
} | ||
if ('phx-wc-receive' === attrs[i].name) { | ||
const [phxEvent, callbackName] = attrs[i].value.split(';'); | ||
this.handleEvent(phxEvent, (payload) => { | ||
this.el[callbackName]?.(payload); | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
import '@gsmlg/lit'; | ||
|
||
export { PhxWCHook } from './hook'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 34 additions & 24 deletions
58
apps/phx_wc_storybook_web/lib/phx_wc_storybook_web/templates/page/index.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,40 @@ | ||
|
||
<.wc_card class="w-[100%_!important] h-[calc(100vh-56px-2em)_!important]"> | ||
<:title class="flex flex-row justify-center items-center"> | ||
<%= link "To Storybook", to: Routes.live_storybook_path(@conn, :root), class: "text-4xl text-blue-600 hover:text-blue-400" %> | ||
<span class="text-4xl text-blue-600 hover:text-blue-400"> | ||
PhxWCHook | ||
</span> | ||
</:title> | ||
<.wc_markdown content={""" | ||
|
||
<div class="flex flex-col justify-start items-start"> | ||
<div> | ||
wc_actionbar | ||
</div> | ||
<div> | ||
wc_appbar | ||
</div> | ||
<div> | ||
wc_card | ||
</div> | ||
<div> | ||
wc_left_menu | ||
</div> | ||
<div> | ||
wc_markdown | ||
</div> | ||
<div> | ||
wc_pagination | ||
</div> | ||
<div> | ||
wc_table | ||
</div> | ||
</div> | ||
Custom hook `PhxWCHook` for connect custom elements to phoenix. | ||
This will send and receive event to live view. | ||
|
||
```js | ||
const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content"); | ||
|
||
import { PhxWCHook } from "phoenix_webcomponent"; | ||
|
||
const liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}, 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. | ||
|
||
"""}> | ||
</.wc_markdown> | ||
</.wc_card> |