Created
August 12, 2020 22:27
-
-
Save andyvanee/0c2e379f3c976eb8ffcb9d2bfeacee64 to your computer and use it in GitHub Desktop.
tagProps lit-element directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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