Skip to content

Commit

Permalink
Use dispatch util function in MorphRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
afcapel committed Oct 8, 2023
1 parent 3f45254 commit a653d12
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/core/drive/morph_renderer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Idiomorph from "idiomorph"
import { nextAnimationFrame } from "../../util"
import { dispatch, nextAnimationFrame } from "../../util"
import { Renderer } from "../renderer"

export class MorphRenderer extends Renderer {
Expand All @@ -17,7 +17,12 @@ export class MorphRenderer extends Renderer {
this.#morphElements(this.currentElement, this.newElement)
this.#reloadRemoteFrames()

this.#dispatchEvent("turbo:morph", { currentElement: this.currentElement, newElement: this.newElement })
dispatch("turbo:morph", {
detail: {
currentElement: this.currentElement,
newElement: this.newElement
}
})
}

#morphElements(currentElement, newElement, morphStyle = "outerHTML") {
Expand Down Expand Up @@ -47,7 +52,10 @@ export class MorphRenderer extends Renderer {
}

#morphFrameUpdate = (currentElement, newElement) => {
this.#dispatchEvent("turbo:before-frame-morph", { currentElement, newElement }, currentElement)
dispatch("turbo:before-frame-morph", {
target: currentElement,
detail: { currentElement, newElement }
})
this.#morphElements(currentElement, newElement, "innerHTML")
}

Expand Down Expand Up @@ -79,10 +87,4 @@ export class MorphRenderer extends Renderer {
#remoteFrames() {
return document.querySelectorAll("turbo-frame[src]")
}

#dispatchEvent(name, detail, target = document.documentElement) {
const event = new CustomEvent(name, { bubbles: true, cancelable: true, detail })
target.dispatchEvent(event)
return event
}
}

0 comments on commit a653d12

Please sign in to comment.