Skip to content

Instantly share code, notes, and snippets.

@treshugart
Created September 5, 2016 02:48
Show Gist options
  • Save treshugart/64934c76d6ab18a07adaf53c7af0e031 to your computer and use it in GitHub Desktop.
Save treshugart/64934c76d6ab18a07adaf53c7af0e031 to your computer and use it in GitHub Desktop.
SkateJS stateless function for doing stuff with slotted nodes on first render and subsequent renders (slotchange event only fires on updates)
const $slotRendered = Symbol();
const Slot = (props, chren) => {
const { changed } = props;
function onSlotchange(e) {
const slot = e.target;
if (slot[$slotRendered]) {
if (changed) {
changed(slot);
}
} else {
slot[$slotRendered] = true;
}
}
function ref(slot) {
if (!slot[$slotRendered]) {
changed(slot);
}
}
return <slot onSlotchange={onSlotchange} ref={ref}>{chren()}</slot>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment