Skip to content

Commit 38f2d23

Browse files
committed
feat(runtime-core): add inheritRef option + make <transition> & <keep-alive> inherit refs
1 parent 88c1e62 commit 38f2d23

File tree

7 files changed

+30
-6
lines changed

7 files changed

+30
-6
lines changed

packages/runtime-core/src/component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export interface FunctionalComponent<
8181
props?: ComponentPropsOptions<P>
8282
emits?: E | (keyof E)[]
8383
inheritAttrs?: boolean
84+
inheritRef?: boolean
8485
displayName?: string
8586
}
8687

packages/runtime-core/src/componentOptions.ts

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export interface ComponentOptionsBase<
100100
components?: Record<string, PublicAPIComponent>
101101
directives?: Record<string, Directive>
102102
inheritAttrs?: boolean
103+
inheritRef?: boolean
103104
emits?: E | EE[]
104105

105106
// Internal ------------------------------------------------------------------

packages/runtime-core/src/componentRenderUtils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ export function renderComponentRoot(
172172
}
173173
root.transition = vnode.transition
174174
}
175+
// inherit ref
176+
if (Component.inheritRef && vnode.ref != null) {
177+
root.ref = vnode.ref
178+
}
175179

176180
if (__DEV__ && setRoot) {
177181
setRoot(root)

packages/runtime-core/src/components/BaseTransition.ts

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ export function useTransitionState(): TransitionState {
100100
const BaseTransitionImpl = {
101101
name: `BaseTransition`,
102102

103+
inheritRef: true,
104+
103105
props: {
104106
mode: String,
105107
appear: Boolean,

packages/runtime-core/src/components/KeepAlive.ts

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ const KeepAliveImpl = {
6363
// would prevent it from being tree-shaken.
6464
__isKeepAlive: true,
6565

66+
inheritRef: true,
67+
6668
props: {
6769
include: [String, RegExp, Array],
6870
exclude: [String, RegExp, Array],

packages/runtime-core/src/renderer.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
ComponentInternalInstance,
1818
createComponentInstance,
1919
Data,
20-
setupComponent
20+
setupComponent,
21+
Component
2122
} from './component'
2223
import {
2324
renderComponentRoot,
@@ -64,6 +65,7 @@ import {
6465
import { createHydrationFunctions, RootHydrateFunction } from './hydration'
6566
import { invokeDirectiveHook } from './directives'
6667
import { startMeasure, endMeasure } from './profiling'
68+
import { ComponentPublicInstance } from './componentProxy'
6769

6870
export interface Renderer<HostElement = any> {
6971
render: RootRenderFunction<HostElement>
@@ -276,11 +278,21 @@ export const setRef = (
276278
parent: ComponentInternalInstance,
277279
vnode: VNode | null
278280
) => {
279-
const value = vnode
280-
? vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT
281-
? vnode.component!.proxy
282-
: vnode.el
283-
: null
281+
let value: ComponentPublicInstance | RendererNode | null
282+
if (!vnode) {
283+
value = null
284+
} else {
285+
const { el, component, shapeFlag, type } = vnode
286+
if (shapeFlag & ShapeFlags.COMPONENT && (type as Component).inheritRef) {
287+
return
288+
}
289+
if (shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
290+
value = component!.proxy
291+
} else {
292+
value = el
293+
}
294+
}
295+
284296
const [owner, ref] = rawRef
285297
if (__DEV__ && !owner) {
286298
warn(

packages/runtime-dom/src/components/Transition.ts

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export const Transition: FunctionalComponent<TransitionProps> = (
3737
{ slots }
3838
) => h(BaseTransition, resolveTransitionProps(props), slots)
3939

40+
Transition.inheritRef = true
41+
4042
export const TransitionPropsValidators = (Transition.props = {
4143
...(BaseTransition as any).props,
4244
name: String,

0 commit comments

Comments
 (0)