refs.ts 534 B

123456789101112131415161718192021
  1. import { isFunction } from '../types'
  2. import type { ComponentPublicInstance, Ref } from 'vue'
  3. export type RefSetter = (
  4. el: Element | ComponentPublicInstance | undefined
  5. ) => void
  6. export const composeRefs = (
  7. ...refs: (Ref<HTMLElement | undefined> | RefSetter)[]
  8. ) => {
  9. return (el: Element | ComponentPublicInstance | null) => {
  10. refs.forEach((ref) => {
  11. if (isFunction(ref)) {
  12. ref(el as Element | ComponentPublicInstance)
  13. } else {
  14. ref.value = el as HTMLElement | undefined
  15. }
  16. })
  17. }
  18. }