Skip to content

Commit

Permalink
feat: detect and apply changes to dynamic attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenrui committed Feb 9, 2024
1 parent b48f0b2 commit 99d2597
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
13 changes: 12 additions & 1 deletion demo/observer.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,26 @@
>
list item (click to delete)
</button>
<button
class="px-2 py-1 bg-gray-200 rounded-md text-base"
:click="this.previousElementSibling.setAttribute(':class', 'isHovered ? \'bg-red-200\' : \'bg-gray-200\'')"
>
Change :class Attribute
</button>
</li>
<li>
<button
:mouseenter="isHovered2 = true"
:mouseleave="isHovered2 = false"
:class="isHovered2 ? 'bg-red-200' : ''"
:click="this.parentNode.parentNode.removeChild(this.parentNode)"
:text="`my id is ${this.getAttribute('id')} (hover me to change) list item (click to delete)`"
></button>
<button
class="px-2 py-1 bg-gray-200 rounded-md text-base"
:click="this.previousElementSibling.setAttribute(':id', '`list2${Math.random()}`')"
>
list item (click to delete)
Change :id Attribute
</button>
</li>
<li>
Expand Down
12 changes: 12 additions & 0 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ export default class Entity {
}
}

async evaluateAttribute(attribute) {
if (attribute === ':class') this.evaluateClass()
else if (attribute === ':text') this.evaluateText()
else if ([':value', ':checked'].includes(attribute)) this.evaluateValue()
else if (attribute === ':each') this.evaluateEach()
else {
if (!this.dynamicAttributes.includes(attribute))
this.dynamicAttributes.push(attribute)
this.evaluateDynamicAttributes()
}
}

async evaluateAll() {
await this.evaluateValue()
await this.evaluateClass()
Expand Down
6 changes: 5 additions & 1 deletion lib/generators/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ export function observeDOM(obj, callback) {
const mutationObserver = new MutationObserver(callback)

// have the observer observe for changes in children
mutationObserver.observe(obj, { childList: true, subtree: true })
mutationObserver.observe(obj, {
childList: true,
subtree: true,
attributes: true,
})

return mutationObserver
// browser support fallback
Expand Down
10 changes: 10 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ const MiniJS = (() => {
function _listenToDOMChanges() {
observeDOM(document.body, (mutation) => {
mutation.forEach((record) => {
if (
record.type === 'attributes' &&
record.attributeName.startsWith(':')
) {
const entity = _elements.find(
(entity) => entity.element === record.target
)
entity?.evaluateAttribute(record.attributeName)
}

record.removedNodes.forEach((node) => {
if (node.nodeType !== 1) return
const entity = _elements.find((entity) => entity.element === node)
Expand Down

0 comments on commit 99d2597

Please sign in to comment.