Skip to content

Commit

Permalink
Morph within an animation frame
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Mar 8, 2024
1 parent f4b35b0 commit 146300c
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 2 deletions.
4 changes: 3 additions & 1 deletion dist/morphlex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/morphlex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ class Morph {
this.#mapSensivity(node);
}

this.#morphNode(node, reference);
requestAnimationFrame(() => {
this.#morphNode(node, reference);
});
}

#mapSensivity(node: ReadonlyNode<ParentNode>): void {
Expand Down
15 changes: 15 additions & 0 deletions test/alpine-morph.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fixture, html, expect } from "@open-wc/testing";
import { morph } from "../";
import { nextFrame } from "./helpers";

// adapted from: https://github.com/alpinejs/alpine/blob/891d68503960a39826e89f2f666d9b1e7ce3f0c9/tests/jest/morph/external.spec.js
describe("alpine-morph", () => {
Expand All @@ -9,6 +10,8 @@ describe("alpine-morph", () => {

morph(a, b);

await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
});

Expand All @@ -18,6 +21,8 @@ describe("alpine-morph", () => {

morph(a, b);

await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
});

Expand All @@ -32,6 +37,8 @@ describe("alpine-morph", () => {

morph(a, b);

await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
});

Expand All @@ -46,6 +53,8 @@ describe("alpine-morph", () => {

morph(a, b);

await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
});

Expand All @@ -55,6 +64,8 @@ describe("alpine-morph", () => {

morph(a, b);

await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
});

Expand All @@ -64,6 +75,8 @@ describe("alpine-morph", () => {

morph(a, b);

await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
});

Expand All @@ -73,6 +86,8 @@ describe("alpine-morph", () => {

morph(a, b);

await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
});
});
7 changes: 7 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function nextFrame() {
return new Promise((resolve) => {
requestAnimationFrame(() => {
resolve();
});
});
}
10 changes: 10 additions & 0 deletions test/morphdom.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fixture, html, expect } from "@open-wc/testing";
import { morph } from "../";
import { nextFrame } from "./helpers";

// adapted from: https://github.com/patrick-steele-idem/morphdom/blob/e98d69e125cda814dd6d1ba71d6c7c9d93edc01e/test/browser/test.js
describe("morphdom", () => {
Expand All @@ -8,6 +9,7 @@ describe("morphdom", () => {
const b = await fixture(html`<div class="bar"></div>`);

morph(a, b);
await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
expect(a.className).to.equal("bar");
Expand All @@ -23,6 +25,7 @@ describe("morphdom", () => {
const b = await fixture(html`<body></body>`);

morph(a, b);
await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
expect(a.nodeName).to.equal("BODY");
Expand All @@ -34,6 +37,7 @@ describe("morphdom", () => {
const b = await fixture(html`<div id="el-1" class="bar"><div id="el-1">B</div></div>`);

morph(a, b);
await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
expect(a.className).to.equal("bar");
Expand All @@ -47,6 +51,7 @@ describe("morphdom", () => {
const b = await fixture(html`<div id="el-1" class="zoo"><div id="el-inner">B</div></div>`);

morph(a, b);
await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
expect(a.className).to.equal("zoo");
Expand All @@ -67,6 +72,7 @@ describe("morphdom", () => {
const b = await fixture(html`<div><p id="hi" class="foo">A</p></div>`);

morph(a, b);
await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
expect(a.children.length).to.equal(2);
Expand All @@ -89,6 +95,7 @@ describe("morphdom", () => {
const b = await fixture(html`<div><h1 id="matching" class="baz">C</h1></div>`);

morph(a, b);
await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
expect(a.children.length).to.equal(1);
Expand All @@ -102,6 +109,7 @@ describe("morphdom", () => {
const b = await fixture(html`<input type="text" value="Hello World 2" />`);

morph(a, b);
await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
expect(a.value).to.equal("Hello World 2");
Expand All @@ -113,6 +121,7 @@ describe("morphdom", () => {
const b = await fixture(html`<input type="text" checked="" />`);

morph(a, b);
await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
expect(a.checked).to.equal(true);
Expand All @@ -126,6 +135,7 @@ describe("morphdom", () => {
b.checked = true;

morph(a, b);
await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
expect(a.checked).to.equal(true);
Expand Down
10 changes: 10 additions & 0 deletions test/morphlex.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fixture, html, expect } from "@open-wc/testing";
import { morph } from "../";
import { nextFrame } from "./helpers";

describe("morph", () => {
it("doesn't cause iframes to reload", async () => {
Expand All @@ -19,6 +20,9 @@ describe("morph", () => {

const originalIframe = original.querySelector("iframe");
morph(original, reference);

await nextFrame();

expect(original.outerHTML).to.equal(reference.outerHTML);
});

Expand All @@ -34,6 +38,8 @@ describe("morph", () => {

morph(original, eventual);

await nextFrame();

expect(original.textContent).to.equal("Hello Joel");
});

Expand All @@ -47,6 +53,8 @@ describe("morph", () => {

morph(a, b);

await nextFrame();

expect(a.textContent).to.equal(b.textContent);
});

Expand All @@ -56,6 +64,8 @@ describe("morph", () => {

morph(a, b);

await nextFrame();

expect(a.outerHTML).to.equal(b.outerHTML);
});
});
Loading

0 comments on commit 146300c

Please sign in to comment.