Skip to content

Commit

Permalink
Added style and class hook support for object values
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Mar 9, 2024
1 parent edbc285 commit 2f47e06
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/jdom.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions src/template/TemplateDOMAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ export default class TemplateDOMAdapter {
if (typeof value === 'object') {
elem.style = ''
Object.entries(value).forEach(([key, value]) => {
if (value instanceof Hook) {
const hook = value
const listener = v => el.style[key] = v
el.addEventListener(':detached', () => hook.removeListener(listener))
el.addEventListener(':attached', () => hook.addListener(listener))

value = hook.value
}
elem.style[key] = value
})
return
Expand All @@ -101,6 +109,19 @@ export default class TemplateDOMAdapter {
classes = value
} else if (typeof value === 'object') {
Object.entries(value).forEach(([key, value]) => {
if (value instanceof Hook) {
const hook = value
const listener = v => {
if (v && !el.classList.contains(key)) {
el.classList.add(key)
} else if (!v && el.classList.contains(key)) {
el.classList.remove(key)
}
}
el.addEventListener(':detached', () => hook.removeListener(listener))
el.addEventListener(':attached', () => hook.addListener(listener))
value = hook.value
}
if (value) {
classes.push(key)
}
Expand Down

0 comments on commit 2f47e06

Please sign in to comment.