Skip to content

Instantly share code, notes, and snippets.

@arthurevans
Created March 6, 2019 01:41
Show Gist options
  • Save arthurevans/120b12a4affd9bd2968524f50f0cce7e to your computer and use it in GitHub Desktop.
Save arthurevans/120b12a4affd9bd2968524f50f0cce7e to your computer and use it in GitHub Desktop.
// Import lit-html functions
import {html, render, directive, NodePart, createMarker} from 'lit-html';
// duplicate directive takes a single value, and renders it in the DOM twice
const duplicate = directive((value) => {
let part1;
let part2;
const createAndAppendPart = (containerPart) => {
// create markers surrounding content managed by the new part
const container = containerPart.startNode.parentNode;
const startNode = createMarker();
container.insertBefore(startNode, containerPart.endNode);
container.insertBefore(createMarker(), containerPart.endNode);
// create and insert the new part
const newPart = new NodePart(containerPart.options);
newPart.insertAfterNode(startNode);
return newPart;
}
return (containerPart) => {
if (!(containerPart instanceof NodePart)) {
throw new Error('duplicate directive can only be used in content bindings');
}
if (part1 === undefined) {
// create parts
part1 = createAndAppendPart(containerPart);
part2 = createAndAppendPart(containerPart);
}
part1.setValue(value);
part1.commit();
part2.setValue(value);
part2.commit();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment