Skip to content
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

fix(scopeId): make slotted scopeId work with Transition/TransitionGroup/KeepAlive component #3150

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/runtime-core/__tests__/components/KeepAlive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,9 @@ describe('KeepAlive', () => {
__scopeId: 'foo',
render: withId(() => {
return h(KeepAlive, null, {
default: () => h(views[viewRef.value], { ref: instanceRef })
// since the children of the KeepAlive component will not be compiled as slots,
// so, the parent's scopeId should be attached to it's children
default: withId(() => h(views[viewRef.value], { ref: instanceRef }))
})
})
}
Expand Down
59 changes: 58 additions & 1 deletion packages/runtime-core/__tests__/helpers/scopeId.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { withScopeId } from '../../src/helpers/scopeId'
import { h, render, nodeOps, serializeInner } from '@vue/runtime-test'
import {
h,
render,
nodeOps,
serializeInner,
BaseTransition,
KeepAlive
} from '@vue/runtime-test'

describe('scopeId runtime support', () => {
const withParentId = withScopeId('parent')
Expand Down Expand Up @@ -102,4 +109,54 @@ describe('scopeId runtime support', () => {

expect(serializeInner(root)).toBe(`<div parent></div>`)
})

test("The Transition component should inherit its parent's scopeId", () => {
const Child = {
__scopeId: 'child',
render: withChildId(function(this: any) {
return h(BaseTransition, withChildId(() => this.$slots.default()))
})
}
const App = {
__scopeId: 'parent',
render: withParentId(() => {
return h(
'div',
h(Child, null, {
default: withParentId(() => h('span'))
})
)
})
}
const root = nodeOps.createElement('div')
render(h(App), root)
expect(serializeInner(root)).toBe(
`<div parent><span parent child-s></span></div>`
)
})

test("The KeepAlive component should inherit its parent's scopeId", () => {
const Child = {
__scopeId: 'child',
render: withChildId(function(this: any) {
return h(KeepAlive, withChildId(() => this.$slots.default()))
})
}
const App = {
__scopeId: 'parent',
render: withParentId((ctx: any) => {
return h(
'div',
h(Child, null, {
default: withParentId(() => h('span'))
})
)
})
}
const root = nodeOps.createElement('div')
render(h(App), root)
expect(serializeInner(root)).toBe(
`<div parent><span parent child-s></span></div>`
)
})
})
4 changes: 4 additions & 0 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export interface ComponentInternalOptions {
* @internal
*/
__scopeId?: string
/**
* @internal
*/
__inheritScopeId?: boolean
/**
* @internal
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/components/BaseTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ const TransitionHookValidator = [Function, Array]
const BaseTransitionImpl = {
name: `BaseTransition`,

__inheritScopeId: true,

props: {
mode: String,
appear: Boolean,
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const KeepAliveImpl = {
// would prevent it from being tree-shaken.
__isKeepAlive: true,

__inheritScopeId: true,

props: {
include: [String, RegExp, Array],
exclude: [String, RegExp, Array],
Expand Down
14 changes: 12 additions & 2 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,15 @@ function baseCreateRenderer(
}
}

const getTreeOwnerWithScopeId = (
parent: ComponentInternalInstance
): ComponentInternalInstance => {
if (parent.type.__inheritScopeId && parent.parent) {
return getTreeOwnerWithScopeId(parent.parent)
}
return parent
}

const setScopeId = (
el: RendererElement,
scopeId: string | false | null,
Expand All @@ -817,13 +826,14 @@ function baseCreateRenderer(
hostSetScopeId(el, scopeId)
}
if (parentComponent) {
const treeOwnerId = parentComponent.type.__scopeId
const treeOwner = getTreeOwnerWithScopeId(parentComponent)
const treeOwnerId = treeOwner.type.__scopeId
// vnode's own scopeId and the current patched component's scopeId is
// different - this is a slot content node.
if (treeOwnerId && treeOwnerId !== scopeId) {
hostSetScopeId(el, treeOwnerId + '-s')
}
let subTree = parentComponent.subTree
let subTree = treeOwner.subTree
if (__DEV__ && subTree.type === Fragment) {
subTree =
filterSingleRoot(subTree.children as VNodeArrayChildren) || subTree
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-dom/src/components/Transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const Transition: FunctionalComponent<TransitionProps> = (
) => h(BaseTransition, resolveTransitionProps(props), slots)

Transition.displayName = 'Transition'
Transition.__inheritScopeId = true

const DOMTransitionPropsValidators = {
name: String,
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-dom/src/components/TransitionGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export type TransitionGroupProps = Omit<TransitionProps, 'mode'> & {
const TransitionGroupImpl = {
name: 'TransitionGroup',

__inheritScopeId: true,

props: /*#__PURE__*/ extend({}, TransitionPropsValidators, {
tag: String,
moveClass: String
Expand Down