UNPKG

flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

1 lines 1.55 kB
{"version":3,"file":"merge-refs.cjs","sources":["../../src/helpers/merge-refs.ts"],"sourcesContent":["/**\n * Merges multiple React refs into a single ref callback function.\n *\n * This utility function combines multiple refs (callback refs, mutable refs, etc.)\n * into a single callback ref that updates all provided refs.\n *\n * @param refs - An array of refs to be merged. Can contain:\n * - MutableRefObject\n * - LegacyRef\n * - undefined\n * - null\n *\n * @returns A RefCallback function that updates all provided refs with the current value\n *\n * @example\n * ```tsx\n * const firstRef = useRef(null);\n * const secondRef = useRef(null);\n *\n * <div ref={mergeRefs([firstRef, secondRef])} />\n * ```\n *\n * @typeParam T - The type of value the refs will reference\n */\nexport function mergeRefs<T>(\n refs: Array<React.MutableRefObject<T> | React.LegacyRef<T> | undefined | null>,\n): React.RefCallback<T> {\n return (value) => {\n refs.forEach((ref) => {\n if (typeof ref === \"function\") {\n ref(value);\n } else if (ref != null) {\n const _ref = ref as React.MutableRefObject<T | null>;\n _ref.current = value;\n }\n });\n };\n}\n"],"names":[],"mappings":";;AACO,SAAS,SAAS,CAAC,IAAI,EAAE;AAChC,EAAE,OAAO,CAAC,KAAK,KAAK;AACpB,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,KAAK;AAC1B,MAAM,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE;AACrC,QAAQ,GAAG,CAAC,KAAK,CAAC;AAClB,OAAO,MAAM,IAAI,GAAG,IAAI,IAAI,EAAE;AAC9B,QAAQ,MAAM,IAAI,GAAG,GAAG;AACxB,QAAQ,IAAI,CAAC,OAAO,GAAG,KAAK;AAC5B;AACA,KAAK,CAAC;AACN,GAAG;AACH;;;;"}