Skip to content

Commit e0f6af9

Browse files
committed
wip
1 parent 49b474c commit e0f6af9

File tree

2 files changed

+92
-22
lines changed

2 files changed

+92
-22
lines changed

morph.html

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33
<script src="./packages/alpinejs/dist/cdn.js" defer></script>
44
<!-- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js" defer></script> -->
55

6-
<div id="before">
7-
<!-- Before markup goes here: -->
8-
<ul>
9-
<li data-key="1">foo<input></li>
10-
</ul>
6+
<div id="container">
7+
<h2>Static Header (won't change)</h2>
8+
9+
<!-- Initial content with boundaries around li elements: -->
10+
<ul>
11+
<!-- [MORPH_START] -->
12+
<li data-key="1">foo<input></li>
13+
<!-- [MORPH_END] -->
14+
</ul>
15+
16+
<p>Static Footer (won't change)</p>
1117
</div>
1218

13-
<div id="after" style="display: none;">
14-
<!-- After markup goes here: -->
15-
<ul>
16-
<li data-key="2">bar<input></li>
17-
<li data-key="3">baz<input></li>
18-
<li data-key="1">foo<input></li>
19-
</ul>
19+
<div id="new-content" style="display: none;">
20+
<!-- New li content to morph to (between boundaries): -->
21+
<li data-key="2">bar<input></li>
22+
<li data-key="3">baz<input></li>
23+
<li data-key="1">foo<input></li>
2024
</div>
2125

2226
<div id="b"></div>
@@ -28,33 +32,98 @@
2832
</div>
2933

3034
<div style="position: absolute; bottom: 0; left: 0; padding: 1rem; width: 100vw; background: gray; box-sizing: border-box;">
31-
<button onclick="start()">Start</button>
35+
<button onclick="start()">Start morphBetween</button>
3236
<button onclick="next()">Next Step</button>
37+
<button onclick="reset()">Reset</button>
3338
</div>
3439

3540
<script>
41+
let startBoundary, endBoundary;
42+
43+
function findBoundaries() {
44+
const container = document.querySelector('#container');
45+
const walker = document.createTreeWalker(
46+
container,
47+
NodeFilter.SHOW_COMMENT,
48+
null,
49+
false
50+
);
51+
52+
let node;
53+
while (node = walker.nextNode()) {
54+
if (node.textContent.trim() === '[MORPH_START]') {
55+
startBoundary = node;
56+
} else if (node.textContent.trim() === '[MORPH_END]') {
57+
endBoundary = node;
58+
}
59+
}
60+
}
61+
3662
function start() {
63+
findBoundaries();
64+
65+
if (!startBoundary || !endBoundary) {
66+
console.error('Could not find boundary comments');
67+
return;
68+
}
69+
3770
Alpine.morph.log((message, from, to) => {
3871
console.log(message, from, to)
39-
document.querySelector('#log-from').innerHTML = escape(from.outerHTML)
40-
document.querySelector('#log-to').innerHTML = escape(to.outerHTML)
72+
if (from && from.outerHTML) {
73+
document.querySelector('#log-from').innerHTML = escape(from.outerHTML)
74+
}
75+
if (to && to.outerHTML) {
76+
document.querySelector('#log-to').innerHTML = escape(to.outerHTML)
77+
}
4178
let li = document.createElement('li')
4279
li.textContent = message
4380
message && document.querySelector('#log-message').appendChild(li)
4481
})
4582

46-
Alpine.morph(
47-
document.querySelector('#before').firstElementChild,
48-
document.querySelector('#after').firstElementChild.outerHTML,
49-
{ debug: true, key(el) { return el.dataset.key } }
50-
)
83+
// Get the new content HTML
84+
const newContentHtml = document.querySelector('#new-content').innerHTML;
85+
86+
// Use morphBetween to morph content between the boundaries
87+
Alpine.morphBetween(
88+
startBoundary,
89+
endBoundary,
90+
newContentHtml,
91+
{
92+
debug: true,
93+
key(el) { return el.dataset.key }
94+
}
95+
);
5196
}
5297

5398
function next() {
5499
Alpine.morph.step()
55100
setTimeout(() => window.dispatchEvent(new CustomEvent('refresh', { bubbles: true })))
56101
}
57102

103+
function reset() {
104+
// Reset to original state
105+
findBoundaries();
106+
if (startBoundary && endBoundary) {
107+
const originalContent = `
108+
<ul>
109+
<li data-key="1">foo<input></li>
110+
</ul>
111+
`;
112+
113+
Alpine.morphBetween(
114+
startBoundary,
115+
endBoundary,
116+
originalContent,
117+
{ key(el) { return el.dataset.key } }
118+
);
119+
}
120+
121+
// Clear logs
122+
document.querySelector('#log-from').innerHTML = '';
123+
document.querySelector('#log-to').innerHTML = '';
124+
document.querySelector('#log-message').innerHTML = '';
125+
}
126+
58127
function escape(unsafe) {
59128
return unsafe
60129
.replace(/\n/g, "⬎\n")

packages/morph/src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { morph } from './morph'
1+
import { morph, morphBetween } from './morph'
22

33
export default function (Alpine) {
44
Alpine.morph = morph
5+
Alpine.morphBetween = morphBetween
56
}
67

7-
export { morph }
8+
export { morph, morphBetween }

0 commit comments

Comments
 (0)