Skip to content

Commit

Permalink
Take two with combined refs
Browse files Browse the repository at this point in the history
  • Loading branch information
wenche committed Nov 5, 2020
1 parent d8e3b3b commit e7327bb
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions libraries/core-react/src/_common/useCombinedRefs.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// @ts-nocheck
import { useRef, useEffect, Ref } from 'react'

export const useCombinedRefs = <T extends any>(
...refs: Array<Ref<T>>
): Ref<T> => {
const targetRef = useRef(null)
import { useRef, useEffect } from 'react'

type Ref = React.MutableRefObject<HTMLElement>
type useCombinedRefsProps<T> = T | ((a: HTMLElement) => T)
export const useCombinedRefs = (...refs: useCombinedRefsProps<Ref>[]): Ref => {
const targetRef = useRef<HTMLElement>(null)
useEffect(() => {
refs.forEach((ref) => {
if (!ref) return
if (typeof ref === 'function') {
ref(targetRef.current)
} else {
;(ref as any).current = (targetRef as any).current
ref.current = targetRef.current
}
})
}, [refs])

return targetRef
}

0 comments on commit e7327bb

Please sign in to comment.