Skip to content

Commit 282125e

Browse files
committed
wip
1 parent 1120424 commit 282125e

File tree

1 file changed

+18
-88
lines changed

1 file changed

+18
-88
lines changed

morph.html

Lines changed: 18 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,20 @@
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="container" x-data="{ count: 0 }">
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-
<li data-key="2">bar<input></li>
14-
<!-- [MORPH_END] -->
15-
</ul>
16-
17-
<p>Static Footer (won't change)</p>
6+
<div id="before">
7+
<!-- Before markup goes here: -->
8+
<ul>
9+
<li data-key="1">foo<input></li>
10+
</ul>
1811
</div>
1912

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

2722
<div id="b"></div>
@@ -33,98 +28,33 @@ <h2>Static Header (won't change)</h2>
3328
</div>
3429

3530
<div style="position: absolute; bottom: 0; left: 0; padding: 1rem; width: 100vw; background: gray; box-sizing: border-box;">
36-
<button onclick="start()">Start morphBetween</button>
31+
<button onclick="start()">Start</button>
3732
<button onclick="next()">Next Step</button>
38-
<button onclick="reset()">Reset</button>
3933
</div>
4034

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

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

9953
function next() {
10054
Alpine.morph.step()
10155
setTimeout(() => window.dispatchEvent(new CustomEvent('refresh', { bubbles: true })))
10256
}
10357

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

0 commit comments

Comments
 (0)