Skip to content

Instantly share code, notes, and snippets.

@wilsonpage
Forked from annevk/credit.md
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wilsonpage/9288711d94e73eadd018 to your computer and use it in GitHub Desktop.
Save wilsonpage/9288711d94e73eadd018 to your computer and use it in GitHub Desktop.

After Ryosuke, Travis, Wilson, William, and I discussed the problem for an hour over a burrito, William came up with this approach. All hail William.

var shadow = host.createShadowRoot({ mode: 'closed' });
// Fill the shadow-root
shadow.innerHTML = template;
// Define the distribution logic. It's important
// that this isn't tied to creation to make it
// possible for a sub-class to override
shadow.distribution = candidates => {
var points = shadow.querySelectorAll('content');
candidates.forEach(candidate => {
for (var i = 0; i < points.length; i++) {
var selector = points[i].getAttribute('select');
if (!selector || candidate.matches(selector)) {
points[i].add(candidate);
break;
}
});
});
};
// Run initial distribution
shadow.distribute();
// Attach mutation observer that invokes distribute() on childList changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment