-
Notifications
You must be signed in to change notification settings - Fork 435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract and re-use element morphing logic #1234
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { FrameElement } from "../../elements/frame_element" | ||
import { MorphingFrameRenderer } from "../frames/morphing_frame_renderer" | ||
import { PageRenderer } from "./page_renderer" | ||
import { dispatch } from "../../util" | ||
import { morphElements } from "../morphing" | ||
|
||
export class MorphingPageRenderer extends PageRenderer { | ||
static renderElement(currentElement, newElement) { | ||
morphElements(currentElement, newElement, { | ||
callbacks: { | ||
beforeNodeMorphed: element => !canRefreshFrame(element) | ||
} | ||
}) | ||
|
||
for (const frame of currentElement.querySelectorAll("turbo-frame")) { | ||
if (canRefreshFrame(frame)) refreshFrame(frame) | ||
} | ||
|
||
dispatch("turbo:morph", { detail: { currentElement, newElement } }) | ||
} | ||
|
||
async preservingPermanentElements(callback) { | ||
return await callback() | ||
} | ||
|
||
get renderMethod() { | ||
return "morph" | ||
} | ||
|
||
get shouldAutofocus() { | ||
return false | ||
} | ||
} | ||
|
||
function canRefreshFrame(frame) { | ||
return frame instanceof FrameElement && | ||
frame.src && | ||
frame.refresh === "morph" && | ||
!frame.closest("[data-turbo-permanent]") | ||
} | ||
|
||
function refreshFrame(frame) { | ||
frame.addEventListener("turbo:before-frame-render", ({ detail }) => { | ||
detail.render = MorphingFrameRenderer.renderElement | ||
}, { once: true }) | ||
|
||
frame.reload() | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { FrameRenderer } from "./frame_renderer" | ||
import { morphChildren } from "../morphing" | ||
import { dispatch } from "../../util" | ||
|
||
export class MorphingFrameRenderer extends FrameRenderer { | ||
static renderElement(currentElement, newElement) { | ||
dispatch("turbo:before-frame-morph", { | ||
target: currentElement, | ||
detail: { currentElement, newElement } | ||
}) | ||
|
||
morphChildren(currentElement, newElement) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Idiomorph } from "idiomorph/dist/idiomorph.esm.js" | ||
import { dispatch } from "../util" | ||
|
||
export function morphElements(currentElement, newElement, { callbacks, ...options } = {}) { | ||
Idiomorph.morph(currentElement, newElement, { | ||
...options, | ||
callbacks: new DefaultIdiomorphCallbacks(callbacks) | ||
}) | ||
} | ||
|
||
export function morphChildren(currentElement, newElement) { | ||
morphElements(currentElement, newElement.children, { | ||
morphStyle: "innerHTML" | ||
}) | ||
} | ||
|
||
class DefaultIdiomorphCallbacks { | ||
#beforeNodeMorphed | ||
|
||
constructor({ beforeNodeMorphed } = {}) { | ||
this.#beforeNodeMorphed = beforeNodeMorphed || (() => true) | ||
} | ||
|
||
beforeNodeAdded = (node) => { | ||
return !(node.id && node.hasAttribute("data-turbo-permanent") && document.getElementById(node.id)) | ||
} | ||
|
||
beforeNodeMorphed = (currentElement, newElement) => { | ||
if (currentElement instanceof Element) { | ||
if (!currentElement.hasAttribute("data-turbo-permanent") && this.#beforeNodeMorphed(currentElement, newElement)) { | ||
const event = dispatch("turbo:before-morph-element", { | ||
cancelable: true, | ||
target: currentElement, | ||
detail: { currentElement, newElement } | ||
}) | ||
|
||
return !event.defaultPrevented | ||
} else { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
beforeAttributeUpdated = (attributeName, target, mutationType) => { | ||
const event = dispatch("turbo:before-morph-attribute", { | ||
cancelable: true, | ||
target, | ||
detail: { attributeName, mutationType } | ||
}) | ||
|
||
return !event.defaultPrevented | ||
} | ||
|
||
beforeNodeRemoved = (node) => { | ||
return this.beforeNodeMorphed(node) | ||
} | ||
|
||
afterNodeMorphed = (currentElement, newElement) => { | ||
if (currentElement instanceof Element) { | ||
dispatch("turbo:morph-element", { | ||
target: currentElement, | ||
detail: { currentElement, newElement } | ||
}) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change adheres to the current style.
As a related change as a follow-up, #1240 proposes extending the existing
[action="replace"]
and[action="update"]
with new[method="morph"]
attributes named in a way that corresponds to the value used in the<meta name="turbo-refresh-method" content="morph">
element.