@@ -18,6 +18,7 @@ import {
18
18
ComponentOptions
19
19
} from './apiOptions'
20
20
import { ExtractPropTypes } from './componentProps'
21
+ import { currentRenderingInstance } from './componentRenderUtils'
21
22
22
23
// `h` is a more user-friendly version of `createVNode` that allows omitting the
23
24
// props when possible. It is intended for manually written render functions.
@@ -77,52 +78,52 @@ interface Constructor<P = any> {
77
78
// manually written render functions.
78
79
79
80
// element
80
- export function h ( type : string , children ?: RawChildren ) : VNode
81
- export function h (
81
+ function _h ( type : string , children ?: RawChildren ) : VNode
82
+ function _h (
82
83
type : string ,
83
84
props ?: RawProps | null ,
84
85
children ?: RawChildren
85
86
) : VNode
86
87
87
88
// fragment
88
- export function h ( type : typeof Fragment , children ?: VNodeArrayChildren ) : VNode
89
- export function h (
89
+ function _h ( type : typeof Fragment , children ?: VNodeArrayChildren ) : VNode
90
+ function _h (
90
91
type : typeof Fragment ,
91
92
props ?: RawProps | null ,
92
93
children ?: VNodeArrayChildren
93
94
) : VNode
94
95
95
96
// portal (target prop is required)
96
- export function h (
97
+ function _h (
97
98
type : typeof Portal ,
98
99
props : RawProps & PortalProps ,
99
100
children : RawChildren
100
101
) : VNode
101
102
102
103
// suspense
103
- export function h ( type : typeof Suspense , children ?: RawChildren ) : VNode
104
- export function h (
104
+ function _h ( type : typeof Suspense , children ?: RawChildren ) : VNode
105
+ function _h (
105
106
type : typeof Suspense ,
106
107
props ?: ( RawProps & SuspenseProps ) | null ,
107
108
children ?: RawChildren | RawSlots
108
109
) : VNode
109
110
110
111
// functional component
111
- export function h ( type : FunctionalComponent , children ?: RawChildren ) : VNode
112
- export function h < P > (
112
+ function _h ( type : FunctionalComponent , children ?: RawChildren ) : VNode
113
+ function _h < P > (
113
114
type : FunctionalComponent < P > ,
114
115
props ?: ( RawProps & P ) | ( { } extends P ? null : never ) ,
115
116
children ?: RawChildren | RawSlots
116
117
) : VNode
117
118
118
119
// stateful component
119
- export function h ( type : ComponentOptions , children ?: RawChildren ) : VNode
120
- export function h (
120
+ function _h ( type : ComponentOptions , children ?: RawChildren ) : VNode
121
+ function _h (
121
122
type : ComponentOptionsWithoutProps | ComponentOptionsWithArrayProps ,
122
123
props ?: RawProps | null ,
123
124
children ?: RawChildren | RawSlots
124
125
) : VNode
125
- export function h < O > (
126
+ function _h < O > (
126
127
type : ComponentOptionsWithObjectProps < O > ,
127
128
props ?:
128
129
| ( RawProps & ExtractPropTypes < O > )
@@ -131,15 +132,15 @@ export function h<O>(
131
132
) : VNode
132
133
133
134
// fake constructor type returned by `defineComponent` or class component
134
- export function h ( type : Constructor , children ?: RawChildren ) : VNode
135
- export function h < P > (
135
+ function _h ( type : Constructor , children ?: RawChildren ) : VNode
136
+ function _h < P > (
136
137
type : Constructor < P > ,
137
138
props ?: ( RawProps & P ) | ( { } extends P ? null : never ) ,
138
139
children ?: RawChildren | RawSlots
139
140
) : VNode
140
141
141
142
// Actual implementation
142
- export function h ( type : any , propsOrChildren ?: any , children ?: any ) : VNode {
143
+ function _h ( type : any , propsOrChildren ?: any , children ?: any ) : VNode {
143
144
if ( arguments . length === 2 ) {
144
145
if ( isObject ( propsOrChildren ) && ! isArray ( propsOrChildren ) ) {
145
146
// single vnode without props
@@ -159,3 +160,24 @@ export function h(type: any, propsOrChildren?: any, children?: any): VNode {
159
160
return createVNode ( type , propsOrChildren , children )
160
161
}
161
162
}
163
+
164
+ export const h : typeof _h = __DEV__ ? ( applyTransformedH as typeof _h ) : _h
165
+
166
+ let argsTransformer : Function | undefined
167
+
168
+ // This is used to hook into the h function and transform its arguments
169
+ // Useful for re-implementing behavior that was previously done with createElement in Vue 2
170
+ function applyTransformedH ( ...args : unknown [ ] ) : VNode {
171
+ if ( argsTransformer ) {
172
+ args = argsTransformer ( args , currentRenderingInstance )
173
+ }
174
+ return _h ( ...( args as Parameters < typeof _h > ) )
175
+ }
176
+
177
+ export function transformHArgs ( transformer : Function ) : void {
178
+ argsTransformer = transformer
179
+ }
180
+
181
+ export function resetTransformHArgs ( ) : void {
182
+ argsTransformer = undefined
183
+ }
0 commit comments