Skip to content

Instantly share code, notes, and snippets.

@tomtev
Last active January 10, 2020 15:30
Show Gist options
  • Save tomtev/d2fa44ff68e5d7d007b47488a53ac5fc to your computer and use it in GitHub Desktop.
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.
<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>
<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