Last active
January 10, 2020 15:30
-
-
Save tomtev/d2fa44ff68e5d7d007b47488a53ac5fc to your computer and use it in GitHub Desktop.
A simple Vue wrapper component that let you add classes to all children.
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
<script> | |
export default { | |
name: 'ChildClasses', | |
functional: true, | |
render (h, ctx) { | |
return ctx.children.map((vnode, index) => { | |
let { tag, data = {} } = vnode | |
let props = ctx.props | |
let children = vnode.children | |
let on = data.on || {} | |
const classes = [ | |
data.class, | |
ctx.data.class | |
].filter(Boolean) | |
const staticClass = [ | |
data.staticClass, | |
ctx.data.staticClass | |
].filter(Boolean).join(' ') | |
if (vnode.componentOptions) { | |
tag = vnode.componentOptions.tag | |
props = vnode.componentOptions.propsData | |
children = vnode.componentOptions.children | |
on = vnode.componentOptions.listeners | |
} | |
return h(tag, { ...data, class: classes, staticClass, on, props }, children) | |
}) | |
} | |
} | |
</script> |
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
<template> | |
<ChildClasses class="mr-5"> | |
<!-- All children inside slot will get a .mr-5 class --> | |
<slot /> | |
</ChildClasses> | |
</template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment