File tree 4 files changed +39
-2
lines changed
4 files changed +39
-2
lines changed Original file line number Diff line number Diff line change 20
20
>
21
21
list item (click to delete)
22
22
</ button >
23
+ < button
24
+ class ="px-2 py-1 bg-gray-200 rounded-md text-base "
25
+ :click ="this.previousElementSibling.setAttribute(':class', 'isHovered ? \'bg-red-200\' : \'bg-gray-200\'') "
26
+ >
27
+ Change :class Attribute
28
+ </ button >
23
29
</ li >
24
30
< li >
25
31
< button
26
32
:mouseenter ="isHovered2 = true "
27
33
:mouseleave ="isHovered2 = false "
28
34
:class ="isHovered2 ? 'bg-red-200' : '' "
29
35
:click ="this.parentNode.parentNode.removeChild(this.parentNode) "
36
+ :text ="`my id is ${this.getAttribute('id')} (hover me to change) list item (click to delete)` "
37
+ > </ button >
38
+ < button
39
+ class ="px-2 py-1 bg-gray-200 rounded-md text-base "
40
+ :click ="this.previousElementSibling.setAttribute(':id', '`list2${Math.random()}`') "
30
41
>
31
- list item (click to delete)
42
+ Change :id Attribute
32
43
</ button >
33
44
</ li >
34
45
< li >
Original file line number Diff line number Diff line change @@ -265,6 +265,18 @@ export default class Entity {
265
265
}
266
266
}
267
267
268
+ async evaluateAttribute ( attribute ) {
269
+ if ( attribute === ':class' ) this . evaluateClass ( )
270
+ else if ( attribute === ':text' ) this . evaluateText ( )
271
+ else if ( [ ':value' , ':checked' ] . includes ( attribute ) ) this . evaluateValue ( )
272
+ else if ( attribute === ':each' ) this . evaluateEach ( )
273
+ else {
274
+ if ( ! this . dynamicAttributes . includes ( attribute ) )
275
+ this . dynamicAttributes . push ( attribute )
276
+ this . evaluateDynamicAttributes ( )
277
+ }
278
+ }
279
+
268
280
async evaluateAll ( ) {
269
281
await this . evaluateValue ( )
270
282
await this . evaluateClass ( )
Original file line number Diff line number Diff line change @@ -9,7 +9,11 @@ export function observeDOM(obj, callback) {
9
9
const mutationObserver = new MutationObserver ( callback )
10
10
11
11
// have the observer observe for changes in children
12
- mutationObserver . observe ( obj , { childList : true , subtree : true } )
12
+ mutationObserver . observe ( obj , {
13
+ childList : true ,
14
+ subtree : true ,
15
+ attributes : true ,
16
+ } )
13
17
14
18
return mutationObserver
15
19
// browser support fallback
Original file line number Diff line number Diff line change @@ -108,6 +108,16 @@ const MiniJS = (() => {
108
108
function _listenToDOMChanges ( ) {
109
109
observeDOM ( document . body , ( mutation ) => {
110
110
mutation . forEach ( ( record ) => {
111
+ if (
112
+ record . type === 'attributes' &&
113
+ record . attributeName . startsWith ( ':' )
114
+ ) {
115
+ const entity = _elements . find (
116
+ ( entity ) => entity . element === record . target
117
+ )
118
+ entity ?. evaluateAttribute ( record . attributeName )
119
+ }
120
+
111
121
record . removedNodes . forEach ( ( node ) => {
112
122
if ( node . nodeType !== 1 ) return
113
123
const entity = _elements . find ( ( entity ) => entity . element === node )
You can’t perform that action at this time.
0 commit comments