-
-
Save davidvanleeuwen/8480b92850d47887093badb59dc6b670 to your computer and use it in GitHub Desktop.
Page transitions in Phoenix Liveview
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
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 |
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
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 | |
}) |
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
<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