Skip to content

Commit 99d2597

Browse files
committed
feat: detect and apply changes to dynamic attributes
1 parent b48f0b2 commit 99d2597

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

demo/observer.html

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,26 @@
2020
>
2121
list item (click to delete)
2222
</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>
2329
</li>
2430
<li>
2531
<button
2632
:mouseenter="isHovered2 = true"
2733
:mouseleave="isHovered2 = false"
2834
:class="isHovered2 ? 'bg-red-200' : ''"
2935
: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()}`')"
3041
>
31-
list item (click to delete)
42+
Change :id Attribute
3243
</button>
3344
</li>
3445
<li>

lib/entity.js

+12
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,18 @@ export default class Entity {
265265
}
266266
}
267267

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+
268280
async evaluateAll() {
269281
await this.evaluateValue()
270282
await this.evaluateClass()

lib/generators/observer.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ export function observeDOM(obj, callback) {
99
const mutationObserver = new MutationObserver(callback)
1010

1111
// 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+
})
1317

1418
return mutationObserver
1519
// browser support fallback

lib/main.js

+10
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ const MiniJS = (() => {
108108
function _listenToDOMChanges() {
109109
observeDOM(document.body, (mutation) => {
110110
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+
111121
record.removedNodes.forEach((node) => {
112122
if (node.nodeType !== 1) return
113123
const entity = _elements.find((entity) => entity.element === node)

0 commit comments

Comments
 (0)