{"version":3,"file":"use-combined-ref.cjs","sources":["../../../src/hooks/use-combined-ref.ts"],"sourcesContent":["import { useRef, useMemo, type RefObject, type ForwardedRef } from 'react';\n\n/**\n * Sets the value of a forwarded ref, handling both function refs and object refs.\n * @param ref - The forwarded ref to set.\n * @param value - The value to set on the ref.\n * @private\n */\nexport function setForwardRef<T>(ref: ForwardedRef<T> | undefined, value: T | null) {\n  if (!ref) return;\n\n  if (typeof ref === 'function') {\n    ref(value);\n  } else if ('current' in ref) {\n    ref.current = value;\n  }\n}\n/**\n * Custom hook to combine a forwarded ref with an internal ref.\n * This is useful when you want to pass a ref to a component while also keeping a reference to the element internally.\n * @private\n * @param ref - The forwarded ref to combine with the internal ref.\n * @returns A ref object that combines the forwarded ref and the internal ref.\n * @example\n * const MyComponent = forwardRef((props, ref) => {\n *   const combinedRef = useCombinedRef(ref);\n *   return <div ref={combinedRef}>Hello</div>;\n * });\n */\nexport function useCombinedRef<T>(ref?: ForwardedRef<T>): RefObject<T | null> {\n  // Explicitly nullable so `current` stays mutable under React 18's typings\n  // too — the proxy setter below assigns to it.\n  const innerRef = useRef<T | null>(null);\n\n  // Assign the forwarded ref DURING COMMIT — when React writes to this object's\n  // `current` — not in a passive effect. React attaches refs before layout effects\n  // run, and consumers rely on that: a parent that reads the forwarded ref in its own\n  // `useLayoutEffect` would see `null` if the assignment were deferred to a passive\n  // effect (parent layout effects run first). `useMeasureElement` is exactly such a\n  // consumer — it bails on a null node and never re-registers, leaving the element\n  // 0x0 in production (StrictMode's remount masks it in dev/tests). A proxy whose\n  // setter forwards keeps the `RefObject` read interface (`.current`) callers depend\n  // on, which a plain callback ref could not. Keyed on `ref` so a changed forwarded\n  // ref re-attaches and receives the current node.\n  return useMemo<RefObject<T | null>>(\n    () => ({\n      get current() {\n        return innerRef.current;\n      },\n      set current(value: T | null) {\n        innerRef.current = value;\n        setForwardRef(ref, value);\n      },\n    }),\n    [ref]\n  );\n}\n"],"names":["useRef","useMemo"],"mappings":";;;;;;;;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