Skip to content

Instantly share code, notes, and snippets.

@rgruesbeck
Last active April 24, 2020 20:15
Show Gist options
  • Save rgruesbeck/0c7f3eb510c8496edfc9a626a7044d85 to your computer and use it in GitHub Desktop.
Save rgruesbeck/0c7f3eb510c8496edfc9a626a7044d85 to your computer and use it in GitHub Desktop.
const app = document.getElementById("app")
const render = (root) => {
let nodes = []
return function(strings, ...expressions) {
if (root.childElementCount > 0 && nodes.length > 0 && nodes.length === expressions.length) {
// update nodes in list with matching expressions
nodes.forEach((node, idx) => {
node.innerHTML = expressions[idx]
})
} else {
// render initial nodes to the root node
// set initial node list
root.innerHTML = expressions
.reduce((htmlString, expr, idx) => {
return htmlString
.replace(/>$/, ` data-update="${idx}">`)
.concat(expr, strings[idx+1])
}, strings[0])
nodes = document.querySelectorAll("[data-update]")
}
}
}
const html = render(app)
html`
<div>
<h1>${2/3}</h1>
<ul>
<li>${1}</li>
<li>${1}</li>
<li>${true ? `<span>true</span>` : `<span>false</span>`}</li>
</ul>
</div>
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment