Skip to content

Add onDocumentPatch callback and allow specifying event dispatch phase - #4043

Merged
SteffenDE merged 3 commits into
mainfrom
sd-beforePatch
Nov 20, 2025
Merged

Add onDocumentPatch callback and allow specifying event dispatch phase#4043
SteffenDE merged 3 commits into
mainfrom
sd-beforePatch

Conversation

@SteffenDE

@SteffenDE SteffenDE commented Nov 4, 2025

Copy link
Copy Markdown
Member

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:

let liveSocket = new LiveSocket("/live", Socket, {
  dom: {
    onDocumentPatch(start) {
      // do something
      // then trigger the patch
      start();
      // do something afterwards
    }
  }
})

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:

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.

Thanks to @spicychickensauce!

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
SteffenDE marked this pull request as ready for review November 7, 2025 14:06
@SteffenDE
SteffenDE requested a review from chrismccord November 7, 2025 14:06
@SteffenDE
SteffenDE requested a review from josevalim November 14, 2025 16:58
Comment thread assets/js/phoenix_live_view/index.ts Outdated
* ```javascript
* let liveSocket = new LiveSocket("/live", Socket, {
* dom: {
* beforePatch(start) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like onDocumentPatch!

@SteffenDE SteffenDE changed the title Add beforePatch callback and allow specifying event dispatch phase Add onDocumentPatch callback and allow specifying event dispatch phase Nov 19, 2025
@SteffenDE
SteffenDE merged commit 7f8b237 into main Nov 20, 2025
14 of 16 checks passed
@SteffenDE
SteffenDE deleted the sd-beforePatch branch November 20, 2025 09:56
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.
@SteffenDE SteffenDE mentioned this pull request Dec 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants