} from './component'
import { currentRenderingInstance } from './componentRenderContext'
import { type EffectScope, setCurrentScope } from '@vue/reactivity'
+import { warn } from './warning'
/**
* @internal
simpleSetCurrentInstance(instance)
}
}
+
+const internalOptions = ['ce'] as const
+
+/**
+ * @internal
+ */
+export const useInstanceOption = <K extends (typeof internalOptions)[number]>(
+ key: K,
+ silent = false,
+): {
+ hasInstance: boolean
+ value: GenericComponentInstance[K] | undefined
+} => {
+ const instance = getCurrentGenericInstance()
+ if (!instance) {
+ if (__DEV__ && !silent) {
+ warn(`useInstanceOption called without an active component instance.`)
+ }
+ return { hasInstance: false, value: undefined }
+ }
+
+ if (!internalOptions.includes(key)) {
+ if (__DEV__) {
+ warn(
+ `useInstanceOption only accepts ` +
+ ` ${internalOptions.map(k => `'${k}'`).join(', ')} as key, got '${key}'.`,
+ )
+ }
+ return { hasInstance: true, value: undefined }
+ }
+
+ return { hasInstance: true, value: instance[key] }
+}
type VNodeProps,
createVNode,
defineComponent,
- getCurrentGenericInstance,
nextTick,
unref,
+ useInstanceOption,
warn,
} from '@vue/runtime-core'
import {
}
export function useHost(caller?: string): VueElementBase | null {
- const instance = getCurrentGenericInstance()
- const el = instance && (instance.ce as VueElementBase)
+ const { hasInstance, value } = useInstanceOption('ce', true)
+ const el = value as VueElementBase
if (el) {
return el
} else if (__DEV__) {
- if (!instance) {
+ if (!hasInstance) {
warn(
`${caller || 'useHost'} called without an active component instance.`,
)