Skip to content

feat(core): add staticClass merge from slot #8312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/compiler/codegen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,16 @@ function genSlot (el: ASTElement, state: CodegenState): string {
const slotName = el.slotName || '"default"'
const children = genChildren(el, state)
let res = `_t(${slotName}${children ? `,${children}` : ''}`
const attrs = el.attrs && `{${el.attrs.map(a => `${camelize(a.name)}:${a.value}`).join(',')}}`
let attrs
if (el.attrs) {
attrs = `{${el.attrs.map(a => `${camelize(a.name)}:${a.value}`).join(',')}`
}
if (el.staticClass) {
attrs = attrs ? `${attrs},` : '{' + `class:${el.staticClass}`
}
if (attrs) {
attrs += '}'
}
const bind = el.attrsMap['v-bind']
if ((attrs || bind) && !children) {
res += `,null`
Expand Down
3 changes: 3 additions & 0 deletions src/core/instance/render-helpers/render-slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export function renderSlot (
)
}
slotNodes._rendered = true
if (props && props.class) {
slotNodes[0].data.staticClass += ` ${props.class}`
}
}
nodes = slotNodes || fallback
}
Expand Down
23 changes: 23 additions & 0 deletions test/unit/features/directives/class.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ describe('Directive v-bind:class', () => {
}).then(done)
})

it('class merge from slot', done => {
const vm = new Vue({
template: `
<component1>
<div class="staticClass" slot="test">
some text
</div>
</component1>
`,
components: {
component1: {
template: `
<div>
<slot name="test" class="fromSlot"/>
</div>
`
}
}
}).$mount()
expect(vm.$el.children[0].className).toBe('staticClass fromSlot')
done()
})

it('deep update', done => {
const vm = new Vue({
template: '<div :class="test"></div>',
Expand Down
7 changes: 7 additions & 0 deletions test/unit/modules/compiler/codegen.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ describe('codegen', () => {
)
})

it('generate slot with class', () => {
assertCodegen(
'<div><slot class="class1"></slot></div>',
`with(this){return _c('div',[_t("default",null,{class:"class1"})],2)}`
)
})

it('generate class binding', () => {
// static
assertCodegen(
Expand Down