Skip to content

Instantly share code, notes, and snippets.

@davidvanleeuwen
Created August 9, 2022 13:03
Show Gist options
  • Save davidvanleeuwen/8480b92850d47887093badb59dc6b670 to your computer and use it in GitHub Desktop.
Save davidvanleeuwen/8480b92850d47887093badb59dc6b670 to your computer and use it in GitHub Desktop.
Page transitions in Phoenix Liveview
defp live_helpers do
quote do
alias Phoenix.LiveView.JS
...
def transition(from, to) do
%{
id: Ecto.UUID.generate,
"phx-hook": "transition",
"data-transition-from": from,
"data-transition-to": to
}
end
...
end
end
const Hooks = {
transition: {
mounted() {
this.from = this.el.getAttribute('data-transition-from').split(' ')
this.to = this.el.getAttribute('data-transition-to').split(' ')
this.el.classList.add(...this.from)
setTimeout(() => {
this.el.classList.remove(...this.from)
this.el.classList.add(...this.to)
}, 10)
},
updated() {
this.el.classList.remove('transition')
this.el.classList.remove(...this.from)
}
},
}
...
const liveSocket = new LiveSocket("/", Socket, {
params: {
_csrf_token: csrfToken,
},
hooks: Hooks
})
<div class="transition duration-100 ease-out opacity-0 scale-95" {transition("opacity-0 scale-95", "opacity-100 scale-100")}>
...
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment