Skip to content

Instantly share code, notes, and snippets.

@CaitlinWeb-st
Created November 4, 2020 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaitlinWeb-st/339b232891205399abfd97710131a5ce to your computer and use it in GitHub Desktop.
Save CaitlinWeb-st/339b232891205399abfd97710131a5ce to your computer and use it in GitHub Desktop.
Vue component for slots to ensure each element has class(es) or tag
/**
* Ensures each child node (element or text) of the slot has a given tag or class.
*/
export default {
name: "SlotWrapper",
functional: true,
props: ["tag", "value"],
render: function(h, ctx) {
const { tag = "div", value: node, class: className = "" } = ctx.props;
// Render elements
if (node.tag) {
// handle given classes
node.data = node.data || {};
node.data.staticClass = (nodes.data.staticClass || "") + className;
return node;
}
// Render text
else if (!node.text.replace(/\s/g, "")) {
// ignore space nodes
return null;
}
return h(tag, [].concat(node));
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment