Skip to content

Instantly share code, notes, and snippets.

@mkumm
Created July 9, 2021 13:11
Show Gist options
  • Save mkumm/17ebd253411ae2eb416fa11ea6f3e6ae to your computer and use it in GitHub Desktop.
Save mkumm/17ebd253411ae2eb416fa11ea6f3e6ae to your computer and use it in GitHub Desktop.
AlpineJS 3 with Phoenix Live
import "../css/app.scss"
import "phoenix_html"
import {Socket} from "phoenix"
import topbar from "topbar"
import {LiveSocket} from "phoenix_live_view"
import Alpine from 'alpinejs'
window.Alpine = Alpine
Alpine.start()
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {
dom: {
onBeforeElUpdated(from, to) {
if (from._x_dataStack) { window.Alpine.clone(from, to) }
}
},
params: {_csrf_token: csrfToken}
})
topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
window.addEventListener("phx:page-loading-start", info => topbar.show())
window.addEventListener("phx:page-loading-stop", info => topbar.hide())
liveSocket.connect()
window.liveSocket = liveSocket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment