|
| 1 | +import * as React from "react" |
| 2 | + |
| 3 | +type PossibleRef<T> = React.Ref<T> | undefined |
| 4 | + |
| 5 | +/** |
| 6 | + * Set a given ref to a given value |
| 7 | + * This utility takes care of different types of refs: callback refs and RefObject(s) |
| 8 | + */ |
| 9 | +function setRef<T>(ref: PossibleRef<T>, value: T): void | (() => void) { |
| 10 | + if (typeof ref === "function") { |
| 11 | + return ref(value) |
| 12 | + } else if (ref !== null && ref !== undefined) { |
| 13 | + ;(ref as React.MutableRefObject<T>).current = value |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * A utility to compose multiple refs together |
| 19 | + * Accepts callback refs and RefObject(s) |
| 20 | + */ |
| 21 | +function composeRefs<T>(...refs: PossibleRef<T>[]): React.RefCallback<T> { |
| 22 | + return (node) => { |
| 23 | + let hasCleanup = false |
| 24 | + const cleanups = refs.map((ref) => { |
| 25 | + const cleanup = setRef(ref, node) |
| 26 | + if (!hasCleanup && typeof cleanup === "function") { |
| 27 | + hasCleanup = true |
| 28 | + } |
| 29 | + return cleanup |
| 30 | + }) |
| 31 | + // React <19 will log an error to the console if a callback ref returns a |
| 32 | + // value. We don't use ref cleanups internally so this will only happen if a |
| 33 | + // user's ref callback returns a value, which we only expect if they are |
| 34 | + // using the cleanup functionality added in React 19. |
| 35 | + if (hasCleanup) { |
| 36 | + return () => { |
| 37 | + for (let i = 0; i < cleanups.length; i++) { |
| 38 | + const cleanup = cleanups[i] |
| 39 | + if (typeof cleanup === "function") { |
| 40 | + cleanup() |
| 41 | + } else { |
| 42 | + setRef(refs[i], null) |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * A custom hook that composes multiple refs |
| 52 | + * Accepts callback refs and RefObject(s) |
| 53 | + */ |
| 54 | +function useComposedRefs<T>(...refs: PossibleRef<T>[]): React.RefCallback<T> { |
| 55 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 56 | + return React.useCallback(composeRefs(...refs), refs) |
| 57 | +} |
| 58 | + |
| 59 | +export { useComposedRefs } |
0 commit comments