Add onDocumentPatch callback and allow specifying event dispatch phase - #4043
Merged
Conversation
Relates to: https://elixirforum.com/t/extending-liveview-with-view-transition-api-support/73136 This commit adds a generic dom hook that runs before the view is patched: ```javascript let liveSocket = new LiveSocket("/live", Socket, { dom: { beforePatch(start) { // do something // then trigger the patch start(); } } }) ``` This can be used to call [document.startViewTransition](https://developer.mozilla.org/en-US/docs/Web/API/Document/startViewTransition). To be able to customize what kind of view transition to perform, this commit also introduces a new optional fourth parameter on `Phoenix.LiveView.push_event/3`. By default, events are only dispatched after the view is patched, but sometimes it is useful to run some code before patching the DOM: ```elixir def mount(_params, _session, socket) do {:ok, socket |> assign(...) |> push_event("start-view-transition", %{type: "page"}, dispatch: :before)} end ``` This event will be dispatched before performing the initial mount patch.
SteffenDE
marked this pull request as ready for review
November 7, 2025 14:06
jeregrine
approved these changes
Nov 13, 2025
josevalim
reviewed
Nov 16, 2025
| * ```javascript | ||
| * let liveSocket = new LiveSocket("/live", Socket, { | ||
| * dom: { | ||
| * beforePatch(start) { |
Member
There was a problem hiding this comment.
I am wondering if it should be called beforePatch, given it receives a function, which I assume does the whole patching, and therefore you can execute something afterwards? There is also onPatchStart and onPatchEnd, which could add to the confusion.
I'd suggest something like onDocumentPatchStart or onDocumentPatch, to make it clear this is invoked before patching the whole document. Or onTreePatchStart etc. Thoughts?
Member
Author
There was a problem hiding this comment.
I like onDocumentPatch!
josevalim
approved these changes
Nov 20, 2025
SteffenDE
added a commit
that referenced
this pull request
Nov 24, 2025
#4043) Relates to: https://elixirforum.com/t/extending-liveview-with-view-transition-api-support/73136 This commit adds a generic dom hook that runs before the view is patched: ```javascript let liveSocket = new LiveSocket("/live", Socket, { dom: { onDocumentPatch(start) { // do something // then trigger the patch start(); } } }) ``` This can be used to call [document.startViewTransition](https://developer.mozilla.org/en-US/docs/Web/API/Document/startViewTransition). To be able to customize what kind of view transition to perform, this commit also introduces a new optional fourth parameter on `Phoenix.LiveView.push_event/3`. By default, events are only dispatched after the view is patched, but sometimes it is useful to run some code before patching the DOM: ```elixir def mount(_params, _session, socket) do {:ok, socket |> assign(...) |> push_event("start-view-transition", %{type: "page"}, dispatch: :before)} end ``` This event will be dispatched before performing the initial mount patch.
Closed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Relates to: https://elixirforum.com/t/extending-liveview-with-view-transition-api-support/73136
https://gist.github.com/SteffenDE/cf7cdb91ba037b08cdc583763e4ffc69
This commit adds a generic dom hook that runs before the view is patched:
This can be used to call document.startViewTransition.
To be able to customize what kind of view transition to perform, this commit also introduces a new optional fourth parameter on
Phoenix.LiveView.push_event/3. By default, events are only dispatched after the view is patched, but sometimes it is useful to run some code before patching the DOM:This event will be dispatched before performing the initial mount patch.
Thanks to @spicychickensauce!