DOMPP can enhance existing
server-rendered or static HTML
without recreating the DOM tree.
Unlike
setChildren(),
setEnhancement()
adopts the existing DOM structure
and attaches behavior directly onto it.
setEnhancement()
does not rerender or replace the
existing DOM tree.
setText()
only updates text nodes
setStyles()
only mutates inline styles
setAttributes()
only mutates attributes
setEvents()
only reattaches listeners
setChildren()
performs full structural replacement
// =====================================
// EXISTING DOM ENHANCEMENT
// =====================================
document
.getElementById(
"counter-app"
)
// ===================================
// LOCAL STATE
// ===================================
.setState({
count: 0
})
// ===================================
// ENHANCE EXISTING DOM
// ===================================
.setEnhancement(({
childNodes,
state,
setState
}) => {
// =================================
// EXISTING NODES
// =================================
const count =
childNodes[1];
const controls =
childNodes[3];
const increment =
controls.childNodes[1];
const decrement =
controls.childNodes[3];
// =================================
// REACTIVE COUNT
// =================================
count
.setText(
state.count
)
.setStyles({
color:
state.count > 0
? "limegreen"
: state.count < 0
? "tomato"
: "#222"
});
// =================================
// INCREMENT
// =================================
increment
.setEvents({
click() {
setState({
count:
state.count + 1
});
}
});
// =================================
// DECREMENT
// =================================
decrement
.setEvents({
click() {
setState({
count:
state.count - 1
});
}
});
});