import { provide } from 'vue' import type { InjectionKey, ObjectDirective, Ref } from 'vue' type ForwardRefSetter = (el: T) => void export type ForwardRefInjectionContext = { setForwardRef: ForwardRefSetter } export const FORWARD_REF_INJECTION_KEY: InjectionKey = Symbol('elForwardRef') export const useForwardRef = (forwardRef: Ref) => { const setForwardRef = (el: T) => { forwardRef.value = el } provide(FORWARD_REF_INJECTION_KEY, { setForwardRef, }) } export const useForwardRefDirective = ( setForwardRef: ForwardRefSetter ): ObjectDirective => { return { mounted(el) { setForwardRef(el) }, updated(el) { setForwardRef(el) }, unmounted() { setForwardRef(null) }, } }