|
3 | 3 | <script src="./packages/alpinejs/dist/cdn.js" defer></script>
|
4 | 4 | <!-- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js" defer></script> -->
|
5 | 5 |
|
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> |
11 | 17 | </div>
|
12 | 18 |
|
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> |
20 | 24 | </div>
|
21 | 25 |
|
22 | 26 | <div id="b"></div>
|
|
28 | 32 | </div>
|
29 | 33 |
|
30 | 34 | <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> |
32 | 36 | <button onclick="next()">Next Step</button>
|
| 37 | + <button onclick="reset()">Reset</button> |
33 | 38 | </div>
|
34 | 39 |
|
35 | 40 | <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 | + |
36 | 62 | function start() {
|
| 63 | + findBoundaries(); |
| 64 | + |
| 65 | + if (!startBoundary || !endBoundary) { |
| 66 | + console.error('Could not find boundary comments'); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
37 | 70 | Alpine.morph.log((message, from, to) => {
|
38 | 71 | 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 | + } |
41 | 78 | let li = document.createElement('li')
|
42 | 79 | li.textContent = message
|
43 | 80 | message && document.querySelector('#log-message').appendChild(li)
|
44 | 81 | })
|
45 | 82 |
|
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 | + ); |
51 | 96 | }
|
52 | 97 |
|
53 | 98 | function next() {
|
54 | 99 | Alpine.morph.step()
|
55 | 100 | setTimeout(() => window.dispatchEvent(new CustomEvent('refresh', { bubbles: true })))
|
56 | 101 | }
|
57 | 102 |
|
| 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 | + |
58 | 127 | function escape(unsafe) {
|
59 | 128 | return unsafe
|
60 | 129 | .replace(/\n/g, "⬎\n")
|
|
0 commit comments