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 ="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 >
18
11
</ div >
19
12
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 >
22
17
< li data-key ="3 "> baz< input > </ li >
23
18
< li data-key ="1 "> foo< input > </ li >
24
- < li data-key =" 2 " > bar < input > </ li >
19
+ </ ul >
25
20
</ div >
26
21
27
22
< div id ="b "> </ div >
@@ -33,98 +28,33 @@ <h2>Static Header (won't change)</h2>
33
28
</ div >
34
29
35
30
< 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 >
37
32
< button onclick ="next() "> Next Step</ button >
38
- < button onclick ="reset() "> Reset</ button >
39
33
</ div >
40
34
41
35
< 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
-
63
36
function start ( ) {
64
- findBoundaries ( ) ;
65
-
66
- if ( ! startBoundary || ! endBoundary ) {
67
- console . error ( 'Could not find boundary comments' ) ;
68
- return ;
69
- }
70
-
71
37
Alpine . morph . log ( ( message , from , to ) => {
72
38
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 )
79
41
let li = document . createElement ( 'li' )
80
42
li . textContent = message
81
43
message && document . querySelector ( '#log-message' ) . appendChild ( li )
82
44
} )
83
45
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
+ )
97
51
}
98
52
99
53
function next ( ) {
100
54
Alpine . morph . step ( )
101
55
setTimeout ( ( ) => window . dispatchEvent ( new CustomEvent ( 'refresh' , { bubbles : true } ) ) )
102
56
}
103
57
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
-
128
58
function escape ( unsafe ) {
129
59
return unsafe
130
60
. replace ( / \n / g, "⬎\n" )
0 commit comments