Skip to content

Instantly share code, notes, and snippets.

@andyvanee
Created August 12, 2020 22:27
Show Gist options
  • Save andyvanee/0c2e379f3c976eb8ffcb9d2bfeacee64 to your computer and use it in GitHub Desktop.
Save andyvanee/0c2e379f3c976eb8ffcb9d2bfeacee64 to your computer and use it in GitHub Desktop.
tagProps lit-element directive
import { directive } from "https://unpkg.com/@polymer/lit-element/lit-element.js?module"
const stateMap = new WeakMap()
export const tagProps = directive((tag, props = {}) => part => {
let state = stateMap.get(part)
const el = () => document.createElement(tag)
if (state === undefined) {
state = { tag, element: el() }
stateMap.set(part, state)
}
if (state.tag != tag) {
state.element = el()
stateMap.set(part, state)
}
Object.assign(state.element, props)
part.setValue(state.element)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment