-
Notifications
You must be signed in to change notification settings - Fork 433
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
Add Turbo stream morph action #1185
Merged
jorgemanrubia
merged 4 commits into
hotwired:main
from
omarluq:omarluq/turbo-stream-morph-action
Mar 14, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8b2228e
Add Turbo stream morph action
omarluq 268dfbc
limit morph action data atrributes to morphStyle only and add morph l…
omarluq 276ee38
extract morph action and add children-only option to stream-element
omarluq f02bfb2
test morph stream action events
omarluq 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 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,63 @@ | ||
import { Idiomorph } from "idiomorph/dist/idiomorph.esm" | ||
import { dispatch } from "../../../util" | ||
|
||
export default function morph(streamElement) { | ||
const morphStyle = streamElement.hasAttribute("children-only") ? "innerHTML" : "outerHTML" | ||
streamElement.targetElements.forEach((element) => { | ||
Idiomorph.morph(element, streamElement.templateContent, { | ||
morphStyle: morphStyle, | ||
callbacks: { | ||
beforeNodeAdded, | ||
beforeNodeMorphed, | ||
beforeAttributeUpdated, | ||
beforeNodeRemoved, | ||
afterNodeMorphed | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
function beforeNodeAdded(node) { | ||
return !(node.id && node.hasAttribute("data-turbo-permanent") && document.getElementById(node.id)) | ||
} | ||
|
||
function beforeNodeRemoved(node) { | ||
return beforeNodeAdded(node) | ||
} | ||
|
||
function beforeNodeMorphed(target, newElement) { | ||
if (target instanceof HTMLElement && !target.hasAttribute("data-turbo-permanent")) { | ||
const event = dispatch("turbo:before-morph-element", { | ||
cancelable: true, | ||
target, | ||
detail: { | ||
newElement | ||
} | ||
}) | ||
return !event.defaultPrevented | ||
} | ||
return false | ||
} | ||
|
||
function beforeAttributeUpdated(attributeName, target, mutationType) { | ||
const event = dispatch("turbo:before-morph-attribute", { | ||
cancelable: true, | ||
target, | ||
detail: { | ||
attributeName, | ||
mutationType | ||
} | ||
}) | ||
return !event.defaultPrevented | ||
} | ||
|
||
function afterNodeMorphed(target, newElement) { | ||
if (newElement instanceof HTMLElement) { | ||
dispatch("turbo:morph-element", { | ||
target, | ||
detail: { | ||
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
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,16 @@ | ||
<!DOCTYPE html> | ||
<html id="html"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Morph Stream Action</title> | ||
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script> | ||
<script src="/src/tests/fixtures/test.js"></script> | ||
<meta name="turbo-refresh-method" content="replace"> | ||
</head> | ||
|
||
<body> | ||
<div id="message_1"> | ||
<div>Morph me</div> | ||
</div> | ||
</body> | ||
</html> |
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 { test, expect } from "@playwright/test" | ||
import { nextEventOnTarget, noNextEventOnTarget } from "../helpers/page" | ||
|
||
test("dispatches a turbo:before-morph-element & turbo:morph-element for each morph stream action", async ({ page }) => { | ||
await page.goto("/src/tests/fixtures/morph_stream_action.html") | ||
|
||
await page.evaluate(() => { | ||
window.Turbo.renderStreamMessage(` | ||
<turbo-stream action="morph" target="message_1"> | ||
<template> | ||
<div id="message_1"> | ||
<h1>Morphed</h1> | ||
</div> | ||
</template> | ||
</turbo-stream> | ||
`) | ||
}) | ||
|
||
await nextEventOnTarget(page, "message_1", "turbo:before-morph-element") | ||
await nextEventOnTarget(page, "message_1", "turbo:morph-element") | ||
await expect(page.locator("#message_1")).toHaveText("Morphed") | ||
}) | ||
|
||
test("preventing a turbo:before-morph-element prevents the morph", async ({ page }) => { | ||
await page.goto("/src/tests/fixtures/morph_stream_action.html") | ||
|
||
await page.evaluate(() => { | ||
addEventListener("turbo:before-morph-element", (event) => { | ||
event.preventDefault() | ||
}) | ||
}) | ||
|
||
await page.evaluate(() => { | ||
window.Turbo.renderStreamMessage(` | ||
<turbo-stream action="morph" target="message_1"> | ||
<template> | ||
<div id="message_1"> | ||
<h1>Morphed</h1> | ||
</div> | ||
</template> | ||
</turbo-stream> | ||
`) | ||
}) | ||
|
||
await nextEventOnTarget(page, "message_1", "turbo:before-morph-element") | ||
await noNextEventOnTarget(page, "message_1", "turbo:morph-element") | ||
await expect(page.locator("#message_1")).toHaveText("Morph me") | ||
}) |
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.
@jorgemanrubia duplicating the
MorphRenderer
logic feels like a big risk. Would generalizing the details of how we invoke Idiomorph (through something like #1192) help here?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.
I lagree with @seanpdoyle I think
morph_elements.js
can be a drop in replacement for the stream action morph implementation.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.
@seanpdoyle the problem is that the current implementation of the page renderer has logic interweaved to deal with morphing frames. I think we need to untangle that in #1192, but I still don't see a clear separation there, and I haven't had proper time for looking into that yet. Agree that, ultimately, we should invoke the same morphing logic from everywhere, but we need to do some deep refactor to accommodate that.
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.
How does the exclusion of the Frame handling logic from the MorphRenderer impact the Stream's behavior?
Are there downsides to handling Frames consistently across Drive navigation morphs and Stream morphs?
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.
@jorgemanrubia In response to #1185 (comment), I've opened #1234 to share the bulk of the morphing logic across Pages, Frames, and Streams.