Skip to content

Commit

Permalink
feat(runtime-core): add inheritRef option + make <transition> & <keep…
Browse files Browse the repository at this point in the history
…-alive> inherit refs
  • Loading branch information
yyx990803 committed May 22, 2020
1 parent 88c1e62 commit 38f2d23
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export interface FunctionalComponent<
props?: ComponentPropsOptions<P>
emits?: E | (keyof E)[]
inheritAttrs?: boolean
inheritRef?: boolean
displayName?: string
}

Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface ComponentOptionsBase<
components?: Record<string, PublicAPIComponent>
directives?: Record<string, Directive>
inheritAttrs?: boolean
inheritRef?: boolean
emits?: E | EE[]

// Internal ------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime-core/src/componentRenderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ export function renderComponentRoot(
}
root.transition = vnode.transition
}
// inherit ref
if (Component.inheritRef && vnode.ref != null) {
root.ref = vnode.ref
}

if (__DEV__ && setRoot) {
setRoot(root)
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 @@ -100,6 +100,8 @@ export function useTransitionState(): TransitionState {
const BaseTransitionImpl = {
name: `BaseTransition`,

inheritRef: 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 @@ -63,6 +63,8 @@ const KeepAliveImpl = {
// would prevent it from being tree-shaken.
__isKeepAlive: true,

inheritRef: true,

props: {
include: [String, RegExp, Array],
exclude: [String, RegExp, Array],
Expand Down
24 changes: 18 additions & 6 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
ComponentInternalInstance,
createComponentInstance,
Data,
setupComponent
setupComponent,
Component
} from './component'
import {
renderComponentRoot,
Expand Down Expand Up @@ -64,6 +65,7 @@ import {
import { createHydrationFunctions, RootHydrateFunction } from './hydration'
import { invokeDirectiveHook } from './directives'
import { startMeasure, endMeasure } from './profiling'
import { ComponentPublicInstance } from './componentProxy'

export interface Renderer<HostElement = any> {
render: RootRenderFunction<HostElement>
Expand Down Expand Up @@ -276,11 +278,21 @@ export const setRef = (
parent: ComponentInternalInstance,
vnode: VNode | null
) => {
const value = vnode
? vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT
? vnode.component!.proxy
: vnode.el
: null
let value: ComponentPublicInstance | RendererNode | null
if (!vnode) {
value = null
} else {
const { el, component, shapeFlag, type } = vnode
if (shapeFlag & ShapeFlags.COMPONENT && (type as Component).inheritRef) {
return
}
if (shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
value = component!.proxy
} else {
value = el
}
}

const [owner, ref] = rawRef
if (__DEV__ && !owner) {
warn(
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-dom/src/components/Transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export const Transition: FunctionalComponent<TransitionProps> = (
{ slots }
) => h(BaseTransition, resolveTransitionProps(props), slots)

Transition.inheritRef = true

export const TransitionPropsValidators = (Transition.props = {
...(BaseTransition as any).props,
name: String,
Expand Down

0 comments on commit 38f2d23

Please sign in to comment.