CableReady
Search…
Morph Sanity Checklist
morphdom can be a fickle muse.
There are some rules you must follow to achieve successful results when using the morph operation, especially when setting childrenOnly: true . Confusion arises when developers don't take care to verify that the outer-most container element of the content they are providing must match the element that you're morphing into, even though that outer-most container element will be discarded during the morph process.
We've done our best to collect most of the typical gotchas one might encounter in one place. The following examples assume the following div is the content which will be morphed.
1
<div id="foo">Your muscles... they are so tight.</div>
Copied!

You cannot change the attributes of your morph target.

Even if you maintain the same CSS selector, you cannot modify any attributes (including data attributes) of the container element with the morph operation.
1
cable_ready["stream"]
2
.morph(
3
selector: "#foo",
4
html: "<div id=\"foo\" data-muscles=\"sore\">data-muscles will not be set.</div>"
5
)
Copied!
You might consider using one of the other CableReady operations like outer_html or set_attribute instead.

Your top-level content needs to be an element.

It's not enough for the container selector to match. Your content needs to be wrapped in an element (eg. not a text node) or else the StimulusReflex data-reflex-permanent attribute will not work.
1
cable_ready["stream"]
2
.morph(
3
selector: "#foo",
4
html: "<div id=\"foo\"><p>Strengthen your core.</p></div>"
5
)
Copied!

No closing tag? No problem.

Inexplicably, morphdom just doesn't seem to care if your top-level element node is closed.
1
cable_ready["stream"]
2
.morph(
3
selector: "#foo",
4
html: "<div id=\"foo\"><span>Who needs muscl</span>"
5
)
Copied!

Different element type altogether? Who cares, so long as the CSS selector matches?

Go ahead, turn your div into a span. morphdom just doesn't care.
1
cable_ready["stream"]
2
.morph(
3
selector: "#foo",
4
html: "<span id=\"foo\">Are these muscles or rocks? lol</span>"
5
)
Copied!

A new CSS selector (or no CSS selector) will be processed as innerHTML

Changing the CSS selector will result in some awkward nesting issues.
1
cable_ready["stream"]
2
.morph(
3
selector: "#foo",
4
html: "<div id=\"baz\">Let me know if this is too strong.</div>"
5
)
Copied!
1
<div id="foo">
2
<div id="baz">Let me know if this is too strong.</div>
3
</div>
Copied!

If the element with the CSS selector is surrounded, external content will be discarded.

1
cable_ready["stream"]
2
.morph(
3
selector: "#foo",
4
html: "I am excited to see your <div id=\"foo\">muscles</div> next week."
5
)
Copied!
1
<div id="foo">muscles</div>
Copied!

If an element matches the target CSS selector, other elements will be ignored.

1
cable_ready["stream"]
2
.morph(
3
selector: "#foo",
4
html: "<div id=\"foo\">Foo!</div><div id=\"post_foo\">Avant-Foo!</div>"
5
)
Copied!
1
<div id="foo">Foo!</div>
Copied!

This is true even if the elements are reversed.

1
cable_ready["stream"]
2
.morph(
3
selector: "#foo",
4
html: "<div id=\"post_foo\">Avant-Foo!</div><div id=\"foo\">Foo!</div>"
5
)
Copied!
1
<div id="foo">Foo!</div>
Copied!

But it's all good in the hood if the selector is not present. 🤦

1
cable_ready["stream"]
2
.morph(
3
selector: "#foo",
4
html: "<div id=\"mike\">Mike</div> and <div id=\"ike\">Ike</div>"
5
)
Copied!
1
<div id="foo">
2
<div id="mike">Mike</div>
3
and
4
<div id="ike">Ike</div>
5
</div>
Copied!
If you spot another weird edge case, please let us know!
Last modified 4mo ago
Export as PDF
Copy link