Skip to content

Commit f4a9a85

Browse files
committed
wip: slotScopeIds
1 parent 1169db8 commit f4a9a85

File tree

6 files changed

+56
-8
lines changed

6 files changed

+56
-8
lines changed

packages/runtime-core/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,10 @@ export { startMeasure, endMeasure } from './profiling'
561561
* @internal
562562
*/
563563
export { initFeatureFlags } from './featureFlags'
564+
/**
565+
* @internal
566+
*/
567+
export {
568+
setCurrentRenderingInstance,
569+
currentRenderingInstance,
570+
} from './componentRenderContext'

packages/runtime-vapor/__tests__/scopeId.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
setInsertionState,
88
template,
99
vaporInteropPlugin,
10+
withVaporCtx,
1011
} from '../src'
1112
import { makeRender } from './_utils'
1213

@@ -200,7 +201,7 @@ describe('scopeId', () => {
200201
test.todo('should attach scopeId to suspense content', async () => {})
201202

202203
// :slotted basic
203-
test.todo('should work on slots', () => {
204+
test('should work on slots', () => {
204205
const Child = defineVaporComponent({
205206
__scopeId: 'child',
206207
setup() {
@@ -225,11 +226,11 @@ describe('scopeId', () => {
225226
Child,
226227
null,
227228
{
228-
default: () => {
229+
default: withVaporCtx(() => {
229230
const n0 = template('<div parent></div>')()
230231
const n1 = createComponent(Child2)
231232
return [n0, n1]
232-
},
233+
}),
233234
},
234235
true,
235236
)
@@ -244,7 +245,7 @@ describe('scopeId', () => {
244245
// - scopeId from template context
245246
// - slotted scopeId from slot owner
246247
// - its own scopeId
247-
`<span child2="" child="" parent="" child-s=""></span>` +
248+
`<span child2="" parent="" child="" child-s=""></span>` +
248249
`<!--slot-->` +
249250
`</div>`,
250251
)

packages/runtime-vapor/src/block.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ export function setScopeId(block: Block, scopeId: string): void {
206206
export function setComponentScopeId(instance: VaporComponentInstance): void {
207207
const parent = instance.parent
208208
if (!parent) return
209-
210209
if (isArray(instance.block) && instance.block.length > 1) return
211210

212211
const scopeId = parent.type.__scopeId

packages/runtime-vapor/src/component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
type SuspenseBoundary,
1414
callWithErrorHandling,
1515
currentInstance,
16+
currentRenderingInstance,
1617
endMeasure,
1718
expose,
1819
nextUid,
@@ -282,6 +283,12 @@ export function createComponent(
282283

283284
onScopeDispose(() => unmountComponent(instance), true)
284285

286+
// component creation during slot rendering, should inherit scopeId
287+
// from current rendering instance
288+
const scopeId =
289+
currentRenderingInstance && currentRenderingInstance.type.__scopeId
290+
if (scopeId) setScopeId(instance.block, scopeId)
291+
285292
if (!isHydrating && _insertionParent) {
286293
mountComponent(instance, _insertionParent, _insertionAnchor)
287294
}

packages/runtime-vapor/src/componentSlots.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import { EMPTY_OBJ, NO, hasOwn, isArray, isFunction } from '@vue/shared'
2-
import { type Block, type BlockFn, DynamicFragment, insert } from './block'
2+
import {
3+
type Block,
4+
type BlockFn,
5+
DynamicFragment,
6+
insert,
7+
setScopeId,
8+
} from './block'
39
import { rawPropsProxyHandlers } from './componentProps'
4-
import { currentInstance, isRef } from '@vue/runtime-dom'
10+
import {
11+
type GenericComponentInstance,
12+
currentInstance,
13+
isRef,
14+
setCurrentRenderingInstance,
15+
} from '@vue/runtime-dom'
516
import type { LooseRawProps, VaporComponentInstance } from './component'
617
import { renderEffect } from './renderEffect'
718
import { insertionAnchor, insertionParent } from './insertionState'
@@ -97,6 +108,22 @@ export function forwardedSlotCreator(): (
97108
createSlot(name, rawProps, fallback, instance)
98109
}
99110

111+
export function withVaporCtx(
112+
fn: Function,
113+
ctx: GenericComponentInstance | null = currentInstance,
114+
): () => Block {
115+
return (...args: any[]): Block => {
116+
const prevInstance = setCurrentRenderingInstance(ctx as any)
117+
let res
118+
try {
119+
res = fn(...args)
120+
} finally {
121+
setCurrentRenderingInstance(prevInstance)
122+
}
123+
return res
124+
}
125+
}
126+
100127
export function createSlot(
101128
name: string | (() => string),
102129
rawProps?: LooseRawProps | null,
@@ -156,6 +183,9 @@ export function createSlot(
156183
}
157184
}
158185

186+
const scopeId = instance.type.__scopeId
187+
if (scopeId) setScopeId(fragment, `${scopeId}-s`)
188+
159189
if (!isHydrating && _insertionParent) {
160190
insert(fragment, _insertionParent, _insertionAnchor)
161191
}

packages/runtime-vapor/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ export { insert, prepend, remove, isFragment, VaporFragment } from './block'
99
export { setInsertionState } from './insertionState'
1010
export { createComponent, createComponentWithFallback } from './component'
1111
export { renderEffect } from './renderEffect'
12-
export { createSlot, forwardedSlotCreator } from './componentSlots'
12+
export {
13+
createSlot,
14+
forwardedSlotCreator,
15+
withVaporCtx,
16+
} from './componentSlots'
1317
export { template } from './dom/template'
1418
export { createTextNode, child, nthChild, next } from './dom/node'
1519
export {

0 commit comments

Comments
 (0)