@primer/react
Version:
An implementation of GitHub's Primer Design System using React
51 lines • 2.17 kB
TypeScript
import type { ForwardedRef, Ref as StandardRef } from 'react';
/**
* Combine two refs of matching type (typically an external or forwarded ref and an internal `useRef` object or
* callback ref).
*
* If you need to combine more than two refs (what are you doing?) just nest the hook:
* `useMergedRefs(refA, useMergedRefs(refB, refC))`
*
* @param refA First ref to merge. The order is not important.
* @param refB Second ref to merge. The order is not important.
* @returns A new ref which must be passed to the relevant child component. **Important**: do not pass `refA` or
* `refB` to the component!
*
* @example
* // React 18
* const Example = forwardRef<HTMLButtonElement, {}>((props, forwardedRef) => {
* const ref = useRef<HTMLButtonElement>(null)
* const combinedRef = useMergedRefs(forwardedRef, ref)
*
* return <button ref={combinedRef} />
* })
*
* @example
* // React 19
* const Example = ({ref: externalRef}: {ref?: Ref<HTMLButtonElement>}) => {
* const ref = useRef<HTMLButtonElement>(null)
* const combinedRef = useMergedRefs(externalRef, ref)
*
* return <button ref={combinedRef} />
* }
*/
export declare function useMergedRefs<T>(refA: Ref<T | null>, refB: Ref<T | null>): (value: T | null) => () => void;
type CleanupFunction = () => void;
/**
* React 19 supports callback refs that can return a cleanup function. If a cleanup function is returned, the
* cleanup is called on unmount **instead** of setting the ref to null.
*/
type React19RefCallback<T> = {
bivarianceHack(instance: T): void | CleanupFunction;
}['bivarianceHack'];
/**
* Supporting React 18 and 19 while alleviating the need for any hacks or casts in consumers:
* - `ForwardedRef` from the React 18 `forwardRef` HOC
* - `React19RefCallback` for callback refs that can return a cleanup function (this is included in `Ref` in React 19
* but not in 18)
* - `Ref` for standard refs from `useRef` or passed in as React 19 prop
* - `undefined` to allow for easy use of optional `ref` props in React 19
*/
type Ref<T> = ForwardedRef<T> | React19RefCallback<T> | StandardRef<T> | undefined;
export {};
//# sourceMappingURL=useMergedRefs.d.ts.map